Skip to content

Commit

Permalink
Only suggest spread all when multiple arguments are unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 25, 2021
1 parent 5abcce7 commit dd451d3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 66 deletions.
41 changes: 21 additions & 20 deletions rules/prefer-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,32 +296,33 @@ const create = context => {
});
}

problem.suggest = [
...suggestions.map(({messageId, isSpreadable, testArgument}) => ({
messageId,
fix: fixConcat(
node,
sourceCode,
// When apply suggestion, we also merge fixable arguments after the first one
[
{
node: firstArgument,
isSpreadable,
testArgument
},
...fixableArgumentsAfterFirstArgument
]
)
})),
{
problem.suggest = suggestions.map(({messageId, isSpreadable, testArgument}) => ({
messageId,
fix: fixConcat(
node,
sourceCode,
// When apply suggestion, we also merge fixable arguments after the first one
[
{
node: firstArgument,
isSpreadable,
testArgument
},
...fixableArgumentsAfterFirstArgument
]
)
}));

if (fixableArgumentsAfterFirstArgument.length < restArguments.length) {
problem.suggest.push({
messageId: SUGGESTION_CONCAT_SPREAD_ALL_ARGUMENTS,
fix: fixConcat(
node,
sourceCode,
node.arguments.map(node => getConcatArgumentSpreadable(node, scope) || {node, isSpreadable: true})
)
}
];
});
}

context.report(problem);
}
Expand Down
64 changes: 18 additions & 46 deletions test/snapshots/prefer-spread.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -832,20 +832,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]␊
--------------------------------------------------------------------------------␊
Suggestion 2/4: First argument is not an `array`.␊
Suggestion 2/3: First argument is not an `array`.␊
1 | [...foo, bar]␊
--------------------------------------------------------------------------------␊
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])]␊
--------------------------------------------------------------------------------␊
Suggestion 4/4: Spread all unknown arguments`.␊
1 | [...foo, ...bar]␊
`

## Invalid #24
Expand Down Expand Up @@ -952,20 +948,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, 2, ...bar]␊
--------------------------------------------------------------------------------␊
Suggestion 2/4: First argument is not an `array`.␊
Suggestion 2/3: First argument is not an `array`.␊
1 | [...foo, 2, bar]␊
--------------------------------------------------------------------------------␊
Suggestion 3/4: Test first argument with `Array.isArray(…)`.␊
Suggestion 3/3: Test first argument with `Array.isArray(…)`.␊
1 | [...foo, 2, ...(Array.isArray(bar) ? bar : [bar])]␊
--------------------------------------------------------------------------------␊
Suggestion 4/4: Spread all unknown arguments`.␊
1 | [...foo, 2, ...bar]␊
`

## Invalid #30
Expand Down Expand Up @@ -994,20 +986,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]␊
--------------------------------------------------------------------------------␊
Suggestion 2/4: First argument is not an `array`.␊
Suggestion 2/3: First argument is not an `array`.␊
1 | [...foo, bar, 2, 3]␊
--------------------------------------------------------------------------------␊
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]␊
--------------------------------------------------------------------------------␊
Suggestion 4/4: Spread all unknown arguments`.␊
1 | [...foo, ...bar, 2, 3]␊
`

## Invalid #32
Expand Down Expand Up @@ -1046,16 +1034,12 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^ Prefer the spread operator over `Array#concat(…)`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/3: First argument is an `array`.␊
Suggestion 1/2: First argument is an `array`.␊
1 | async function a() {return [...(await bar)]}␊
--------------------------------------------------------------------------------␊
Suggestion 2/3: First argument is not an `array`.␊
Suggestion 2/2: First argument is not an `array`.␊
1 | async function a() {return [await bar]}␊
--------------------------------------------------------------------------------␊
Suggestion 3/3: Spread all unknown arguments`.␊
1 | async function a() {return [...(await bar)]}␊
`

## Invalid #34
Expand All @@ -1068,16 +1052,12 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^ Prefer the spread operator over `Array#concat(…)`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/3: First argument is an `array`.␊
Suggestion 1/2: First argument is an `array`.␊
1 | async function a() {return [...((await bar))]}␊
--------------------------------------------------------------------------------␊
Suggestion 2/3: First argument is not an `array`.␊
Suggestion 2/2: First argument is not an `array`.␊
1 | async function a() {return [((await bar))]}␊
--------------------------------------------------------------------------------␊
Suggestion 3/3: Spread all unknown arguments`.␊
1 | async function a() {return [...((await bar))]}␊
`

## Invalid #35
Expand Down Expand Up @@ -1317,20 +1297,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 | [...(a.b.c), 2]␊
--------------------------------------------------------------------------------␊
Suggestion 2/4: First argument is not an `array`.␊
Suggestion 2/3: First argument is not an `array`.␊
1 | [(a.b.c), 2]␊
--------------------------------------------------------------------------------␊
Suggestion 3/4: Test first argument with `Array.isArray(…)`.␊
Suggestion 3/3: Test first argument with `Array.isArray(…)`.␊
1 | [...(Array.isArray((a.b.c)) ? (a.b.c) : [(a.b.c)]), 2]␊
--------------------------------------------------------------------------------␊
Suggestion 4/4: Spread all unknown arguments`.␊
1 | [...(a.b.c), 2]␊
`

## Invalid #49
Expand All @@ -1343,14 +1319,10 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^ Prefer the spread operator over `Array#concat(…)`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/3: First argument is an `array`.␊
Suggestion 1/2: First argument is an `array`.␊
1 | [...a.b(), 2]␊
--------------------------------------------------------------------------------␊
Suggestion 2/3: First argument is not an `array`.␊
Suggestion 2/2: First argument is not an `array`.␊
1 | [a.b(), 2]␊
--------------------------------------------------------------------------------␊
Suggestion 3/3: Spread all unknown arguments`.␊
1 | [...a.b(), 2]␊
`
Binary file modified test/snapshots/prefer-spread.js.snap
Binary file not shown.

0 comments on commit dd451d3

Please sign in to comment.