Skip to content

Commit

Permalink
Fix "spread all suggestion"
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 25, 2021
1 parent dd451d3 commit bebddcf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion rules/prefer-spread.js
Expand Up @@ -313,7 +313,10 @@ const create = context => {
)
}));

if (fixableArgumentsAfterFirstArgument.length < restArguments.length) {
if (
fixableArgumentsAfterFirstArgument.length < restArguments.length &&
!restArguments.some(({type}) => type !== 'SpreadElement')
) {
problem.suggest.push({
messageId: SUGGESTION_CONCAT_SPREAD_ALL_ARGUMENTS,
fix: fixConcat(
Expand Down
3 changes: 2 additions & 1 deletion test/prefer-spread.js
Expand Up @@ -227,6 +227,7 @@ test.snapshot({
)
`,
'[].concat((a.b.c), 2)',
'[].concat(a.b(), 2)'
'[].concat(a.b(), 2)',
'foo.concat(bar, 2, 3, ...baz)'
]
});
32 changes: 25 additions & 7 deletions test/snapshots/prefer-spread.js.md
Expand Up @@ -1008,20 +1008,16 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^ Prefer the spread operator over `Array#concat(…)`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/4: First argument is an `array`.␊
Suggestion 1/3: First argument is an `array`.␊
1 | [...foo, ...bar, 2, 3].concat(baz)␊
--------------------------------------------------------------------------------␊
Suggestion 2/4: First argument is not an `array`.␊
Suggestion 2/3: First argument is not an `array`.␊
1 | [...foo, bar, 2, 3].concat(baz)␊
--------------------------------------------------------------------------------␊
Suggestion 3/4: Test first argument with `Array.isArray(…)`.␊
Suggestion 3/3: Test first argument with `Array.isArray(…)`.␊
1 | [...foo, ...(Array.isArray(bar) ? bar : [bar]), 2, 3].concat(baz)␊
--------------------------------------------------------------------------------␊
Suggestion 4/4: Spread all unknown arguments`.␊
1 | [...foo, ...bar, 2, 3, ...baz]␊
`

## Invalid #33
Expand Down Expand Up @@ -1326,3 +1322,25 @@ Generated by [AVA](https://avajs.dev).
Suggestion 2/2: First argument is not an `array`.␊
1 | [a.b(), 2]␊
`

## Invalid #50
1 | foo.concat(bar, 2, 3, ...baz)

> Error 1/1
`␊
> 1 | foo.concat(bar, 2, 3, ...baz)␊
| ^^^^^^ Prefer the spread operator over `Array#concat(…)`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/3: First argument is an `array`.␊
1 | [...foo, ...bar, 2, 3].concat(...baz)␊
--------------------------------------------------------------------------------␊
Suggestion 2/3: First argument is not an `array`.␊
1 | [...foo, bar, 2, 3].concat(...baz)␊
--------------------------------------------------------------------------------␊
Suggestion 3/3: Test first argument with `Array.isArray(…)`.␊
1 | [...foo, ...(Array.isArray(bar) ? bar : [bar]), 2, 3].concat(...baz)␊
`
Binary file modified test/snapshots/prefer-spread.js.snap
Binary file not shown.

0 comments on commit bebddcf

Please sign in to comment.