diff --git a/rules/prefer-spread.js b/rules/prefer-spread.js index 7542a07125..d20eb043b2 100644 --- a/rules/prefer-spread.js +++ b/rules/prefer-spread.js @@ -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( diff --git a/test/prefer-spread.js b/test/prefer-spread.js index 5d065158f0..0b7883d4d9 100644 --- a/test/prefer-spread.js +++ b/test/prefer-spread.js @@ -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)' ] }); diff --git a/test/snapshots/prefer-spread.js.md b/test/snapshots/prefer-spread.js.md index 720f171a20..04ffb6e979 100644 --- a/test/snapshots/prefer-spread.js.md +++ b/test/snapshots/prefer-spread.js.md @@ -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 @@ -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)␊ + ` diff --git a/test/snapshots/prefer-spread.js.snap b/test/snapshots/prefer-spread.js.snap index 36a7f7fe3f..0ba03cf461 100644 Binary files a/test/snapshots/prefer-spread.js.snap and b/test/snapshots/prefer-spread.js.snap differ