Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance no-array-for-each to handle optional chaining #1753

Merged
merged 27 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/rules/no-array-for-each.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ array.forEach(element => {
});
```

```js
array?.forEach(element => {
bar(element);
});
```

```js
array.forEach((element, index) => {
bar(element, index);
Expand Down
16 changes: 7 additions & 9 deletions rules/no-array-for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function getFixFunction(callExpression, functionInfo, context) {
const parameters = callback.params;
const array = callExpression.callee.object;
const {returnStatements} = functionInfo.get(callback);
const isOptionalChaining = callExpression.callee.optional;

const getForOfLoopHeadText = () => {
const [elementText, indexText] = parameters.map(parameter => sourceCode.getText(parameter));
Expand Down Expand Up @@ -228,7 +229,7 @@ function getFixFunction(callExpression, functionInfo, context) {
yield * replaceReturnStatement(returnStatement, fixer);
}

const expressionStatementLastToken = sourceCode.getLastToken(callExpression.parent);
const expressionStatementLastToken = sourceCode.getLastToken(isOptionalChaining ? callExpression.parent.parent : callExpression.parent);
// Remove semicolon if it's not needed anymore
// foo.forEach(bar => {});
// ^
Expand All @@ -238,6 +239,10 @@ function getFixFunction(callExpression, functionInfo, context) {

yield * fixSpaceAroundKeyword(fixer, callExpression.parent, sourceCode);

if (isOptionalChaining) {
yield fixer.insertTextBefore(callExpression, `if (${callExpression.callee.object.name}) `);
}

// Prevent possible variable conflicts
yield * extendFixRange(fixer, callExpression.parent.range);
};
Expand Down Expand Up @@ -314,14 +319,7 @@ function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context
}

// Check `CallExpression.parent`
if (callExpression.parent.type !== 'ExpressionStatement') {
return false;
}

// Check `CallExpression.callee`
// Because of `ChainExpression` wrapper, `foo?.forEach()` is already failed on previous check keep this just for safety
/* c8 ignore next 3 */
if (callExpression.callee.optional) {
if (callExpression.parent.type !== 'ExpressionStatement' && callExpression.parent.type !== 'ChainExpression') {
fisker marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

Expand Down
95 changes: 94 additions & 1 deletion test/no-array-for-each.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ test.snapshot({
'foo.forEach(element => bar(element), thisArgument)',
'foo.forEach()',
'const baz = foo.forEach(element => bar(element))',
'foo?.forEach(element => bar(element))',
'foo.forEach(bar)',
'foo.forEach(async function(element) {})',
'foo.forEach(function * (element) {})',
Expand Down Expand Up @@ -439,6 +438,100 @@ test({
sourceType: 'script',
},
},
{
code: outdent`
foo?.forEach(function(element) {
delete element;
console.log(element)
});
`,
output: outdent`
if (foo) for (const element of foo) {
delete element;
console.log(element)
}
`,
errors: 1,
parserOptions: {
sourceType: 'script',
},
},
{
code: outdent`
foo?.forEach(element => {
delete element;
console.log(element)
});
`,
output: outdent`
if (foo) for (const element of foo) {
delete element;
console.log(element)
}
`,
errors: 1,
parserOptions: {
sourceType: 'script',
},
},
{
code: outdent`
foo?.forEach(element => console.log(element));
`,
output: outdent`
if (foo) for (const element of foo) console.log(element);
`,
errors: 1,
parserOptions: {
sourceType: 'script',
},
},
{
code: outdent`
foo?.forEach((element) => console.log(element));
`,
output: outdent`
if (foo) for (const element of foo) console.log(element);
`,
errors: 1,
parserOptions: {
sourceType: 'script',
},
},
{
code: outdent`
foo?.forEach(element => { console.log(element) });
`,
output: outdent`
if (foo) for (const element of foo) { console.log(element) }
`,
errors: 1,
parserOptions: {
sourceType: 'script',
},
},
{
code: outdent`
function a() {
foo?.forEach(function(element) {
delete element;
console.log(element)
});
}
`,
output: outdent`
function a() {
if (foo) for (const element of foo) {
delete element;
console.log(element)
}
}
`,
errors: 1,
parserOptions: {
sourceType: 'script',
},
},
{
code: 'foo.forEach(function(element, element) {})',
errors: 1,
Expand Down