Skip to content

Commit df1ff1c

Browse files
authored
prefer-spread: Fix it to not report on optional chaining (#2304)
1 parent 907a3f7 commit df1ff1c

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

rules/prefer-spread.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
needsSemicolon,
77
isNodeMatches,
88
isMethodNamed,
9+
hasOptionalChainElement,
910
} = require('./utils/index.js');
1011
const {removeMethodCall} = require('./fix/index.js');
1112
const {isLiteral, isMethodCall} = require('./ast/index.js');
@@ -403,7 +404,8 @@ const create = context => {
403404
optionalCall: false,
404405
optionalMember: false,
405406
})
406-
&& node.callee.object.type !== 'ArrayExpression'
407+
&& !isArrayLiteral(node.callee.object)
408+
&& !hasOptionalChainElement(node.callee.object)
407409
)) {
408410
return;
409411
}
@@ -476,7 +478,7 @@ const create = context => {
476478
const resultBySpread = [...value];
477479

478480
hasSameResult = resultBySplit.length === resultBySpread.length
479-
&& resultBySplit.every((character, index) => character === resultBySpread[index]);
481+
&& resultBySplit.every((character, index) => character === resultBySpread[index]);
480482
}
481483

482484
const problem = {

test/prefer-spread.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ test.snapshot({
312312
'buffer.slice()',
313313
'file.slice()',
314314
'class A {foo() {this.slice()}}',
315+
'scopeManager?.scopes.slice()',
315316
],
316317
invalid: [
317318
'array.slice()',
@@ -320,6 +321,7 @@ test.snapshot({
320321
'array.slice().slice(1)',
321322
'const copy = array.slice()',
322323
'(( (( (( array )).slice ))() ))',
324+
'(scopeManager?.scopes).slice()',
323325
// Semicolon
324326
outdent`
325327
bar()

test/snapshots/prefer-spread.mjs.md

+28-7
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,28 @@ Generated by [AVA](https://avajs.dev).
24642464
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
24652465
`
24662466

2467-
## invalid(7): bar() foo.slice()
2467+
## invalid(7): (scopeManager?.scopes).slice()
2468+
2469+
> Input
2470+
2471+
`␊
2472+
1 | (scopeManager?.scopes).slice()␊
2473+
`
2474+
2475+
> Output
2476+
2477+
`␊
2478+
1 | [...(scopeManager?.scopes)]␊
2479+
`
2480+
2481+
> Error 1/1
2482+
2483+
`␊
2484+
> 1 | (scopeManager?.scopes).slice()␊
2485+
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
2486+
`
2487+
2488+
## invalid(8): bar() foo.slice()
24682489

24692490
> Input
24702491
@@ -2488,7 +2509,7 @@ Generated by [AVA](https://avajs.dev).
24882509
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
24892510
`
24902511

2491-
## invalid(8): "".slice()
2512+
## invalid(9): "".slice()
24922513

24932514
> Input
24942515
@@ -2509,7 +2530,7 @@ Generated by [AVA](https://avajs.dev).
25092530
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
25102531
`
25112532

2512-
## invalid(9): new Uint8Array([10, 20, 30, 40, 50]).slice()
2533+
## invalid(10): new Uint8Array([10, 20, 30, 40, 50]).slice()
25132534

25142535
> Input
25152536
@@ -2530,7 +2551,7 @@ Generated by [AVA](https://avajs.dev).
25302551
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
25312552
`
25322553

2533-
## invalid(10): array.slice(0)
2554+
## invalid(11): array.slice(0)
25342555

25352556
> Input
25362557
@@ -2551,7 +2572,7 @@ Generated by [AVA](https://avajs.dev).
25512572
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
25522573
`
25532574

2554-
## invalid(11): array.slice(0b0)
2575+
## invalid(12): array.slice(0b0)
25552576

25562577
> Input
25572578
@@ -2572,7 +2593,7 @@ Generated by [AVA](https://avajs.dev).
25722593
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
25732594
`
25742595

2575-
## invalid(12): array.slice(0.00)
2596+
## invalid(13): array.slice(0.00)
25762597

25772598
> Input
25782599
@@ -2593,7 +2614,7 @@ Generated by [AVA](https://avajs.dev).
25932614
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
25942615
`
25952616

2596-
## invalid(13): array.slice(0.00, )
2617+
## invalid(14): array.slice(0.00, )
25972618

25982619
> Input
25992620

test/snapshots/prefer-spread.mjs.snap

53 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)