diff --git a/rules/no-array-for-each.js b/rules/no-array-for-each.js index 98cbe49f28..15103e021c 100644 --- a/rules/no-array-for-each.js +++ b/rules/no-array-for-each.js @@ -1,7 +1,6 @@ 'use strict'; const { isParenthesized, - isArrowToken, isCommaToken, isSemicolonToken, isClosingParenToken, @@ -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'); @@ -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]; }; @@ -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) {}) diff --git a/test/no-array-for-each.mjs b/test/no-array-for-each.mjs index aa77f39e2a..2e0db378f8 100644 --- a/test/no-array-for-each.mjs +++ b/test/no-array-for-each.mjs @@ -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) }) @@ -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, }, @@ -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, diff --git a/test/snapshots/no-array-for-each.mjs.md b/test/snapshots/no-array-for-each.mjs.md index da2d17333f..cefabd97c5 100644 --- a/test/snapshots/no-array-for-each.mjs.md +++ b/test/snapshots/no-array-for-each.mjs.md @@ -20,7 +20,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo) bar(element) ␊ + 1 | for (const element of foo) bar(element) ␊ ` > Error 1/1 @@ -36,7 +36,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo) for (const element of foo) bar(element) ␊ + 1 | if (foo) for (const element of foo) bar(element) ␊ ` > Error 1/1 @@ -774,7 +774,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const foo of a((foo) => foo)) bar();␊ + 1 | for (const foo of a((foo) => foo)) bar();␊ ` > Error 1/1 @@ -795,7 +795,7 @@ Generated by [AVA](https://avajs.dev). ␊ --------------------------------------------------------------------------------␊ Suggestion 1/1: Switch to \`for…of\`.␊ - 1 | if (a((foo) => foo)) for (const foo of a((foo) => foo)) bar();␊ + 1 | if (a((foo) => foo)) for (const foo of a((foo) => foo)) bar();␊ ` ## Invalid #60 @@ -804,7 +804,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, foo] of a((foo, index) => foo + index).entries()) bar();␊ + 1 | for (const [index, foo] of a((foo, index) => foo + index).entries()) bar();␊ ` > Error 1/1 @@ -825,7 +825,7 @@ Generated by [AVA](https://avajs.dev). ␊ --------------------------------------------------------------------------------␊ Suggestion 1/1: Switch to \`for…of\`.␊ - 1 | if (a((foo, index) => foo + index)) for (const [index, foo] of a((foo, index) => foo + index).entries()) bar();␊ + 1 | if (a((foo, index) => foo + index)) for (const [index, foo] of a((foo, index) => foo + index).entries()) bar();␊ ` ## Invalid #62 @@ -838,7 +838,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | const foo = [];␊ 2 | const index = 1;␊ - 3 | for (const [index, foo] of a.entries()) foo[index];␊ + 3 | for (const [index, foo] of a.entries()) foo[index];␊ ` > Error 1/1 @@ -860,7 +860,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | const foo = [];␊ 2 | const index = 1;␊ - 3 | if (a) for (const [index, foo] of a.entries()) foo[index];␊ + 3 | if (a) for (const [index, foo] of a.entries()) foo[index];␊ ` > Error 1/1 @@ -1518,7 +1518,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo) bar(element);␊ + 1 | for (const element of foo) bar(element);␊ ` > Error 1/1 @@ -1534,7 +1534,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo) for (const element of foo) bar(element);␊ + 1 | if (foo) for (const element of foo) bar(element);␊ ` > Error 1/1 @@ -1682,7 +1682,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, element] of foo.entries()) bar(element, index);␊ + 1 | for (const [index, element] of foo.entries()) bar(element, index);␊ ` > Error 1/1 @@ -1698,7 +1698,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo) for (const [index, element] of foo.entries()) bar(element, index);␊ + 1 | if (foo) for (const [index, element] of foo.entries()) bar(element, index);␊ ` > Error 1/1 @@ -1714,7 +1714,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo?.bar) bar(element);␊ + 1 | for (const element of foo?.bar) bar(element);␊ ` > Error 1/1 @@ -1730,7 +1730,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo?.bar) for (const element of foo?.bar) bar(element);␊ + 1 | if (foo?.bar) for (const element of foo?.bar) bar(element);␊ ` > Error 1/1 @@ -1746,7 +1746,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo.bar) log(element)␊ + 1 | for (const element of foo.bar) log(element)␊ ` > Error 1/1 @@ -1762,7 +1762,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo.bar) for (const element of foo.bar) log(element)␊ + 1 | if (foo.bar) for (const element of foo.bar) log(element)␊ ` > Error 1/1 @@ -1778,7 +1778,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo.bar()) log(element)␊ + 1 | for (const element of foo.bar()) log(element)␊ ` > Error 1/1 @@ -1799,7 +1799,7 @@ Generated by [AVA](https://avajs.dev). ␊ --------------------------------------------------------------------------------␊ Suggestion 1/1: Switch to \`for…of\`.␊ - 1 | if (foo.bar()) for (const element of foo.bar()) log(element)␊ + 1 | if (foo.bar()) for (const element of foo.bar()) log(element)␊ ` ## Invalid #110 @@ -1808,7 +1808,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of (a ? b : c)) log(element)␊ + 1 | for (const element of (a ? b : c)) log(element)␊ ` > Error 1/1 @@ -1824,7 +1824,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (a ? b : c) for (const element of (a ? b : c)) log(element)␊ + 1 | if (a ? b : c) for (const element of (a ? b : c)) log(element)␊ ` > Error 1/1 @@ -1840,7 +1840,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of (a ? b : c())) log(element)␊ + 1 | for (const element of (a ? b : c())) log(element)␊ ` > Error 1/1 @@ -1861,7 +1861,7 @@ Generated by [AVA](https://avajs.dev). ␊ --------------------------------------------------------------------------------␊ Suggestion 1/1: Switch to \`for…of\`.␊ - 1 | if (a ? b : c()) for (const element of (a ? b : c())) log(element)␊ + 1 | if (a ? b : c()) for (const element of (a ? b : c())) log(element)␊ ` ## Invalid #114 @@ -1870,7 +1870,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of (foo || bar)) log(element)␊ + 1 | for (const element of (foo || bar)) log(element)␊ ` > Error 1/1 @@ -1886,7 +1886,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo || bar) for (const element of (foo || bar)) log(element)␊ + 1 | if (foo || bar) for (const element of (foo || bar)) log(element)␊ ` > Error 1/1 @@ -1902,7 +1902,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of (foo || bar())) log(element)␊ + 1 | for (const element of (foo || bar())) log(element)␊ ` > Error 1/1 @@ -1923,7 +1923,7 @@ Generated by [AVA](https://avajs.dev). ␊ --------------------------------------------------------------------------------␊ Suggestion 1/1: Switch to \`for…of\`.␊ - 1 | if (foo || bar()) for (const element of (foo || bar())) log(element)␊ + 1 | if (foo || bar()) for (const element of (foo || bar())) log(element)␊ ` ## Invalid #118 @@ -1932,7 +1932,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, element] of (foo).entries()) bar(element, index)␊ + 1 | for (const [index, element] of (foo).entries()) bar(element, index)␊ ` > Error 1/1 @@ -1948,7 +1948,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo) for (const [index, element] of (foo).entries()) bar(element, index)␊ + 1 | if (foo) for (const [index, element] of (foo).entries()) bar(element, index)␊ ` > Error 1/1 @@ -1964,7 +1964,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, element] of (0, foo).entries()) bar(element, index)␊ + 1 | for (const [index, element] of (0, foo).entries()) bar(element, index)␊ ` > Error 1/1 @@ -1980,7 +1980,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (0, foo) for (const [index, element] of (0, foo).entries()) bar(element, index)␊ + 1 | if (0, foo) for (const [index, element] of (0, foo).entries()) bar(element, index)␊ ` > Error 1/1 @@ -2128,7 +2128,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo) bar(element);␊ + 1 | for (const element of foo) bar(element);␊ ` > Error 1/1 @@ -2144,7 +2144,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo) for (const element of foo) bar(element);␊ + 1 | if (foo) for (const element of foo) bar(element);␊ ` > Error 1/1 @@ -2161,7 +2161,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo) bar(element)␊ + 1 | for (const element of foo) bar(element)␊ 2 | ;[foo].pop();␊ ` @@ -2180,7 +2180,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo) for (const element of foo) bar(element)␊ + 1 | if (foo) for (const element of foo) bar(element)␊ 2 | ;[foo].pop();␊ ` @@ -2272,7 +2272,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo) ({})␊ + 1 | for (const element of foo) ({})␊ ` > Error 1/1 @@ -2288,7 +2288,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo) for (const element of foo) ({})␊ + 1 | if (foo) for (const element of foo) ({})␊ ` > Error 1/1 @@ -2304,7 +2304,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo) bar(element);␊ + 1 | for (const element of foo) bar(element);␊ ` > Error 1/1 @@ -2320,7 +2320,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (foo) for (const element of foo) bar(element);␊ + 1 | if (foo) for (const element of foo) bar(element);␊ ` > Error 1/1 @@ -2608,7 +2608,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, children] of node.children.index.entries()) process(children, index)␊ + 1 | for (const [index, children] of node.children.index.entries()) process(children, index)␊ ` > Error 1/1 @@ -2624,7 +2624,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (node.children.index) for (const [index, children] of node.children.index.entries()) process(children, index)␊ + 1 | if (node.children.index) for (const [index, children] of node.children.index.entries()) process(children, index)␊ ` > Error 1/1 @@ -2640,7 +2640,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, children] of (node?.children?.index).entries()) process(children, index)␊ + 1 | for (const [index, children] of (node?.children?.index).entries()) process(children, index)␊ ` > Error 1/1 @@ -2656,7 +2656,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (node?.children?.index) for (const [index, children] of (node?.children?.index).entries()) process(children, index)␊ + 1 | if (node?.children?.index) for (const [index, children] of (node?.children?.index).entries()) process(children, index)␊ ` > Error 1/1 @@ -2712,7 +2712,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, children] of [{children: 1, index: 1}].entries()) process(children, index)␊ + 1 | for (const [index, children] of [{children: 1, index: 1}].entries()) process(children, index)␊ ` > Error 1/1 @@ -2728,7 +2728,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if ([{children: 1, index: 1}]) for (const [index, children] of [{children: 1, index: 1}].entries()) process(children, index)␊ + 1 | if ([{children: 1, index: 1}]) for (const [index, children] of [{children: 1, index: 1}].entries()) process(children, index)␊ ` > Error 1/1 @@ -2824,7 +2824,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, name] of [function name() {}].entries()) process(name, index)␊ + 1 | for (const [index, name] of [function name() {}].entries()) process(name, index)␊ ` > Error 1/1 @@ -2840,7 +2840,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if ([function name() {}]) for (const [index, name] of [function name() {}].entries()) process(name, index)␊ + 1 | if ([function name() {}]) for (const [index, name] of [function name() {}].entries()) process(name, index)␊ ` > Error 1/1 @@ -2864,7 +2864,7 @@ Generated by [AVA](https://avajs.dev). 2 | function () {␊ 3 | function index() {}␊ 4 | }␊ - 5 | ].entries()) process(name, index)␊ + 5 | ].entries()) process(name, index)␊ ` > Error 1/1 @@ -2896,7 +2896,7 @@ Generated by [AVA](https://avajs.dev). 6 | function () {␊ 7 | function index() {}␊ 8 | }␊ - 9 | ].entries()) process(name, index)␊ + 9 | ].entries()) process(name, index)␊ ` > Error 1/1 @@ -2924,7 +2924,7 @@ Generated by [AVA](https://avajs.dev). 2 | function () {␊ 3 | class index {}␊ 4 | }␊ - 5 | ].entries()) process(name, index)␊ + 5 | ].entries()) process(name, index)␊ ` > Error 1/1 @@ -2956,7 +2956,7 @@ Generated by [AVA](https://avajs.dev). 6 | function () {␊ 7 | class index {}␊ 8 | }␊ - 9 | ].entries()) process(name, index)␊ + 9 | ].entries()) process(name, index)␊ ` > Error 1/1 @@ -2976,7 +2976,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [index, Foo] of [class Foo{}].entries()) process(Foo, index)␊ + 1 | for (const [index, Foo] of [class Foo{}].entries()) process(Foo, index)␊ ` > Error 1/1 @@ -2992,7 +2992,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if ([class Foo{}]) for (const [index, Foo] of [class Foo{}].entries()) process(Foo, index)␊ + 1 | if ([class Foo{}]) for (const [index, Foo] of [class Foo{}].entries()) process(Foo, index)␊ ` > Error 1/1 @@ -3008,7 +3008,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const [Foo, X] of [class Foo{}].entries()) process(X, Foo)␊ + 1 | for (const [Foo, X] of [class Foo{}].entries()) process(X, Foo)␊ ` > Error 1/1 @@ -3024,7 +3024,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if ([class Foo{}]) for (const [Foo, X] of [class Foo{}].entries()) process(X, Foo)␊ + 1 | if ([class Foo{}]) for (const [Foo, X] of [class Foo{}].entries()) process(X, Foo)␊ ` > Error 1/1 @@ -3048,7 +3048,7 @@ Generated by [AVA](https://avajs.dev). 2 | class Foo {␊ 3 | bar() {}␊ 4 | }␊ - 5 | ].entries()) process(Foo, bar)␊ + 5 | ].entries()) process(Foo, bar)␊ ` > Error 1/1 @@ -3080,7 +3080,7 @@ Generated by [AVA](https://avajs.dev). 6 | class Foo {␊ 7 | bar() {}␊ 8 | }␊ - 9 | ].entries()) process(Foo, bar)␊ + 9 | ].entries()) process(Foo, bar)␊ ` > Error 1/1 @@ -3922,7 +3922,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | const a = () => { for (const element of foo) bar(element) } ␊ + 1 | const a = () => { for (const element of foo) bar(element) } ␊ ` > Error 1/1 @@ -3938,7 +3938,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | const a = () => { if (foo) for (const element of foo) bar(element) } ␊ + 1 | const a = () => { if (foo) for (const element of foo) bar(element) } ␊ ` > Error 1/1 @@ -3954,7 +3954,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | const a = () => { for (const element of foo) bar(element) } ;␊ + 1 | const a = () => { for (const element of foo) bar(element) } ;␊ ` > Error 1/1 @@ -3970,7 +3970,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | const a = () => { if (foo) for (const element of foo) bar(element) } ;␊ + 1 | const a = () => { if (foo) for (const element of foo) bar(element) } ;␊ ` > Error 1/1 @@ -3986,7 +3986,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | const a = () => { for (const element of foo) bar(element) }␊ + 1 | const a = () => { for (const element of foo) bar(element) }␊ ` > Error 1/1 @@ -4002,7 +4002,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | const a = () => { if (foo) for (const element of foo) bar(element) }␊ + 1 | const a = () => { if (foo) for (const element of foo) bar(element) }␊ ` > Error 1/1 @@ -4018,7 +4018,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | const a = () => { for (const element of foo) bar(element) };␊ + 1 | const a = () => { for (const element of foo) bar(element) };␊ ` > Error 1/1 @@ -4034,7 +4034,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | const a = () => { if (foo) for (const element of foo) bar(element) };␊ + 1 | const a = () => { if (foo) for (const element of foo) bar(element) };␊ ` > Error 1/1 @@ -4070,7 +4070,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | if (1) for (const [b, a] of (1).entries()) call(a, b)␊ + 1 | if (1) for (const [b, a] of (1).entries()) call(a, b)␊ ` > Error 1/1 @@ -4086,7 +4086,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const arrayInArray of array) for (const element of arrayInArray) bar(element);␊ + 1 | for (const arrayInArray of array) for (const element of arrayInArray) bar(element);␊ ` > Error 1/2 @@ -4109,7 +4109,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const arrayInArray of array) if (arrayInArray) for (const element of arrayInArray) bar(element);␊ + 1 | for (const arrayInArray of array) if (arrayInArray) for (const element of arrayInArray) bar(element);␊ ` > Error 1/2 diff --git a/test/snapshots/no-array-for-each.mjs.snap b/test/snapshots/no-array-for-each.mjs.snap index 79f3f00f0f..41cec331d4 100644 Binary files a/test/snapshots/no-array-for-each.mjs.snap and b/test/snapshots/no-array-for-each.mjs.snap differ