Skip to content

Commit 5f39c37

Browse files
authored
no-array-for-each: Add fix for parenthesized call (#1784)
1 parent 51166f4 commit 5f39c37

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

rules/no-array-for-each.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const {getParentheses} = require('./utils/parentheses.js');
1717
const isFunctionSelfUsedInside = require('./utils/is-function-self-used-inside.js');
1818
const {isNodeMatches} = require('./utils/is-node-matches.js');
1919
const assertToken = require('./utils/assert-token.js');
20-
const {fixSpaceAroundKeyword} = require('./fix/index.js');
20+
const {fixSpaceAroundKeyword, removeParentheses} = require('./fix/index.js');
2121

2222
const MESSAGE_ID_ERROR = 'no-array-for-each/error';
2323
const MESSAGE_ID_SUGGESTION = 'no-array-for-each/suggestion';
@@ -209,6 +209,9 @@ function getFixFunction(callExpression, functionInfo, context) {
209209
}
210210

211211
return function * (fixer) {
212+
// `(( foo.forEach(bar => bar) ))`
213+
yield * removeParentheses(callExpression, fixer, sourceCode);
214+
212215
// Replace these with `for (const … of …) `
213216
// foo.forEach(bar => bar)
214217
// ^^^^^^^^^^^^^^^^^^ (space after `=>` didn't included)
@@ -323,14 +326,8 @@ function isFunctionParameterVariableReassigned(callbackFunction, context) {
323326
}
324327

325328
function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context}) {
326-
const sourceCode = context.getSourceCode();
327329
// Check `CallExpression`
328-
if (
329-
callExpression.optional
330-
// TODO: Parenthesized should also be fixable.
331-
|| isParenthesized(callExpression, sourceCode)
332-
|| callExpression.arguments.length !== 1
333-
) {
330+
if (callExpression.optional || callExpression.arguments.length !== 1) {
334331
return false;
335332
}
336333

test/no-array-for-each.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ test.snapshot({
2121
invalid: [
2222
'foo.forEach?.(element => bar(element))',
2323
...[
24+
'(( foo.forEach(element => bar(element)) ))',
25+
2426
// Not fixable
25-
'(foo.forEach(element => bar(element)))',
2627
'foo.forEach(element => bar(element), thisArgument)',
2728
'foo.forEach()',
2829
'const baz = foo.forEach(element => bar(element))',

test/snapshots/no-array-for-each.mjs.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,35 @@ Generated by [AVA](https://avajs.dev).
1515
`
1616

1717
## Invalid #2
18-
1 | (foo.forEach(element => bar(element)))
18+
1 | (( foo.forEach(element => bar(element)) ))
19+
20+
> Output
21+
22+
`␊
23+
1 | for (const element of foo) bar(element) ␊
24+
`
1925

2026
> Error 1/1
2127
2228
`␊
23-
> 1 | (foo.forEach(element => bar(element)))␊
24-
| ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊
29+
> 1 | (( foo.forEach(element => bar(element)) ))␊
30+
| ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊
2531
`
2632

2733
## Invalid #3
28-
1 | (foo?.forEach(element => bar(element)))
34+
1 | (( foo?.forEach(element => bar(element)) ))
35+
36+
> Output
37+
38+
`␊
39+
1 | if (foo) for (const element of foo) bar(element) ␊
40+
`
2941

3042
> Error 1/1
3143
3244
`␊
33-
> 1 | (foo?.forEach(element => bar(element)))␊
34-
| ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊
45+
> 1 | (( foo?.forEach(element => bar(element)) ))␊
46+
| ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊
3547
`
3648

3749
## Invalid #4
29 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)