diff --git a/rules/no-array-for-each.js b/rules/no-array-for-each.js index a1f74fc9fc..c883662869 100644 --- a/rules/no-array-for-each.js +++ b/rules/no-array-for-each.js @@ -17,7 +17,7 @@ const {getParentheses} = require('./utils/parentheses.js'); const isFunctionSelfUsedInside = require('./utils/is-function-self-used-inside.js'); const {isNodeMatches} = require('./utils/is-node-matches.js'); const assertToken = require('./utils/assert-token.js'); -const {fixSpaceAroundKeyword} = require('./fix/index.js'); +const {fixSpaceAroundKeyword, removeParentheses} = require('./fix/index.js'); const MESSAGE_ID_ERROR = 'no-array-for-each/error'; const MESSAGE_ID_SUGGESTION = 'no-array-for-each/suggestion'; @@ -209,6 +209,9 @@ function getFixFunction(callExpression, functionInfo, context) { } return function * (fixer) { + // `(( foo.forEach(bar => bar) ))` + yield * removeParentheses(callExpression, fixer, sourceCode); + // Replace these with `for (const … of …) ` // foo.forEach(bar => bar) // ^^^^^^^^^^^^^^^^^^ (space after `=>` didn't included) @@ -323,14 +326,8 @@ function isFunctionParameterVariableReassigned(callbackFunction, context) { } function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context}) { - const sourceCode = context.getSourceCode(); // Check `CallExpression` - if ( - callExpression.optional - // TODO: Parenthesized should also be fixable. - || isParenthesized(callExpression, sourceCode) - || callExpression.arguments.length !== 1 - ) { + if (callExpression.optional || callExpression.arguments.length !== 1) { return false; } diff --git a/test/no-array-for-each.mjs b/test/no-array-for-each.mjs index 7a35962bf6..670ef4bdbd 100644 --- a/test/no-array-for-each.mjs +++ b/test/no-array-for-each.mjs @@ -21,8 +21,9 @@ test.snapshot({ invalid: [ 'foo.forEach?.(element => bar(element))', ...[ + '(( foo.forEach(element => bar(element)) ))', + // Not fixable - '(foo.forEach(element => bar(element)))', 'foo.forEach(element => bar(element), thisArgument)', 'foo.forEach()', 'const baz = foo.forEach(element => bar(element))', diff --git a/test/snapshots/no-array-for-each.mjs.md b/test/snapshots/no-array-for-each.mjs.md index 667ab195a2..f7c206ec06 100644 --- a/test/snapshots/no-array-for-each.mjs.md +++ b/test/snapshots/no-array-for-each.mjs.md @@ -15,23 +15,35 @@ Generated by [AVA](https://avajs.dev). ` ## Invalid #2 - 1 | (foo.forEach(element => bar(element))) + 1 | (( foo.forEach(element => bar(element)) )) + +> Output + + `␊ + 1 | for (const element of foo) bar(element) ␊ + ` > Error 1/1 `␊ - > 1 | (foo.forEach(element => bar(element)))␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | (( foo.forEach(element => bar(element)) ))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` ## Invalid #3 - 1 | (foo?.forEach(element => bar(element))) + 1 | (( foo?.forEach(element => bar(element)) )) + +> Output + + `␊ + 1 | if (foo) for (const element of foo) bar(element) ␊ + ` > Error 1/1 `␊ - > 1 | (foo?.forEach(element => bar(element)))␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | (( foo?.forEach(element => bar(element)) ))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` ## Invalid #4 diff --git a/test/snapshots/no-array-for-each.mjs.snap b/test/snapshots/no-array-for-each.mjs.snap index 65c89fabc4..f953853662 100644 Binary files a/test/snapshots/no-array-for-each.mjs.snap and b/test/snapshots/no-array-for-each.mjs.snap differ