forked from sindresorhus/eslint-plugin-unicorn
/
no-useless-spread.mjs
326 lines (300 loc) · 7.17 KB
/
no-useless-spread.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import outdent from 'outdent';
import {getTester} from './utils/test.mjs';
const {test} = getTester(import.meta);
// Spread in list
test.snapshot({
valid: [
'const array = [[]]',
'const array = [{}]',
'const object = ({...[]})',
'foo([])',
'foo({})',
'new Foo([])',
'new Foo({})',
'const array = [...a]',
'const object = {...a}',
'const [first, ...rest] = []',
'const {foo, ...rest} = {}',
'function a(foo, ...rest) {}',
],
invalid: [
'const array = [...[a]]',
'const object = {...{a}}',
'foo(...[a])',
'new Foo(...[a])',
// Trailing comma
'const array = [...[a,]]',
'const object = {...{a,}}',
'foo(...[a,])',
'new Foo(...[a,])',
// Trailing comma in parent
'const array = [...[a,],]',
'const object = {...{a,},}',
'foo(...[a,],)',
'new Foo(...[a,],)',
// Parentheses
'const array = [...(( [a] ))]',
'const object = {...(( {a} ))}',
'foo(...(( [a] )))',
'new Foo(...(( [a] )))',
// Empty
'const array = [...[]]',
'const object = {...{}}',
'foo(...[])',
'new Foo(...[])',
// Hole(s)
'const array = [...[,]]',
'foo(...[,])',
'new Foo(...[,])',
'const array = [...[,,]]',
'foo(...[,,])',
'new Foo(...[,,])',
'const array = [...[a, , b,]]',
'foo(...[a, , b,])',
'new Foo(...[a, , b,])',
'const array = [...[a, , b,],]',
'foo(...[a, , b,],)',
'new Foo(...[a, , b,],)',
'foo(...[,, ,(( a )), ,,(0, b), ,,])',
// Extra elements/properties
'const array = [a, ...[a, b]]',
'const object = {a, ...{a, b}}',
'foo(a, ...[a, b])',
'new Foo(a, ...[a, b])',
'const array = [...[a, b], b,]',
'const object = {...{a, b}, b,}',
'foo(...[a, b], b,)',
'new Foo(...[a, b], b,)',
'const array = [a, ...[a, b], b,]',
'const object = {a, ...{a, b}, b,}',
'foo(a, ...[a, b], b,)',
'new Foo(a, ...[a, b], b,)',
// Duplicated keys
'({a:1, ...{a: 2}})',
'({...{a:1}, ...{a: 2}})',
outdent`
({
get a() {},
set a(v) {},
...{
get a() {}
}
})
`,
// Computed
'({[a]:1, ...{[a]: 2}})',
outdent`
const object = {
a: 1,
...{
testKeys() {
console.assert(Object.keys(this).length === 2)
}
}
}
object.testKeys();
`,
outdent`
new Foo(
foo(
a,
...[a, b],
b,
),
...[
a,
...[
a,
b,
],
b,
],
{
a: [...[a, b]],
...{
a,
b,
},
}
)
`,
// Code from example in `prefer-spread` rule docs
outdent`
const baz = [2];
call(foo, ...[bar, ...baz]);
`,
'Promise.all(...[...iterable])',
'new Map(...[...iterable])',
],
});
// Iterable to array
test.snapshot({
valid: [
'new NotMatchedConstructor([...iterable])',
'new foo.Map([...iterable])',
'new Map([...iterable], extraArgument)',
'new Map()',
'new Map(...iterable)',
'new Map([,...iterable])',
'new Map([...iterable, extraElement])',
'new Map({...iterable})',
'new Uint8Array(...iterable)',
'new Uint8Array(before, [...iterable])',
'Promise.notMatchedMethod([...iterable])',
'NotPromise.all([...iterable])',
'foo.Promise.all([...iterable])',
'Promise.all?.([...iterable])',
'Promise?.all([...iterable])',
'Promise[all]([...iterable])',
'Promise.all()',
'Promise.all([...iterable], extraArgument)',
'Promise.all(...iterable)',
'Promise.all([,...iterable])',
'Promise.all([...iterable, extraElement])',
'Promise.all({...iterable})',
'Object.notFromEntries([...iterable])',
'NotObject.fromEntries([...iterable])',
'Object.fromEntries?.([...iterable])',
'Object?.fromEntries([...iterable])',
'Object[fromEntries]([...iterable])',
'Object.fromEntries()',
'Object.fromEntries([...iterable], extraArgument)',
'Object.fromEntries(...iterable)',
'Object.fromEntries({...iterable})',
'Uint8Array.notFrom([...iterable])',
'NotTypedArray.from([...iterable])',
'Uint8Array.from?.([...iterable])',
'Uint8Array?.from([...iterable])',
'Uint8Array.from([...iterable], extraArgument)',
'Uint8Array.from(...iterable)',
'Uint8Array.from({...iterable})',
'for (const [...iterable] of foo);',
'for (const foo of bar) [...iterable];',
'for (const foo of [,...iterable]);',
'for (const foo of [...iterable, extraElement]);',
'for (const foo of {...iterable});',
'for (const foo in [...iterable]);',
'function * fn() {yield [...iterable];}',
'function * fn() {yield* [...iterable, extraElement];}',
'function * fn() {yield* {...iterable};}',
],
invalid: [
'const map = new Map([...iterable])',
'const weakMap = new WeakMap([...iterable])',
'const set = new Set([...iterable])',
'const weakSet = new WeakSet([...iterable])',
'const typed = new BigUint64Array([...iterable], byteOffset, length)',
'const typed = new BigUint64Array([...iterable], ...args)',
'const promise = Promise.all([...iterable])',
'const promise = Promise.allSettled([...iterable])',
'const promise = Promise.any([...iterable])',
'const promise = Promise.race([...iterable])',
'const array = Array.from([...iterable])',
'const typed = BigUint64Array.from([...iterable])',
'const object = Object.fromEntries([...iterable])',
'for (const foo of [...iterable]);',
'async () => {for await (const foo of [...iterable]);}',
// Trailing comma
'const map = new Map([...iterable,])',
'for (const foo of [...iterable]);',
'const map = new Map([...iterable,],)',
// Parentheses
'const map = new Map([...(( iterable ))])',
'for (const foo of [...(( iterable ))]);',
'const map = new Map((( [...(( iterable ))] )))',
'for (const foo of (( [...(( iterable ))] )));',
'for (const foo of[...iterable]);',
...[
'[...iterable]',
'[...iterable,]',
'(( [...iterable] ))',
'(( [...(( iterable ))] ))',
].map(code => outdent`
function * fn() {
yield * ${code};
}
`),
],
});
// Cloning an immediate array
test.snapshot({
valid: [
'[...not.array]',
'[...not.array()]',
'[...array.unknown()]',
'[...Object.notReturningArray(foo)]',
'[...NotObject.keys(foo)]',
'[...Int8Array.from(foo)]',
'[...Int8Array.of()]',
'[...new Int8Array(3)]',
'[...Promise.all(foo)]',
'[...Promise.allSettled(foo)]',
'[...await Promise.all(foo, extraArgument)]',
],
invalid: [
'[...foo.concat(bar)]',
'[...foo.copyWithin(-2)]',
'[...foo.filter(bar)]',
'[...foo.flat()]',
'[...foo.flatMap(bar)]',
'[...foo.map(bar)]',
'[...foo.slice(1)]',
'[...foo.splice(1)]',
'[...foo.split("|")]',
'[...Object.keys(foo)]',
'[...Object.values(foo)]',
'[...Array.from(foo)]',
'[...Array.of()]',
'[...new Array(3)]',
'[...await Promise.all(foo)]',
'[...await Promise.allSettled(foo)]',
outdent`
function foo(bar) {
return[...Object.keys(bar)];
}
`,
outdent`
function foo(bar) {
return[
...Object.keys(bar)
];
}
`,
outdent`
function foo(bar) {
return[
...(
Object.keys(bar)
)
];
}
`,
outdent`
function foo(bar) {
return([
...Object.keys(bar)
]);
}
`,
],
});
test.babel({
valid: [],
invalid: [
{
code: 'for (const foo of[...iterable]);',
output: 'for (const foo of iterable);',
errors: 1,
},
],
});
test.typescript({
valid: [],
invalid: [
{
code: 'for (const foo of[...iterable2]);',
output: 'for (const foo of iterable2);',
errors: 1,
},
],
});