Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-useless-spread: Check Array#{toReversed,toSorted,toSpliced,with} #2030

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.