Skip to content

Commit 994e31c

Browse files
authored
no-useless-spread: Fix false positive on Iterator (#2831)
1 parent 8be6218 commit 994e31c

File tree

5 files changed

+25
-97
lines changed

5 files changed

+25
-97
lines changed

docs/rules/no-useless-spread.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,6 @@ function * foo() {
100100
}
101101
```
102102

103-
```js
104-
//
105-
function foo(bar) {
106-
return [
107-
...bar.map(x => x * 2),
108-
];
109-
}
110-
111-
//
112-
function foo(bar) {
113-
return bar.map(x => x * 2);
114-
}
115-
```
116-
117103
```js
118104
//
119105
const array = [...foo, bar];

rules/no-useless-spread.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,13 @@ const create = context => {
293293

294294
const node = arrayExpression.elements[0].argument;
295295
if (!(
296-
// Array methods returns a new array
296+
// Array methods returns a new array, but exclude these also exists in `Iterator`
297+
// `filter`, `flatMap`, and `map`
297298
isMethodCall(node, {
298299
methods: [
299300
'concat',
300301
'copyWithin',
301-
'filter',
302302
'flat',
303-
'flatMap',
304-
'map',
305303
'slice',
306304
'splice',
307305
'toReversed',

test/no-useless-spread.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,15 @@ test.snapshot({
259259
'[...Promise.all(foo)]',
260260
'[...Promise.allSettled(foo)]',
261261
'[...await Promise.all(foo, extraArgument)]',
262+
// Can be `Iterator` methods
263+
'[...foo.filter(bar)]',
264+
'[...foo.flatMap(bar)]',
265+
'[...foo.map(bar)]',
262266
],
263267
invalid: [
264268
'[...foo.concat(bar)]',
265269
'[...foo.copyWithin(-2)]',
266-
'[...foo.filter(bar)]',
267270
'[...foo.flat()]',
268-
'[...foo.flatMap(bar)]',
269-
'[...foo.map(bar)]',
270271
'[...foo.slice(1)]',
271272
'[...foo.splice(1)]',
272273
'[...foo.toReversed()]',

test/snapshots/no-useless-spread.js.md

Lines changed: 19 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,26 +2034,7 @@ Generated by [AVA](https://avajs.dev).
20342034
1 | foo.copyWithin(-2)␊
20352035
`
20362036

2037-
## invalid(3): [...foo.filter(bar)]
2038-
2039-
> Input
2040-
2041-
`␊
2042-
1 | [...foo.filter(bar)]␊
2043-
`
2044-
2045-
> Error 1/1
2046-
2047-
`␊
2048-
Message:␊
2049-
> 1 | [...foo.filter(bar)]␊
2050-
| ^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
2051-
2052-
Output:␊
2053-
1 | foo.filter(bar)␊
2054-
`
2055-
2056-
## invalid(4): [...foo.flat()]
2037+
## invalid(3): [...foo.flat()]
20572038

20582039
> Input
20592040
@@ -2072,45 +2053,7 @@ Generated by [AVA](https://avajs.dev).
20722053
1 | foo.flat()␊
20732054
`
20742055

2075-
## invalid(5): [...foo.flatMap(bar)]
2076-
2077-
> Input
2078-
2079-
`␊
2080-
1 | [...foo.flatMap(bar)]␊
2081-
`
2082-
2083-
> Error 1/1
2084-
2085-
`␊
2086-
Message:␊
2087-
> 1 | [...foo.flatMap(bar)]␊
2088-
| ^^^^^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
2089-
2090-
Output:␊
2091-
1 | foo.flatMap(bar)␊
2092-
`
2093-
2094-
## invalid(6): [...foo.map(bar)]
2095-
2096-
> Input
2097-
2098-
`␊
2099-
1 | [...foo.map(bar)]␊
2100-
`
2101-
2102-
> Error 1/1
2103-
2104-
`␊
2105-
Message:␊
2106-
> 1 | [...foo.map(bar)]␊
2107-
| ^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
2108-
2109-
Output:␊
2110-
1 | foo.map(bar)␊
2111-
`
2112-
2113-
## invalid(7): [...foo.slice(1)]
2056+
## invalid(4): [...foo.slice(1)]
21142057

21152058
> Input
21162059
@@ -2126,7 +2069,7 @@ Generated by [AVA](https://avajs.dev).
21262069
| ^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
21272070
`
21282071

2129-
## invalid(8): [...foo.splice(1)]
2072+
## invalid(5): [...foo.splice(1)]
21302073

21312074
> Input
21322075
@@ -2145,7 +2088,7 @@ Generated by [AVA](https://avajs.dev).
21452088
1 | foo.splice(1)␊
21462089
`
21472090

2148-
## invalid(9): [...foo.toReversed()]
2091+
## invalid(6): [...foo.toReversed()]
21492092

21502093
> Input
21512094
@@ -2164,7 +2107,7 @@ Generated by [AVA](https://avajs.dev).
21642107
1 | foo.toReversed()␊
21652108
`
21662109

2167-
## invalid(10): [...foo.toSorted()]
2110+
## invalid(7): [...foo.toSorted()]
21682111

21692112
> Input
21702113
@@ -2183,7 +2126,7 @@ Generated by [AVA](https://avajs.dev).
21832126
1 | foo.toSorted()␊
21842127
`
21852128

2186-
## invalid(11): [...foo.toSpliced(0, 1)]
2129+
## invalid(8): [...foo.toSpliced(0, 1)]
21872130

21882131
> Input
21892132
@@ -2202,7 +2145,7 @@ Generated by [AVA](https://avajs.dev).
22022145
1 | foo.toSpliced(0, 1)␊
22032146
`
22042147

2205-
## invalid(12): [...foo.with(0, bar)]
2148+
## invalid(9): [...foo.with(0, bar)]
22062149

22072150
> Input
22082151
@@ -2221,7 +2164,7 @@ Generated by [AVA](https://avajs.dev).
22212164
1 | foo.with(0, bar)␊
22222165
`
22232166

2224-
## invalid(13): [...foo.split("|")]
2167+
## invalid(10): [...foo.split("|")]
22252168

22262169
> Input
22272170
@@ -2240,7 +2183,7 @@ Generated by [AVA](https://avajs.dev).
22402183
1 | foo.split("|")␊
22412184
`
22422185

2243-
## invalid(14): [...Object.keys(foo)]
2186+
## invalid(11): [...Object.keys(foo)]
22442187

22452188
> Input
22462189
@@ -2259,7 +2202,7 @@ Generated by [AVA](https://avajs.dev).
22592202
1 | Object.keys(foo)␊
22602203
`
22612204

2262-
## invalid(15): [...Object.values(foo)]
2205+
## invalid(12): [...Object.values(foo)]
22632206

22642207
> Input
22652208
@@ -2278,7 +2221,7 @@ Generated by [AVA](https://avajs.dev).
22782221
1 | Object.values(foo)␊
22792222
`
22802223

2281-
## invalid(16): [...Array.from(foo)]
2224+
## invalid(13): [...Array.from(foo)]
22822225

22832226
> Input
22842227
@@ -2297,7 +2240,7 @@ Generated by [AVA](https://avajs.dev).
22972240
1 | Array.from(foo)␊
22982241
`
22992242

2300-
## invalid(17): [...Array.of()]
2243+
## invalid(14): [...Array.of()]
23012244

23022245
> Input
23032246
@@ -2316,7 +2259,7 @@ Generated by [AVA](https://avajs.dev).
23162259
1 | Array.of()␊
23172260
`
23182261

2319-
## invalid(18): [...new Array(3)]
2262+
## invalid(15): [...new Array(3)]
23202263

23212264
> Input
23222265
@@ -2332,7 +2275,7 @@ Generated by [AVA](https://avajs.dev).
23322275
| ^^^^^^^^^^^^^^^^^ Unnecessarily cloning an array.␊
23332276
`
23342277

2335-
## invalid(19): [...await Promise.all(foo)]
2278+
## invalid(16): [...await Promise.all(foo)]
23362279

23372280
> Input
23382281
@@ -2351,7 +2294,7 @@ Generated by [AVA](https://avajs.dev).
23512294
1 | await Promise.all(foo)␊
23522295
`
23532296

2354-
## invalid(20): [...await Promise.allSettled(foo)]
2297+
## invalid(17): [...await Promise.allSettled(foo)]
23552298

23562299
> Input
23572300
@@ -2370,7 +2313,7 @@ Generated by [AVA](https://avajs.dev).
23702313
1 | await Promise.allSettled(foo)␊
23712314
`
23722315

2373-
## invalid(21): function foo(bar) { return[...Object.keys(bar)]; }
2316+
## invalid(18): function foo(bar) { return[...Object.keys(bar)]; }
23742317

23752318
> Input
23762319
@@ -2395,7 +2338,7 @@ Generated by [AVA](https://avajs.dev).
23952338
3 | }␊
23962339
`
23972340

2398-
## invalid(22): function foo(bar) { return[ ...Object.keys(bar) ]; }
2341+
## invalid(19): function foo(bar) { return[ ...Object.keys(bar) ]; }
23992342

24002343
> Input
24012344
@@ -2428,7 +2371,7 @@ Generated by [AVA](https://avajs.dev).
24282371
5 | }␊
24292372
`
24302373

2431-
## invalid(23): function foo(bar) { return[ ...( Object.keys(bar) ) ]; }
2374+
## invalid(20): function foo(bar) { return[ ...( Object.keys(bar) ) ]; }
24322375

24332376
> Input
24342377
@@ -2469,7 +2412,7 @@ Generated by [AVA](https://avajs.dev).
24692412
7 | }␊
24702413
`
24712414

2472-
## invalid(24): function foo(bar) { return([ ...Object.keys(bar) ]); }
2415+
## invalid(21): function foo(bar) { return([ ...Object.keys(bar) ]); }
24732416

24742417
> Input
24752418
-74 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)