Skip to content

Commit

Permalink
no-array-for-each: Simplify fix (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 26, 2022
1 parent 8fe4272 commit 989fe9d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 84 deletions.
19 changes: 5 additions & 14 deletions rules/no-array-for-each.js
@@ -1,7 +1,6 @@
'use strict';
const {
isParenthesized,
isArrowToken,
isCommaToken,
isSemicolonToken,
isClosingParenToken,
Expand All @@ -13,7 +12,7 @@ const {extendFixRange} = require('./fix/index.js');
const needsSemicolon = require('./utils/needs-semicolon.js');
const shouldAddParenthesesToExpressionStatementExpression = require('./utils/should-add-parentheses-to-expression-statement-expression.js');
const shouldAddParenthesesToMemberExpressionObject = require('./utils/should-add-parentheses-to-member-expression-object.js');
const {getParentheses} = require('./utils/parentheses.js');
const {getParentheses, getParenthesizedRange} = 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');
Expand Down Expand Up @@ -119,17 +118,7 @@ function getFixFunction(callExpression, functionInfo, context) {

const getForOfLoopHeadRange = () => {
const [start] = callExpression.range;
let end;
if (callback.body.type === 'BlockStatement') {
end = callback.body.range[0];
} else {
// In this case, parentheses are not included in body location, so we look for `=>` token
// foo.forEach(bar => ({bar}))
// ^
const arrowToken = sourceCode.getTokenBefore(callback.body, isArrowToken);
end = arrowToken.range[1];
}

const [end] = getParenthesizedRange(callback.body, sourceCode);
return [start, end];
};

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

// Replace these with `for (const … of …) `
// foo.forEach(bar => bar)
// ^^^^^^^^^^^^^^^^^^ (space after `=>` didn't included)
// ^^^^^^^^^^^^^^^^^^^^^^
// foo.forEach(bar => (bar))
// ^^^^^^^^^^^^^^^^^^^^^^
// foo.forEach(bar => {})
// ^^^^^^^^^^^^^^^^^^^^^^
// foo.forEach(function(bar) {})
Expand Down
6 changes: 3 additions & 3 deletions test/no-array-for-each.mjs
Expand Up @@ -555,7 +555,7 @@ test.typescript({
})
`,
output: outdent`
for (const pg of staticPages) allStaticPages.add(pg)
for (const pg of staticPages) allStaticPages.add(pg)
pageInfos.forEach((info: PageInfo, key: string) => {
allPageInfos.set(key, info)
})
Expand Down Expand Up @@ -590,7 +590,7 @@ test.typescript({
`,
output: outdent`
const cloakVals: string[] = [];
for (const element of elements) cloakVals.push(cloakElement(element));
for (const element of elements) cloakVals.push(cloakElement(element));
`,
errors: 1,
},
Expand Down Expand Up @@ -621,7 +621,7 @@ test({
`,
output: outdent`
while (true) return;
for (const element of foo) bar(element);
for (const element of foo) bar(element);
`,
errors: 1,
parserOptions: globalReturnOptions,
Expand Down

0 comments on commit 989fe9d

Please sign in to comment.