Skip to content

Commit

Permalink
prefer-spread: Fix it to not report on optional chaining (#2304)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBlm authored Apr 3, 2024
1 parent 907a3f7 commit df1ff1c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
6 changes: 4 additions & 2 deletions rules/prefer-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
needsSemicolon,
isNodeMatches,
isMethodNamed,
hasOptionalChainElement,
} = require('./utils/index.js');
const {removeMethodCall} = require('./fix/index.js');
const {isLiteral, isMethodCall} = require('./ast/index.js');
Expand Down Expand Up @@ -403,7 +404,8 @@ const create = context => {
optionalCall: false,
optionalMember: false,
})
&& node.callee.object.type !== 'ArrayExpression'
&& !isArrayLiteral(node.callee.object)
&& !hasOptionalChainElement(node.callee.object)
)) {
return;
}
Expand Down Expand Up @@ -476,7 +478,7 @@ const create = context => {
const resultBySpread = [...value];

hasSameResult = resultBySplit.length === resultBySpread.length
&& resultBySplit.every((character, index) => character === resultBySpread[index]);
&& resultBySplit.every((character, index) => character === resultBySpread[index]);
}

const problem = {
Expand Down
2 changes: 2 additions & 0 deletions test/prefer-spread.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ test.snapshot({
'buffer.slice()',
'file.slice()',
'class A {foo() {this.slice()}}',
'scopeManager?.scopes.slice()',
],
invalid: [
'array.slice()',
Expand All @@ -320,6 +321,7 @@ test.snapshot({
'array.slice().slice(1)',
'const copy = array.slice()',
'(( (( (( array )).slice ))() ))',
'(scopeManager?.scopes).slice()',
// Semicolon
outdent`
bar()
Expand Down
35 changes: 28 additions & 7 deletions test/snapshots/prefer-spread.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,28 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
`

## invalid(7): bar() foo.slice()
## invalid(7): (scopeManager?.scopes).slice()

> Input
`␊
1 | (scopeManager?.scopes).slice()␊
`

> Output
`␊
1 | [...(scopeManager?.scopes)]␊
`

> Error 1/1
`␊
> 1 | (scopeManager?.scopes).slice()␊
| ^^^^^ Prefer the spread operator over \`Array#slice()\`.␊
`

## invalid(8): bar() foo.slice()

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

## invalid(8): "".slice()
## invalid(9): "".slice()

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

## invalid(9): new Uint8Array([10, 20, 30, 40, 50]).slice()
## invalid(10): new Uint8Array([10, 20, 30, 40, 50]).slice()

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

## invalid(10): array.slice(0)
## invalid(11): array.slice(0)

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

## invalid(11): array.slice(0b0)
## invalid(12): array.slice(0b0)

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

## invalid(12): array.slice(0.00)
## invalid(13): array.slice(0.00)

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

## invalid(13): array.slice(0.00, )
## invalid(14): array.slice(0.00, )

> Input
Expand Down
Binary file modified test/snapshots/prefer-spread.mjs.snap
Binary file not shown.

0 comments on commit df1ff1c

Please sign in to comment.