Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/rules/prefer-array-from-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ ruleTester.run('prefer-array-from-map', preferArrayFromMap, {
'const result = [arr].map(x => x * 2)',

// Empty array
'const result = [].map(x => x * 2)'
'const result = [].map(x => x * 2)',

// Mapper can access the third `array` argument, which Array.from's
// mapper is never given
'const result = [...arr].map((x, i, all) => all[i - 1])',
'const result = [...arr].map(function(x, i, all) { return all[i - 1] })',
'const result = [...arr].map((...args) => args[2])',
'const result = [...arr].map((x, ...rest) => rest[1])'
],

invalid: [
Expand Down
19 changes: 18 additions & 1 deletion src/rules/prefer-array-from-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,29 @@ export const preferArrayFromMap: Rule.RuleModule = {
return;
}

const mapper = node.arguments[0]!;

// Array.from's mapper is called with (element, index) only, so a
// callback which can access the third `array` argument can't be
// converted
if (
mapper.type === 'FunctionExpression' ||
mapper.type === 'ArrowFunctionExpression'
) {
if (
mapper.params.length > 2 ||
mapper.params.some((param) => param.type === 'RestElement')
) {
return;
}
}

const spreadElement = arrayExpr.elements[0];
const iterableText = formatArguments(
[spreadElement.argument],
sourceCode
);
const mapperText = formatArguments([node.arguments[0]!], sourceCode);
const mapperText = formatArguments([mapper], sourceCode);

context.report({
node,
Expand Down