Skip to content

Commit 7aeabab

Browse files
voderlfisker
andauthored
no-useless-spread: Safely remove empty array and object (#2764)
Co-authored-by: fisker <[email protected]>
1 parent 098fc34 commit 7aeabab

File tree

4 files changed

+131
-9
lines changed

4 files changed

+131
-9
lines changed

rules/no-useless-spread.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ const create = context => {
176176
yield fixer.remove(penultimateToken);
177177
}
178178

179+
// `[...[], 1]`
180+
// ^
181+
if (
182+
(node.type === 'ArrayExpression' && node.elements.length === 0)
183+
|| (node.type === 'ObjectExpression' && node.properties.length === 0)
184+
) {
185+
const nextToken = sourceCode.getTokenAfter(spreadElement);
186+
if (isCommaToken(nextToken)) {
187+
yield fixer.remove(nextToken);
188+
}
189+
}
190+
179191
if (parentType !== 'CallExpression' && parentType !== 'NewExpression') {
180192
return;
181193
}

test/no-useless-spread.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ test.snapshot({
7777
'const object = {a, ...{a, b}, b,}',
7878
'foo(a, ...[a, b], b,)',
7979
'new Foo(a, ...[a, b], b,)',
80+
'const array = [a, ...[], b]',
81+
'const array = [a, ...(( [] )),]',
82+
'const array = [a, ...(( [] ))]',
83+
'const array = [a, ...[b], c]',
84+
'const object = {a, ...(({})), b,}',
8085

8186
// Duplicated keys
8287
'({a:1, ...{a: 2}})',

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

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,112 @@ Generated by [AVA](https://avajs.dev).
949949
| ^^^ Spread an array literal in arguments is unnecessary.␊
950950
`
951951

952-
## invalid(46): ({a:1, ...{a: 2}})
952+
## invalid(46): const array = [a, ...[], b]
953+
954+
> Input
955+
956+
`␊
957+
1 | const array = [a, ...[], b]␊
958+
`
959+
960+
> Output
961+
962+
`␊
963+
1 | const array = [a, b]␊
964+
`
965+
966+
> Error 1/1
967+
968+
`␊
969+
> 1 | const array = [a, ...[], b]␊
970+
| ^^^ Spread an array literal in array literal is unnecessary.␊
971+
`
972+
973+
## invalid(47): const array = [a, ...(( [] )),]
974+
975+
> Input
976+
977+
`␊
978+
1 | const array = [a, ...(( [] )),]␊
979+
`
980+
981+
> Output
982+
983+
`␊
984+
1 | const array = [a, ]␊
985+
`
986+
987+
> Error 1/1
988+
989+
`␊
990+
> 1 | const array = [a, ...(( [] )),]␊
991+
| ^^^ Spread an array literal in array literal is unnecessary.␊
992+
`
993+
994+
## invalid(48): const array = [a, ...(( [] ))]
995+
996+
> Input
997+
998+
`␊
999+
1 | const array = [a, ...(( [] ))]␊
1000+
`
1001+
1002+
> Output
1003+
1004+
`␊
1005+
1 | const array = [a, ]␊
1006+
`
1007+
1008+
> Error 1/1
1009+
1010+
`␊
1011+
> 1 | const array = [a, ...(( [] ))]␊
1012+
| ^^^ Spread an array literal in array literal is unnecessary.␊
1013+
`
1014+
1015+
## invalid(49): const array = [a, ...[b], c]
1016+
1017+
> Input
1018+
1019+
`␊
1020+
1 | const array = [a, ...[b], c]␊
1021+
`
1022+
1023+
> Output
1024+
1025+
`␊
1026+
1 | const array = [a, b, c]␊
1027+
`
1028+
1029+
> Error 1/1
1030+
1031+
`␊
1032+
> 1 | const array = [a, ...[b], c]␊
1033+
| ^^^ Spread an array literal in array literal is unnecessary.␊
1034+
`
1035+
1036+
## invalid(50): const object = {a, ...(({})), b,}
1037+
1038+
> Input
1039+
1040+
`␊
1041+
1 | const object = {a, ...(({})), b,}␊
1042+
`
1043+
1044+
> Output
1045+
1046+
`␊
1047+
1 | const object = {a, b,}␊
1048+
`
1049+
1050+
> Error 1/1
1051+
1052+
`␊
1053+
> 1 | const object = {a, ...(({})), b,}␊
1054+
| ^^^ Spread an object literal in object literal is unnecessary.␊
1055+
`
1056+
1057+
## invalid(51): ({a:1, ...{a: 2}})
9531058

9541059
> Input
9551060
@@ -970,7 +1075,7 @@ Generated by [AVA](https://avajs.dev).
9701075
| ^^^ Spread an object literal in object literal is unnecessary.␊
9711076
`
9721077

973-
## invalid(47): ({...{a:1}, ...{a: 2}})
1078+
## invalid(52): ({...{a:1}, ...{a: 2}})
9741079

9751080
> Input
9761081
@@ -998,7 +1103,7 @@ Generated by [AVA](https://avajs.dev).
9981103
| ^^^ Spread an object literal in object literal is unnecessary.␊
9991104
`
10001105

1001-
## invalid(48): ({ get a() {}, set a(v) {}, ...{ get a() {} } })
1106+
## invalid(53): ({ get a() {}, set a(v) {}, ...{ get a() {} } })
10021107

10031108
> Input
10041109
@@ -1037,7 +1142,7 @@ Generated by [AVA](https://avajs.dev).
10371142
7 | })␊
10381143
`
10391144

1040-
## invalid(49): ({[a]:1, ...{[a]: 2}})
1145+
## invalid(54): ({[a]:1, ...{[a]: 2}})
10411146

10421147
> Input
10431148
@@ -1058,7 +1163,7 @@ Generated by [AVA](https://avajs.dev).
10581163
| ^^^ Spread an object literal in object literal is unnecessary.␊
10591164
`
10601165

1061-
## invalid(50): const object = { a: 1, ...{ testKeys() { console.assert(Object.keys(this).length === 2) } } } object.testKeys();
1166+
## invalid(55): const object = { a: 1, ...{ testKeys() { console.assert(Object.keys(this).length === 2) } } } object.testKeys();
10621167

10631168
> Input
10641169
@@ -1106,7 +1211,7 @@ Generated by [AVA](https://avajs.dev).
11061211
10 | object.testKeys();␊
11071212
`
11081213

1109-
## invalid(51): new Foo( foo( a, ...[a, b], b, ), ...[ a, ...[ a, b, ], b, ], { a: [...[a, b]], ...{ a, b, }, } )
1214+
## invalid(56): new Foo( foo( a, ...[a, b], b, ), ...[ a, ...[ a, b, ], b, ], { a: [...[a, b]], ...{ a, b, }, } )
11101215

11111216
> Input
11121217
@@ -1302,7 +1407,7 @@ Generated by [AVA](https://avajs.dev).
13021407
22 | )␊
13031408
`
13041409

1305-
## invalid(52): const baz = [2]; call(foo, ...[bar, ...baz]);
1410+
## invalid(57): const baz = [2]; call(foo, ...[bar, ...baz]);
13061411

13071412
> Input
13081413
@@ -1326,7 +1431,7 @@ Generated by [AVA](https://avajs.dev).
13261431
| ^^^ Spread an array literal in arguments is unnecessary.␊
13271432
`
13281433

1329-
## invalid(53): Promise.all(...[...iterable])
1434+
## invalid(58): Promise.all(...[...iterable])
13301435

13311436
> Input
13321437
@@ -1347,7 +1452,7 @@ Generated by [AVA](https://avajs.dev).
13471452
| ^^^ Spread an array literal in arguments is unnecessary.␊
13481453
`
13491454

1350-
## invalid(54): new Map(...[...iterable])
1455+
## invalid(59): new Map(...[...iterable])
13511456

13521457
> Input
13531458
164 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)