Skip to content

Commit

Permalink
no-useless-spread: Check `Array#{toReversed,toSorted,toSpliced,with…
Browse files Browse the repository at this point in the history
…}` (#2030)
  • Loading branch information
fisker authored Feb 2, 2023
1 parent fea5b42 commit 55469e5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
4 changes: 4 additions & 0 deletions rules/no-useless-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ const uselessArrayCloneSelector = [
'map',
'slice',
'splice',
'toReversed',
'toSorted',
'toSpliced',
'with',
]),
// `String#split()`
methodCallSelector('split'),
Expand Down
4 changes: 4 additions & 0 deletions test/no-useless-spread.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ test.snapshot({
'[...foo.map(bar)]',
'[...foo.slice(1)]',
'[...foo.splice(1)]',
'[...foo.toReversed()]',
'[...foo.toSorted()]',
'[...foo.toSpliced(0, 1)]',
'[...foo.with(0, bar)]',
'[...foo.split("|")]',
'[...Object.keys(foo)]',
'[...Object.values(foo)]',
Expand Down
86 changes: 75 additions & 11 deletions test/snapshots/no-useless-spread.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,70 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #9
1 | [...foo.toReversed()]

> Output
`␊
1 | foo.toReversed()␊
`

> Error 1/1
`␊
> 1 | [...foo.toReversed()]␊
| ^^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #10
1 | [...foo.toSorted()]

> Output
`␊
1 | foo.toSorted()␊
`

> Error 1/1
`␊
> 1 | [...foo.toSorted()]␊
| ^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #11
1 | [...foo.toSpliced(0, 1)]

> Output
`␊
1 | foo.toSpliced(0, 1)␊
`

> Error 1/1
`␊
> 1 | [...foo.toSpliced(0, 1)]␊
| ^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #12
1 | [...foo.with(0, bar)]

> Output
`␊
1 | foo.with(0, bar)␊
`

> Error 1/1
`␊
> 1 | [...foo.with(0, bar)]␊
| ^^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #13
1 | [...foo.split("|")]

> Output
Expand All @@ -1692,7 +1756,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #10
## Invalid #14
1 | [...Object.keys(foo)]

> Output
Expand All @@ -1708,7 +1772,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #11
## Invalid #15
1 | [...Object.values(foo)]

> Output
Expand All @@ -1724,7 +1788,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #12
## Invalid #16
1 | [...Array.from(foo)]

> Output
Expand All @@ -1740,7 +1804,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #13
## Invalid #17
1 | [...Array.of()]

> Output
Expand All @@ -1756,7 +1820,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #14
## Invalid #18
1 | [...new Array(3)]

> Error 1/1
Expand All @@ -1766,7 +1830,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #15
## Invalid #19
1 | [...await Promise.all(foo)]

> Output
Expand All @@ -1782,7 +1846,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #16
## Invalid #20
1 | [...await Promise.allSettled(foo)]

> Output
Expand All @@ -1798,7 +1862,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
`

## Invalid #17
## Invalid #21
1 | function foo(bar) {
2 | return[...Object.keys(bar)];
3 | }
Expand All @@ -1820,7 +1884,7 @@ Generated by [AVA](https://avajs.dev).
3 | }␊
`

## Invalid #18
## Invalid #22
1 | function foo(bar) {
2 | return[
3 | ...Object.keys(bar)
Expand Down Expand Up @@ -1850,7 +1914,7 @@ Generated by [AVA](https://avajs.dev).
5 | }␊
`

## Invalid #19
## Invalid #23
1 | function foo(bar) {
2 | return[
3 | ...(
Expand Down Expand Up @@ -1888,7 +1952,7 @@ Generated by [AVA](https://avajs.dev).
7 | }␊
`

## Invalid #20
## Invalid #24
1 | function foo(bar) {
2 | return([
3 | ...Object.keys(bar)
Expand Down
Binary file modified test/snapshots/no-useless-spread.mjs.snap
Binary file not shown.

0 comments on commit 55469e5

Please sign in to comment.