Skip to content

Commit

Permalink
no-array-callback-reference: Improve suggestions for `Array#forEach…
Browse files Browse the repository at this point in the history
…()` (#1049)
  • Loading branch information
fisker committed Jan 22, 2021
1 parent 4271e95 commit 77bcdc6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
13 changes: 10 additions & 3 deletions rules/no-array-callback-reference.js
Expand Up @@ -25,7 +25,11 @@ const iteratorMethods = [
['find'],
['findIndex'],
['flatMap'],
['forEach'],
[
'forEach', {
returnsUndefined: true
}
],
['map'],
[
'reduce', {
Expand Down Expand Up @@ -58,6 +62,7 @@ const iteratorMethods = [
ignore: ['Boolean'],
minParameters: 1,
extraSelector: '',
returnsUndefined: false,
...options
};
return [method, options];
Expand Down Expand Up @@ -105,7 +110,7 @@ function check(context, node, method, options) {
suggest: []
};

const {parameters, minParameters} = options;
const {parameters, minParameters, returnsUndefined} = options;
for (let parameterLength = minParameters; parameterLength <= parameters.length; parameterLength++) {
const suggestionParameters = parameters.slice(0, parameterLength).join(', ');

Expand All @@ -124,7 +129,9 @@ function check(context, node, method, options) {

return fixer.replaceText(
node,
`(${suggestionParameters}) => ${nodeText}(${suggestionParameters})`
returnsUndefined ?
`(${suggestionParameters}) => { ${nodeText}(${suggestionParameters}); }` :
`(${suggestionParameters}) => ${nodeText}(${suggestionParameters})`
);
}
};
Expand Down
28 changes: 25 additions & 3 deletions test/no-array-callback-reference.js
Expand Up @@ -17,6 +17,8 @@ const simpleMethods = [
'map'
];

const simpleMethodsExceptForEach = simpleMethods.filter(name => name !== 'forEach');

const reduceLikeMethods = [
'reduce',
'reduceRight'
Expand Down Expand Up @@ -125,7 +127,7 @@ ruleTester.run('no-array-callback-reference', rule, {
],
invalid: [
// Suggestions
...simpleMethods.map(
...simpleMethodsExceptForEach.map(
method => invalidTestCase({
code: `foo.${method}(fn)`,
method,
Expand All @@ -137,6 +139,16 @@ ruleTester.run('no-array-callback-reference', rule, {
]
})
),
invalidTestCase({
code: 'foo.forEach(fn)',
method: 'forEach',
name: 'fn',
suggestions: [
'foo.forEach((element) => { fn(element); })',
'foo.forEach((element, index) => { fn(element, index); })',
'foo.forEach((element, index, array) => { fn(element, index, array); })'
]
}),
...reduceLikeMethods.map(
method => invalidTestCase({
code: `foo.${method}(fn)`,
Expand All @@ -151,7 +163,7 @@ ruleTester.run('no-array-callback-reference', rule, {
),

// 2 arguments
...simpleMethods.map(
...simpleMethodsExceptForEach.map(
method => invalidTestCase({
code: `foo.${method}(fn, thisArgument)`,
method,
Expand All @@ -163,6 +175,16 @@ ruleTester.run('no-array-callback-reference', rule, {
]
})
),
invalidTestCase({
code: 'foo.forEach(fn, thisArgument)',
method: 'forEach',
name: 'fn',
suggestions: [
'foo.forEach((element) => { fn(element); }, thisArgument)',
'foo.forEach((element, index) => { fn(element, index); }, thisArgument)',
'foo.forEach((element, index, array) => { fn(element, index, array); }, thisArgument)'
]
}),
...reduceLikeMethods.map(
method => invalidTestCase({
code: `foo.${method}(fn, initialValue)`,
Expand Down Expand Up @@ -191,7 +213,7 @@ ruleTester.run('no-array-callback-reference', rule, {
),

// Not `Identifier`
...simpleMethods.map(
...simpleMethodsExceptForEach.map(
method => invalidTestCase({
code: `foo.${method}(lib.fn)`,
method,
Expand Down

0 comments on commit 77bcdc6

Please sign in to comment.