Skip to content

Commit 6e6d3d8

Browse files
SamVerschuerensindresorhus
authored andcommitted
Do not auto-fix prefer-spread in certain cases - fixes #164 (#165)
1 parent f41abbb commit 6e6d3d8

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

rules/prefer-spread.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const isArrayFrom = node => {
1212
);
1313
};
1414

15+
const isArrayLike = arg => arg && arg.type !== 'ObjectExpression';
16+
1517
const parseArgument = (context, arg) => {
1618
if (arg.type === 'Identifier') {
1719
return arg.name;
@@ -23,7 +25,7 @@ const parseArgument = (context, arg) => {
2325
const create = context => {
2426
return {
2527
CallExpression(node) {
26-
if (isArrayFrom(node)) {
28+
if (isArrayFrom(node) && isArrayLike(node.arguments[0])) {
2729
context.report({
2830
node,
2931
message: 'Prefer the spread operator over `Array.from()`.',

test/prefer-spread.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ ruleTester.run('prefer-spread', rule, {
1919
'Int32Array.from(set);',
2020
'Uint32Array.from(set);',
2121
'Float32Array.from(set);',
22-
'Float64Array.from(set);'
22+
'Float64Array.from(set);',
23+
'Array.from()',
24+
'Array.from({length: 10})',
25+
'Array.from({length: 10}, mapFn)'
2326
],
2427
invalid: [
2528
{

0 commit comments

Comments
 (0)