Skip to content

Commit

Permalink
prefer-spread: Stop checking Array.from call with map function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 8, 2023
1 parent 8d6d007 commit f5beccb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 347 deletions.
26 changes: 3 additions & 23 deletions rules/prefer-spread.js
Expand Up @@ -5,11 +5,7 @@ const needsSemicolon = require('./utils/needs-semicolon.js');
const {getParenthesizedRange, getParenthesizedText} = require('./utils/parentheses.js');
const shouldAddParenthesesToSpreadElementArgument = require('./utils/should-add-parentheses-to-spread-element-argument.js');
const {isNodeMatches} = require('./utils/is-node-matches.js');
const {
replaceNodeOrTokenAndSpacesBefore,
removeSpacesAfter,
removeMethodCall,
} = require('./fix/index.js');
const {removeMethodCall} = require('./fix/index.js');
const {isLiteral} = require('./ast/index.js');
const isMethodNamed = require('./utils/is-method-named.js');

Expand Down Expand Up @@ -40,8 +36,7 @@ const arrayFromCallSelector = [
methodCallSelector({
object: 'Array',
method: 'from',
minimumArguments: 1,
maximumArguments: 3,
argumentsLength: 1,
}),
// Allow `Array.from({length})`
'[arguments.0.type!="ObjectExpression"]',
Expand Down Expand Up @@ -266,13 +261,6 @@ function fixArrayFrom(node, sourceCode) {
return `[...${text}]`;
}

function * removeObject(fixer) {
yield * replaceNodeOrTokenAndSpacesBefore(object, '', fixer, sourceCode);
const commaToken = sourceCode.getTokenAfter(object, isCommaToken);
yield * replaceNodeOrTokenAndSpacesBefore(commaToken, '', fixer, sourceCode);
yield removeSpacesAfter(commaToken, sourceCode, fixer);
}

return function * (fixer) {
// Fixed code always starts with `[`
if (needsSemicolon(sourceCode.getTokenBefore(node), sourceCode, '[')) {
Expand All @@ -281,15 +269,7 @@ function fixArrayFrom(node, sourceCode) {

const objectText = getObjectText();

if (node.arguments.length === 1) {
yield fixer.replaceText(node, objectText);
return;
}

// `Array.from(object, mapFunction, thisArgument)` -> `[...object].map(mapFunction, thisArgument)`
yield fixer.replaceText(node.callee.object, objectText);
yield fixer.replaceText(node.callee.property, 'map');
yield * removeObject(fixer);
yield fixer.replaceText(node, objectText);
};
}

Expand Down
22 changes: 4 additions & 18 deletions test/prefer-spread.mjs
Expand Up @@ -39,15 +39,15 @@ test.snapshot({
'Array.from();',
'Array.from(foo, mapFn, thisArg, extra);',
'Array.from(...argumentsArray);',
'Array.from(set, mapFn).reduce(() => {});',
'Array.from(set, mapFn, thisArg).reduce(() => {});',
'Array.from(set, () => {}, thisArg).reduce(() => {});',
// FirstArgument is `ObjectExpression`
'Array.from({length: 10});',
],
invalid: [
'const x = Array.from(set);',
'Array.from(set).map(() => {});',
'Array.from(set, mapFn).reduce(() => {});',
'Array.from(set, mapFn, thisArg).reduce(() => {});',
'Array.from(set, () => {}, thisArg).reduce(() => {});',
'Array.from(new Set([1, 2])).map(() => {});',
'Array.from(document.querySelectorAll("*")).map(() => {});',

Expand Down Expand Up @@ -140,24 +140,10 @@ test.snapshot({
'(Array).from((0, foo))',
'(Array.from)((0, foo))',
'((Array).from)((0, foo))',
'(Array).from(foo, bar)',
'(Array.from)(foo, bar)',
'((Array).from)(foo, bar)',
'(Array).from((0, foo), bar)',
'(Array.from)((0, foo), bar)',
'((Array).from)((0, foo), bar)',
'(Array).from(foo, bar, baz)',
'(Array.from)(foo, bar, baz)',
'((Array).from)(foo, bar, baz)',
'(Array).from((0, foo), bar, baz)',
'(Array.from)((0, foo), bar, baz)',
'((Array).from)((0, foo), bar, baz)',
'Array.from(a, (0, bar), (0, baz),)',
'Array.from(a ? b : c)',
'Array.from([...a, ...b], b, c)',
'Array.from([...a, ...b], )',
'Array.from([1])',
'Array.from([...a, ...b])',
'/* 1 */ Array /* 2 */ .from /* 3 */ ( /* 4 */ a /* 5 */, /* 6 */ b /* 7 */, /* 8 */ c /* 9 */,)',
'/* 1 */ Array /* 2 */ .from /* 3 */ ( /* 4 */ a /* 5 */,)',
],
});
Expand Down

0 comments on commit f5beccb

Please sign in to comment.