Skip to content

Commit 989fe9d

Browse files
authored
no-array-for-each: Simplify fix (#1826)
1 parent 8fe4272 commit 989fe9d

File tree

4 files changed

+75
-84
lines changed

4 files changed

+75
-84
lines changed

rules/no-array-for-each.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const {
33
isParenthesized,
4-
isArrowToken,
54
isCommaToken,
65
isSemicolonToken,
76
isClosingParenToken,
@@ -13,7 +12,7 @@ const {extendFixRange} = require('./fix/index.js');
1312
const needsSemicolon = require('./utils/needs-semicolon.js');
1413
const shouldAddParenthesesToExpressionStatementExpression = require('./utils/should-add-parentheses-to-expression-statement-expression.js');
1514
const shouldAddParenthesesToMemberExpressionObject = require('./utils/should-add-parentheses-to-member-expression-object.js');
16-
const {getParentheses} = require('./utils/parentheses.js');
15+
const {getParentheses, getParenthesizedRange} = require('./utils/parentheses.js');
1716
const isFunctionSelfUsedInside = require('./utils/is-function-self-used-inside.js');
1817
const {isNodeMatches} = require('./utils/is-node-matches.js');
1918
const assertToken = require('./utils/assert-token.js');
@@ -119,17 +118,7 @@ function getFixFunction(callExpression, functionInfo, context) {
119118

120119
const getForOfLoopHeadRange = () => {
121120
const [start] = callExpression.range;
122-
let end;
123-
if (callback.body.type === 'BlockStatement') {
124-
end = callback.body.range[0];
125-
} else {
126-
// In this case, parentheses are not included in body location, so we look for `=>` token
127-
// foo.forEach(bar => ({bar}))
128-
// ^
129-
const arrowToken = sourceCode.getTokenBefore(callback.body, isArrowToken);
130-
end = arrowToken.range[1];
131-
}
132-
121+
const [end] = getParenthesizedRange(callback.body, sourceCode);
133122
return [start, end];
134123
};
135124

@@ -215,7 +204,9 @@ function getFixFunction(callExpression, functionInfo, context) {
215204

216205
// Replace these with `for (const … of …) `
217206
// foo.forEach(bar => bar)
218-
// ^^^^^^^^^^^^^^^^^^ (space after `=>` didn't included)
207+
// ^^^^^^^^^^^^^^^^^^^^^^
208+
// foo.forEach(bar => (bar))
209+
// ^^^^^^^^^^^^^^^^^^^^^^
219210
// foo.forEach(bar => {})
220211
// ^^^^^^^^^^^^^^^^^^^^^^
221212
// foo.forEach(function(bar) {})

test/no-array-for-each.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ test.typescript({
555555
})
556556
`,
557557
output: outdent`
558-
for (const pg of staticPages) allStaticPages.add(pg)
558+
for (const pg of staticPages) allStaticPages.add(pg)
559559
pageInfos.forEach((info: PageInfo, key: string) => {
560560
allPageInfos.set(key, info)
561561
})
@@ -590,7 +590,7 @@ test.typescript({
590590
`,
591591
output: outdent`
592592
const cloakVals: string[] = [];
593-
for (const element of elements) cloakVals.push(cloakElement(element));
593+
for (const element of elements) cloakVals.push(cloakElement(element));
594594
`,
595595
errors: 1,
596596
},
@@ -621,7 +621,7 @@ test({
621621
`,
622622
output: outdent`
623623
while (true) return;
624-
for (const element of foo) bar(element);
624+
for (const element of foo) bar(element);
625625
`,
626626
errors: 1,
627627
parserOptions: globalReturnOptions,

0 commit comments

Comments
 (0)