diff --git a/docs/rules/no-array-for-each.md b/docs/rules/no-array-for-each.md index 036d6f7e6f..967d3f318d 100644 --- a/docs/rules/no-array-for-each.md +++ b/docs/rules/no-array-for-each.md @@ -4,7 +4,7 @@ βœ… *This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.* -πŸ”§ *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).* +πŸ”§πŸ’‘ *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) and provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).* Benefits of [`for…of` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) over [`Array#forEach(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) can include: @@ -21,6 +21,12 @@ array.forEach(element => { }); ``` +```js +array?.forEach(element => { + bar(element); +}); +``` + ```js array.forEach((element, index) => { bar(element, index); diff --git a/readme.md b/readme.md index bb77e05536..2e6c45119b 100644 --- a/readme.md +++ b/readme.md @@ -69,7 +69,7 @@ Each rule has emojis denoting: | [new-for-builtins](docs/rules/new-for-builtins.md) | Enforce the use of `new` for all builtins, except `String`, `Number`, `Boolean`, `Symbol` and `BigInt`. | βœ… | πŸ”§ | | | [no-abusive-eslint-disable](docs/rules/no-abusive-eslint-disable.md) | Enforce specifying rules to disable in `eslint-disable` comments. | βœ… | | | | [no-array-callback-reference](docs/rules/no-array-callback-reference.md) | Prevent passing a function reference directly to iterator methods. | βœ… | | πŸ’‘ | -| [no-array-for-each](docs/rules/no-array-for-each.md) | Prefer `for…of` over `Array#forEach(…)`. | βœ… | πŸ”§ | | +| [no-array-for-each](docs/rules/no-array-for-each.md) | Prefer `for…of` over `Array#forEach(…)`. | βœ… | πŸ”§ | πŸ’‘ | | [no-array-method-this-argument](docs/rules/no-array-method-this-argument.md) | Disallow using the `this` argument in array methods. | βœ… | πŸ”§ | πŸ’‘ | | [no-array-push-push](docs/rules/no-array-push-push.md) | Enforce combining multiple `Array#push()` into one call. | βœ… | πŸ”§ | πŸ’‘ | | [no-array-reduce](docs/rules/no-array-reduce.md) | Disallow `Array#reduce()` and `Array#reduceRight()`. | βœ… | | | diff --git a/rules/no-array-for-each.js b/rules/no-array-for-each.js index 74e6cb8e02..a1f74fc9fc 100644 --- a/rules/no-array-for-each.js +++ b/rules/no-array-for-each.js @@ -6,20 +6,24 @@ const { isSemicolonToken, isClosingParenToken, findVariable, + hasSideEffect, } = require('eslint-utils'); const {methodCallSelector, referenceIdentifierSelector} = require('./selectors/index.js'); 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 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 MESSAGE_ID = 'no-array-for-each'; +const MESSAGE_ID_ERROR = 'no-array-for-each/error'; +const MESSAGE_ID_SUGGESTION = 'no-array-for-each/suggestion'; const messages = { - [MESSAGE_ID]: 'Use `for…of` instead of `Array#forEach(…)`.', + [MESSAGE_ID_ERROR]: 'Use `for…of` instead of `Array#forEach(…)`.', + [MESSAGE_ID_SUGGESTION]: 'Switch to `for…of`.', }; const arrayForEachCallSelector = methodCallSelector({ @@ -36,6 +40,11 @@ const continueAbleNodeTypes = new Set([ 'ForInStatement', ]); +const stripChainExpression = node => + (node.parent.type === 'ChainExpression' && node.parent.expression === node) + ? node.parent + : node; + function isReturnStatementInContinueAbleNodes(returnStatement, callbackFunction) { for (let node = returnStatement; node && node !== callbackFunction; node = node.parent) { if (continueAbleNodeTypes.has(node.type)) { @@ -73,6 +82,9 @@ function getFixFunction(callExpression, functionInfo, context) { const parameters = callback.params; const array = callExpression.callee.object; const {returnStatements} = functionInfo.get(callback); + const isOptionalArray = callExpression.callee.optional; + const expressionStatement = stripChainExpression(callExpression).parent; + const arrayText = sourceCode.getText(array); const getForOfLoopHeadText = () => { const [elementText, indexText] = parameters.map(parameter => sourceCode.getText(parameter)); @@ -84,12 +96,16 @@ function getFixFunction(callExpression, functionInfo, context) { text += useEntries ? `[${indexText}, ${elementText}]` : elementText; text += ' of '; - let arrayText = sourceCode.getText(array); - if (isParenthesized(array, sourceCode)) { - arrayText = `(${arrayText})`; - } + const shouldAddParenthesesToArray + = isParenthesized(array, sourceCode) + || ( + // `1?.forEach()` -> `(1).entries()` + isOptionalArray + && useEntries + && shouldAddParenthesesToMemberExpressionObject(array, sourceCode) + ); - text += arrayText; + text += shouldAddParenthesesToArray ? `(${arrayText})` : arrayText; if (useEntries) { text += '.entries()'; @@ -228,7 +244,7 @@ function getFixFunction(callExpression, functionInfo, context) { yield * replaceReturnStatement(returnStatement, fixer); } - const expressionStatementLastToken = sourceCode.getLastToken(callExpression.parent); + const expressionStatementLastToken = sourceCode.getLastToken(expressionStatement); // Remove semicolon if it's not needed anymore // foo.forEach(bar => {}); // ^ @@ -238,6 +254,10 @@ function getFixFunction(callExpression, functionInfo, context) { yield * fixSpaceAroundKeyword(fixer, callExpression.parent, sourceCode); + if (isOptionalArray) { + yield fixer.insertTextBefore(callExpression, `if (${arrayText}) `); + } + // Prevent possible variable conflicts yield * extendFixRange(fixer, callExpression.parent.range); }; @@ -302,19 +322,12 @@ function isFunctionParameterVariableReassigned(callbackFunction, context) { }); } -const isExpressionStatement = node => { - if (node.type === 'ChainExpression') { - node = node.parent; - } - - return node.type === 'ExpressionStatement'; -}; - 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 ) { @@ -322,12 +335,7 @@ function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context } // Check ancestors, we only fix `ExpressionStatement` - if (!isExpressionStatement(callExpression.parent)) { - return false; - } - - // Check `CallExpression.callee` - if (callExpression.callee.optional) { + if (stripChainExpression(callExpression).parent.type !== 'ExpressionStatement') { return false; } @@ -379,6 +387,7 @@ const create = context => { const callExpressions = []; const allIdentifiers = []; const functionInfo = new Map(); + const sourceCode = context.getSourceCode(); return { ':function'(node) { @@ -411,13 +420,31 @@ const create = context => { }, * 'Program:exit'() { for (const {node, scope} of callExpressions) { + // TODO: Rename this to iteratable + const array = node.callee; + const problem = { - node: node.callee.property, - messageId: MESSAGE_ID, + node: array.property, + messageId: MESSAGE_ID_ERROR, }; - if (isFixable(node, {scope, allIdentifiers, functionInfo, context})) { - problem.fix = getFixFunction(node, functionInfo, context); + if (!isFixable(node, {scope, allIdentifiers, functionInfo, context})) { + yield problem; + continue; + } + + const shouldUseSuggestion = array.optional && hasSideEffect(array, sourceCode); + const fix = getFixFunction(node, functionInfo, context); + + if (shouldUseSuggestion) { + problem.suggest = [ + { + messageId: MESSAGE_ID_SUGGESTION, + fix, + }, + ]; + } else { + problem.fix = fix; } yield problem; @@ -435,6 +462,7 @@ module.exports = { description: 'Prefer `for…of` over `Array#forEach(…)`.', }, fixable: 'code', + hasSuggestions: true, messages, }, }; diff --git a/test/no-array-for-each.mjs b/test/no-array-for-each.mjs index e3df34b062..7a35962bf6 100644 --- a/test/no-array-for-each.mjs +++ b/test/no-array-for-each.mjs @@ -19,404 +19,415 @@ test.snapshot({ `, ], invalid: [ - // Not fixable 'foo.forEach?.(element => bar(element))', - '(foo.forEach(element => bar(element)))', - 'foo.forEach(element => bar(element), thisArgument)', - 'foo.forEach()', - 'const baz = foo.forEach(element => bar(element))', - 'foo?.forEach(element => bar(element))', - 'foo.forEach(bar)', - 'foo.forEach(async function(element) {})', - 'foo.forEach(function * (element) {})', - 'foo.forEach(() => bar())', - 'foo.forEach((element, index, array) => bar())', - 'property.forEach(({property}) => bar(property))', + ...[ + // Not fixable + '(foo.forEach(element => bar(element)))', + 'foo.forEach(element => bar(element), thisArgument)', + 'foo.forEach()', + 'const baz = foo.forEach(element => bar(element))', + 'foo.forEach(bar)', + 'foo.forEach(async function(element) {})', + 'foo.forEach(function * (element) {})', + 'foo.forEach(() => bar())', + 'foo.forEach((element, index, array) => bar())', + 'property.forEach(({property}) => bar(property))', + '() => foo.forEach()', - // Can't turn `return` to `continue` - outdent` - foo.forEach(element => { - do { - return - } while (element) - }); - `, - outdent` - foo.forEach(element => { - while (element) { - return; - } - }); - `, - outdent` - foo.forEach(element => { - for (let i = 0; i < 2; i++) { - return; - } - }); - `, - outdent` - foo.forEach(element => { - for (let i in element) { - return; - } - }); - `, - outdent` - foo.forEach(element => { - for (let i of element) { - return; - } - }); - `, - // `ReturnStatement` in `switch` is fixable - outdent` - foo.forEach(element => { - switch (element) { - case 1: - break; - case 2: + // Can't turn `return` to `continue` + outdent` + foo.forEach(element => { + do { + return + } while (element) + }); + `, + outdent` + foo.forEach(element => { + while (element) { return; - } - }); - `, + } + }); + `, + outdent` + foo.forEach(element => { + for (let i = 0; i < 2; i++) { + return; + } + }); + `, + outdent` + foo.forEach(element => { + for (let i in element) { + return; + } + }); + `, + outdent` + foo.forEach(element => { + for (let i of element) { + return; + } + }); + `, + // `ReturnStatement` in `switch` is fixable + outdent` + foo.forEach(element => { + switch (element) { + case 1: + break; + case 2: + return; + } + }); + `, - // `parameters` - 'foo.forEach(foo => bar());', - outdent` - const foo = []; - foo.forEach(foo => bar()); - `, - outdent` - const foo = []; - function unicorn() { + // `parameters` + 'foo.forEach(foo => bar());', + outdent` + const foo = []; foo.forEach(foo => bar()); - } - `, - 'index.forEach((a, index) => bar());', - outdent` - const index = []; - index.forEach((a, index) => bar()); - `, - outdent` - const index = []; - function unicorn() { + `, + outdent` + const foo = []; + function unicorn() { + foo.forEach(foo => bar()); + } + `, + 'index.forEach((a, index) => bar());', + outdent` + const index = []; index.forEach((a, index) => bar()); - } - `, - 'a[foo].forEach(foo => bar());', - outdent` - const foo = 1; - a[foo].forEach(foo => bar()); - `, - outdent` - const foo = 1; - function unicorn() { + `, + outdent` + const index = []; + function unicorn() { + index.forEach((a, index) => bar()); + } + `, + 'a[foo].forEach(foo => bar());', + outdent` + const foo = 1; a[foo].forEach(foo => bar()); - } - `, - 'a[index].forEach((b, index) => bar());', - 'a((foo) => foo).forEach(foo => bar());', - 'a((foo, index) => foo + index).forEach((foo, index) => bar());', - outdent` - const foo = []; - const index = 1; - a.forEach((foo, index) => foo[index]); - `, - - // `FunctionExpression.id` - outdent` - foo.forEach(function a(element) { - bar(a) - }) - `, - outdent` - foo.forEach(function a(element) { - function b() { - bar(a) + `, + outdent` + const foo = 1; + function unicorn() { + a[foo].forEach(foo => bar()); } - }) - `, - outdent` - foo.forEach(function a(element) { - function b(a) { + `, + 'a[index].forEach((b, index) => bar());', + 'a((foo) => foo).forEach(foo => bar());', + 'a((foo, index) => foo + index).forEach((foo, index) => bar());', + outdent` + const foo = []; + const index = 1; + a.forEach((foo, index) => foo[index]); + `, + + // `FunctionExpression.id` + outdent` + foo.forEach(function a(element) { bar(a) - } - }) - `, + }) + `, + outdent` + foo.forEach(function a(element) { + function b() { + bar(a) + } + }) + `, + outdent` + foo.forEach(function a(element) { + function b(a) { + bar(a) + } + }) + `, - // This - outdent` - foo.forEach(function(element) { - bar(this) - }) - `, - outdent` - foo.forEach(function(element) { - function b() { + // This + outdent` + foo.forEach(function(element) { bar(this) - } - }) - `, - outdent` - foo.forEach(function(element) { - const x = b => { + }) + `, + outdent` + foo.forEach(function(element) { + function b() { + bar(this) + } + }) + `, + outdent` + foo.forEach(function(element) { + const x = b => { + bar(this) + } + }) + `, + outdent` + foo.forEach((element) => { bar(this) - } - }) - `, - outdent` - foo.forEach((element) => { - bar(this) - }) - `, + }) + `, - // `arguments` - outdent` - foo.forEach(function(element) { - bar(arguments) - }) - `, - outdent` - foo.forEach(function(element) { - function b() { + // `arguments` + outdent` + foo.forEach(function(element) { bar(arguments) - } - }) - `, - outdent` - foo.forEach(function(element) { - const b = () => { + }) + `, + outdent` + foo.forEach(function(element) { + function b() { + bar(arguments) + } + }) + `, + outdent` + foo.forEach(function(element) { + const b = () => { + bar(arguments) + } + }) + `, + outdent` + foo.forEach((element) => { bar(arguments) - } - }) - `, - outdent` - foo.forEach((element) => { - bar(arguments) - }) - `, - 'a = foo?.bar.forEach((element) => bar(element));', + }) + `, + 'a = foo?.bar.forEach((element) => bar(element));', - // Auto-fix - outdent` - foo.forEach(function (element) { - bar(element); - }); - `, - outdent` - foo.forEach(function withName(element) { - bar(element); - }); - `, - outdent` - foo.forEach((element) => { - bar(element); - }); - `, - 'foo.forEach((element) => bar(element));', - outdent` - foo.forEach(function (element, index) { - bar(element, index); - }); - `, - outdent` - foo.forEach(function withName(element, index) { - bar(element, index); - }); - `, - outdent` - foo.forEach((element, index) => { - bar(element, index); - }); - `, - 'foo.forEach((element, index) => bar(element, index));', - 'foo?.bar.forEach((element) => bar(element));', - // Array is parenthesized - '(foo).forEach((element, index) => bar(element, index))', - '(0, foo).forEach((element, index) => bar(element, index))', - // Trailing comma - outdent` - foo.forEach(function (element) { - bar(element); - },); - `, - outdent` - foo.forEach(function withName(element) { - bar(element); - },); - `, - outdent` - foo.forEach((element) => { - bar(element); - },); - `, - 'foo.forEach((element) => bar(element),);', - // Last semi token - outdent` - foo.forEach((element) => bar(element)) - ;[foo].pop(); - `, - outdent` - foo.forEach((element) => { - bar(element); - }); - function noneRelatedFunction() { - while (element) { - return; - } - } - `, - // A test to make sure function head part range correctly calculated - 'foo.forEach(element => ({}))', - // `callback` is parenthesized - 'foo.forEach((((((element => bar(element)))))));', - outdent` - foo.forEach((element) => { - if (1) { - return; - } - if (1) { - return - } - if (1) { - return!true; - } - if (1) { - return!true - } - if (1) { - return bar(); - } - if (1) { - return bar() - unreachable(); - } - if (1) { - return {}; - } - if (1) { - return ({}); - } - if (1) { - return {a} = a; - } - if (1) { - return [a] = a; - } - if (1) { - foo - return [] - } - if (1) { - foo - return [foo] = bar; - } - }); - `, - 'node.children.index.forEach((children, index) => process(children, index))', - '(node?.children?.index).forEach((children, index) => process(children, index))', - 'node[children].index.forEach((children, index) => process(children, index))', - '(node.children?.[index]).forEach((children, index) => process(children, index))', - '[{children: 1, index: 1}].forEach((children, index) => process(children, index))', - '[{[children]: 1, index: 1}].forEach((children, index) => process(children, index))', - '[{[children]: 1, [index]: 1}].forEach((children, index) => process(children, index))', - '[{children, index: 1}].forEach((children, index) => process(children, index))', - '[{children: 1, index}].forEach((children, index) => process(children, index))', - '[function name() {}].forEach((name, index) => process(name, index))', - outdent` - [ - function () { - function index() {} - } - ].forEach((name, index) => process(name, index)) - `, - outdent` - [ - function () { - class index {} - } - ].forEach((name, index) => process(name, index)) - `, - '[class Foo{}].forEach((Foo, index) => process(Foo, index))', - '[class Foo{}].forEach((X, Foo) => process(X, Foo))', - outdent` - [ - class Foo { - bar() {} + // Auto-fix + outdent` + foo.forEach(function (element) { + bar(element); + }); + `, + outdent` + foo.forEach(function withName(element) { + bar(element); + }); + `, + outdent` + foo.forEach((element) => { + bar(element); + }); + `, + 'foo.forEach((element) => bar(element));', + outdent` + foo.forEach(function (element, index) { + bar(element, index); + }); + `, + outdent` + foo.forEach(function withName(element, index) { + bar(element, index); + }); + `, + outdent` + foo.forEach((element, index) => { + bar(element, index); + }); + `, + 'foo.forEach((element, index) => bar(element, index));', + 'foo?.bar.forEach((element) => bar(element));', + 'foo.bar.forEach((element) => log(element))', + 'foo.bar().forEach((element) => log(element))', + '(a ? b : c).forEach((element) => log(element))', + '(a ? b : c()).forEach((element) => log(element))', + '(foo || bar).forEach((element) => log(element))', + '(foo || bar()).forEach((element) => log(element))', + // Array is parenthesized + '(foo).forEach((element, index) => bar(element, index))', + '(0, foo).forEach((element, index) => bar(element, index))', + // Trailing comma + outdent` + foo.forEach(function (element) { + bar(element); + },); + `, + outdent` + foo.forEach(function withName(element) { + bar(element); + },); + `, + outdent` + foo.forEach((element) => { + bar(element); + },); + `, + 'foo.forEach((element) => bar(element),);', + // Last semi token + outdent` + foo.forEach((element) => bar(element)) + ;[foo].pop(); + `, + outdent` + foo.forEach((element) => { + bar(element); + }); + function noneRelatedFunction() { + while (element) { + return; + } } - ].forEach((Foo, bar) => process(Foo, bar)) - `, - 'foo.React.Children.forEach(bar)', - 'NotReact.Children.forEach(bar)', - 'React.NotChildren.forEach(bar)', - 'React?.Children.forEach(bar)', - 'NotChildren.forEach(bar)', - // Parameters are reassigned - outdent` - foo.forEach(element => { - element ++; - }) - `, - outdent` - foo.forEach(element => { - const a = -- element; - }) - `, - outdent` - foo.forEach((element, index) => { - index ++; - element = 2 - }); - `, - outdent` - foo.forEach((element, index) => { - element >>>= 2; - }); - `, - outdent` - foo.forEach((element, index) => { - const a = element = 1; - }); - `, - outdent` - foo.forEach((element, index) => { - let a; - a >>>= element; - }); - `, + `, + // A test to make sure function head part range correctly calculated + 'foo.forEach(element => ({}))', + // `callback` is parenthesized + 'foo.forEach((((((element => bar(element)))))));', + outdent` + foo.forEach((element) => { + if (1) { + return; + } + if (1) { + return + } + if (1) { + return!true; + } + if (1) { + return!true + } + if (1) { + return bar(); + } + if (1) { + return bar() + unreachable(); + } + if (1) { + return {}; + } + if (1) { + return ({}); + } + if (1) { + return {a} = a; + } + if (1) { + return [a] = a; + } + if (1) { + foo + return [] + } + if (1) { + foo + return [foo] = bar; + } + }); + `, + 'node.children.index.forEach((children, index) => process(children, index))', + '(node?.children?.index).forEach((children, index) => process(children, index))', + 'node[children].index.forEach((children, index) => process(children, index))', + '(node.children?.[index]).forEach((children, index) => process(children, index))', + '[{children: 1, index: 1}].forEach((children, index) => process(children, index))', + '[{[children]: 1, index: 1}].forEach((children, index) => process(children, index))', + '[{[children]: 1, [index]: 1}].forEach((children, index) => process(children, index))', + '[{children, index: 1}].forEach((children, index) => process(children, index))', + '[{children: 1, index}].forEach((children, index) => process(children, index))', + '[function name() {}].forEach((name, index) => process(name, index))', + outdent` + [ + function () { + function index() {} + } + ].forEach((name, index) => process(name, index)) + `, + outdent` + [ + function () { + class index {} + } + ].forEach((name, index) => process(name, index)) + `, + '[class Foo{}].forEach((Foo, index) => process(Foo, index))', + '[class Foo{}].forEach((X, Foo) => process(X, Foo))', + outdent` + [ + class Foo { + bar() {} + } + ].forEach((Foo, bar) => process(Foo, bar)) + `, + 'foo.React.Children.forEach(bar)', + 'NotReact.Children.forEach(bar)', + 'React.NotChildren.forEach(bar)', + 'React?.Children.forEach(bar)', + 'NotChildren.forEach(bar)', + // Parameters are reassigned + outdent` + foo.forEach(element => { + element ++; + }) + `, + outdent` + foo.forEach(element => { + const a = -- element; + }) + `, + outdent` + foo.forEach((element, index) => { + index ++; + element = 2 + }); + `, + outdent` + foo.forEach((element, index) => { + element >>>= 2; + }); + `, + outdent` + foo.forEach((element, index) => { + const a = element = 1; + }); + `, + outdent` + foo.forEach((element, index) => { + let a; + a >>>= element; + }); + `, - // Complicated parameters - 'foo.forEach(({property}) => {bar(property)})', - 'foo.forEach(({foo: {foo: [property]}}) => {bar(property, index)})', - 'foo.forEach((element, {bar: {bar: [index]}}) => {bar(element, index)})', - 'foo.forEach((element = elementDefaultValue, index = indexDefaultValue) => {})', - 'foo.forEach(({}) => {})', - 'foo.forEach(function foo({a, b, c, d}) {})', - 'foo.forEach(function foo({a, b, c, d, foo}) {})', - 'foo.forEach(({foo: property}) => {bar(property)})', - 'foo.forEach(({[foo]: property}) => {bar(property)})', - outdent` - foo.forEach(({element}, index) => { - element &&= 2; - }); - `, + // Complicated parameters + 'foo.forEach(({property}) => {bar(property)})', + 'foo.forEach(({foo: {foo: [property]}}) => {bar(property, index)})', + 'foo.forEach((element, {bar: {bar: [index]}}) => {bar(element, index)})', + 'foo.forEach((element = elementDefaultValue, index = indexDefaultValue) => {})', + 'foo.forEach(({}) => {})', + 'foo.forEach(function foo({a, b, c, d}) {})', + 'foo.forEach(function foo({a, b, c, d, foo}) {})', + 'foo.forEach(({foo: property}) => {bar(property)})', + 'foo.forEach(({[foo]: property}) => {bar(property)})', + outdent` + foo.forEach(({element}, index) => { + element &&= 2; + }); + `, - // Need switch to `BlockStatement`, #1318 - outdent` - foo.forEach(_ => { - if (true) return {}; - }) - `, - outdent` - foo.forEach(_ => { - if (true); - else return {}; - }) - `, + // Need switch to `BlockStatement`, #1318 + outdent` + foo.forEach(_ => { + if (true) return {}; + }) + `, + outdent` + foo.forEach(_ => { + if (true); + else return {}; + }) + `, + + // Need insert space after keyword + 'if (true) {} else[foo].forEach((element) => {})', + ].flatMap(code => [code, code.replace('.forEach', '?.forEach')]), - // Need insert space after keyword - 'if (true) {} else[foo].forEach((element) => {})', + // Should not fix to invalid code + '1?.forEach((a, b) => call(a, b))', ], }); diff --git a/test/snapshots/no-array-for-each.mjs.md b/test/snapshots/no-array-for-each.mjs.md index e1c0d5eff6..667ab195a2 100644 --- a/test/snapshots/no-array-for-each.mjs.md +++ b/test/snapshots/no-array-for-each.mjs.md @@ -25,6 +25,16 @@ Generated by [AVA](https://avajs.dev). ` ## Invalid #3 + 1 | (foo?.forEach(element => bar(element))) + +> Error 1/1 + + `␊ + > 1 | (foo?.forEach(element => bar(element)))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #4 1 | foo.forEach(element => bar(element), thisArgument) > Error 1/1 @@ -34,7 +44,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #4 +## Invalid #5 + 1 | foo?.forEach(element => bar(element), thisArgument) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => bar(element), thisArgument)␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #6 1 | foo.forEach() > Error 1/1 @@ -44,7 +64,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #5 +## Invalid #7 + 1 | foo?.forEach() + +> Error 1/1 + + `␊ + > 1 | foo?.forEach()␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #8 1 | const baz = foo.forEach(element => bar(element)) > Error 1/1 @@ -54,17 +84,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #6 - 1 | foo?.forEach(element => bar(element)) +## Invalid #9 + 1 | const baz = foo?.forEach(element => bar(element)) > Error 1/1 `␊ - > 1 | foo?.forEach(element => bar(element))␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | const baz = foo?.forEach(element => bar(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #7 +## Invalid #10 1 | foo.forEach(bar) > Error 1/1 @@ -74,7 +104,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #8 +## Invalid #11 + 1 | foo?.forEach(bar) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(bar)␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #12 1 | foo.forEach(async function(element) {}) > Error 1/1 @@ -84,7 +124,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #9 +## Invalid #13 + 1 | foo?.forEach(async function(element) {}) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(async function(element) {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #14 1 | foo.forEach(function * (element) {}) > Error 1/1 @@ -94,7 +144,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #10 +## Invalid #15 + 1 | foo?.forEach(function * (element) {}) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function * (element) {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #16 1 | foo.forEach(() => bar()) > Error 1/1 @@ -104,7 +164,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #11 +## Invalid #17 + 1 | foo?.forEach(() => bar()) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(() => bar())␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #18 1 | foo.forEach((element, index, array) => bar()) > Error 1/1 @@ -114,7 +184,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #12 +## Invalid #19 + 1 | foo?.forEach((element, index, array) => bar()) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element, index, array) => bar())␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #20 1 | property.forEach(({property}) => bar(property)) > Error 1/1 @@ -124,7 +204,37 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #13 +## Invalid #21 + 1 | property?.forEach(({property}) => bar(property)) + +> Error 1/1 + + `␊ + > 1 | property?.forEach(({property}) => bar(property))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #22 + 1 | () => foo.forEach() + +> Error 1/1 + + `␊ + > 1 | () => foo.forEach()␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #23 + 1 | () => foo?.forEach() + +> Error 1/1 + + `␊ + > 1 | () => foo?.forEach()␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #24 1 | foo.forEach(element => { 2 | do { 3 | return @@ -142,7 +252,25 @@ Generated by [AVA](https://avajs.dev). 5 | });␊ ` -## Invalid #14 +## Invalid #25 + 1 | foo?.forEach(element => { + 2 | do { + 3 | return + 4 | } while (element) + 5 | }); + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | do {␊ + 3 | return␊ + 4 | } while (element)␊ + 5 | });␊ + ` + +## Invalid #26 1 | foo.forEach(element => { 2 | while (element) { 3 | return; @@ -160,7 +288,25 @@ Generated by [AVA](https://avajs.dev). 5 | });␊ ` -## Invalid #15 +## Invalid #27 + 1 | foo?.forEach(element => { + 2 | while (element) { + 3 | return; + 4 | } + 5 | }); + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | while (element) {␊ + 3 | return;␊ + 4 | }␊ + 5 | });␊ + ` + +## Invalid #28 1 | foo.forEach(element => { 2 | for (let i = 0; i < 2; i++) { 3 | return; @@ -178,7 +324,25 @@ Generated by [AVA](https://avajs.dev). 5 | });␊ ` -## Invalid #16 +## Invalid #29 + 1 | foo?.forEach(element => { + 2 | for (let i = 0; i < 2; i++) { + 3 | return; + 4 | } + 5 | }); + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | for (let i = 0; i < 2; i++) {␊ + 3 | return;␊ + 4 | }␊ + 5 | });␊ + ` + +## Invalid #30 1 | foo.forEach(element => { 2 | for (let i in element) { 3 | return; @@ -196,7 +360,25 @@ Generated by [AVA](https://avajs.dev). 5 | });␊ ` -## Invalid #17 +## Invalid #31 + 1 | foo?.forEach(element => { + 2 | for (let i in element) { + 3 | return; + 4 | } + 5 | }); + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | for (let i in element) {␊ + 3 | return;␊ + 4 | }␊ + 5 | });␊ + ` + +## Invalid #32 1 | foo.forEach(element => { 2 | for (let i of element) { 3 | return; @@ -214,7 +396,25 @@ Generated by [AVA](https://avajs.dev). 5 | });␊ ` -## Invalid #18 +## Invalid #33 + 1 | foo?.forEach(element => { + 2 | for (let i of element) { + 3 | return; + 4 | } + 5 | }); + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | for (let i of element) {␊ + 3 | return;␊ + 4 | }␊ + 5 | });␊ + ` + +## Invalid #34 1 | foo.forEach(element => { 2 | switch (element) { 3 | case 1: @@ -251,7 +451,44 @@ Generated by [AVA](https://avajs.dev). 8 | });␊ ` -## Invalid #19 +## Invalid #35 + 1 | foo?.forEach(element => { + 2 | switch (element) { + 3 | case 1: + 4 | break; + 5 | case 2: + 6 | return; + 7 | } + 8 | }); + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | switch (element) {␊ + 3 | case 1:␊ + 4 | break;␊ + 5 | case 2:␊ + 6 | continue;␊ + 7 | }␊ + 8 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | switch (element) {␊ + 3 | case 1:␊ + 4 | break;␊ + 5 | case 2:␊ + 6 | return;␊ + 7 | }␊ + 8 | });␊ + ` + +## Invalid #36 1 | foo.forEach(foo => bar()); > Error 1/1 @@ -261,7 +498,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #20 +## Invalid #37 + 1 | foo?.forEach(foo => bar()); + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(foo => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #38 1 | const foo = []; 2 | foo.forEach(foo => bar()); @@ -273,7 +520,19 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #21 +## Invalid #39 + 1 | const foo = []; + 2 | foo?.forEach(foo => bar()); + +> Error 1/1 + + `␊ + 1 | const foo = [];␊ + > 2 | foo?.forEach(foo => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #40 1 | const foo = []; 2 | function unicorn() { 3 | foo.forEach(foo => bar()); @@ -289,8 +548,24 @@ Generated by [AVA](https://avajs.dev). 4 | }␊ ` -## Invalid #22 - 1 | index.forEach((a, index) => bar()); +## Invalid #41 + 1 | const foo = []; + 2 | function unicorn() { + 3 | foo?.forEach(foo => bar()); + 4 | } + +> Error 1/1 + + `␊ + 1 | const foo = [];␊ + 2 | function unicorn() {␊ + > 3 | foo?.forEach(foo => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 4 | }␊ + ` + +## Invalid #42 + 1 | index.forEach((a, index) => bar()); > Error 1/1 @@ -299,7 +574,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #23 +## Invalid #43 + 1 | index?.forEach((a, index) => bar()); + +> Error 1/1 + + `␊ + > 1 | index?.forEach((a, index) => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #44 1 | const index = []; 2 | index.forEach((a, index) => bar()); @@ -311,7 +596,19 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #24 +## Invalid #45 + 1 | const index = []; + 2 | index?.forEach((a, index) => bar()); + +> Error 1/1 + + `␊ + 1 | const index = [];␊ + > 2 | index?.forEach((a, index) => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #46 1 | const index = []; 2 | function unicorn() { 3 | index.forEach((a, index) => bar()); @@ -327,7 +624,23 @@ Generated by [AVA](https://avajs.dev). 4 | }␊ ` -## Invalid #25 +## Invalid #47 + 1 | const index = []; + 2 | function unicorn() { + 3 | index?.forEach((a, index) => bar()); + 4 | } + +> Error 1/1 + + `␊ + 1 | const index = [];␊ + 2 | function unicorn() {␊ + > 3 | index?.forEach((a, index) => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 4 | }␊ + ` + +## Invalid #48 1 | a[foo].forEach(foo => bar()); > Error 1/1 @@ -337,7 +650,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #26 +## Invalid #49 + 1 | a[foo]?.forEach(foo => bar()); + +> Error 1/1 + + `␊ + > 1 | a[foo]?.forEach(foo => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #50 1 | const foo = 1; 2 | a[foo].forEach(foo => bar()); @@ -349,7 +672,19 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #27 +## Invalid #51 + 1 | const foo = 1; + 2 | a[foo]?.forEach(foo => bar()); + +> Error 1/1 + + `␊ + 1 | const foo = 1;␊ + > 2 | a[foo]?.forEach(foo => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #52 1 | const foo = 1; 2 | function unicorn() { 3 | a[foo].forEach(foo => bar()); @@ -365,7 +700,23 @@ Generated by [AVA](https://avajs.dev). 4 | }␊ ` -## Invalid #28 +## Invalid #53 + 1 | const foo = 1; + 2 | function unicorn() { + 3 | a[foo]?.forEach(foo => bar()); + 4 | } + +> Error 1/1 + + `␊ + 1 | const foo = 1;␊ + 2 | function unicorn() {␊ + > 3 | a[foo]?.forEach(foo => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 4 | }␊ + ` + +## Invalid #54 1 | a[index].forEach((b, index) => bar()); > Error 1/1 @@ -375,7 +726,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #29 +## Invalid #55 + 1 | a[index]?.forEach((b, index) => bar()); + +> Error 1/1 + + `␊ + > 1 | a[index]?.forEach((b, index) => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #56 1 | a((foo) => foo).forEach(foo => bar()); > Output @@ -391,7 +752,21 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #30 +## Invalid #57 + 1 | a((foo) => foo)?.forEach(foo => bar()); + +> Error 1/1 + + `␊ + > 1 | a((foo) => foo)?.forEach(foo => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/1: Switch to \`for…of\`.␊ + 1 | if (a((foo) => foo)) for (const foo of a((foo) => foo)) bar();␊ + ` + +## Invalid #58 1 | a((foo, index) => foo + index).forEach((foo, index) => bar()); > Output @@ -407,7 +782,21 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #31 +## Invalid #59 + 1 | a((foo, index) => foo + index)?.forEach((foo, index) => bar()); + +> Error 1/1 + + `␊ + > 1 | a((foo, index) => foo + index)?.forEach((foo, index) => bar());␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + 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();␊ + ` + +## Invalid #60 1 | const foo = []; 2 | const index = 1; 3 | a.forEach((foo, index) => foo[index]); @@ -429,7 +818,29 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #32 +## Invalid #61 + 1 | const foo = []; + 2 | const index = 1; + 3 | a?.forEach((foo, index) => foo[index]); + +> Output + + `␊ + 1 | const foo = [];␊ + 2 | const index = 1;␊ + 3 | if (a) for (const [index, foo] of a.entries()) foo[index];␊ + ` + +> Error 1/1 + + `␊ + 1 | const foo = [];␊ + 2 | const index = 1;␊ + > 3 | a?.forEach((foo, index) => foo[index]);␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #62 1 | foo.forEach(function a(element) { 2 | bar(a) 3 | }) @@ -443,7 +854,21 @@ Generated by [AVA](https://avajs.dev). 3 | })␊ ` -## Invalid #33 +## Invalid #63 + 1 | foo?.forEach(function a(element) { + 2 | bar(a) + 3 | }) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function a(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(a)␊ + 3 | })␊ + ` + +## Invalid #64 1 | foo.forEach(function a(element) { 2 | function b() { 3 | bar(a) @@ -461,7 +886,25 @@ Generated by [AVA](https://avajs.dev). 5 | })␊ ` -## Invalid #34 +## Invalid #65 + 1 | foo?.forEach(function a(element) { + 2 | function b() { + 3 | bar(a) + 4 | } + 5 | }) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function a(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | function b() {␊ + 3 | bar(a)␊ + 4 | }␊ + 5 | })␊ + ` + +## Invalid #66 1 | foo.forEach(function a(element) { 2 | function b(a) { 3 | bar(a) @@ -489,7 +932,35 @@ Generated by [AVA](https://avajs.dev). 5 | })␊ ` -## Invalid #35 +## Invalid #67 + 1 | foo?.forEach(function a(element) { + 2 | function b(a) { + 3 | bar(a) + 4 | } + 5 | }) + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | function b(a) {␊ + 3 | bar(a)␊ + 4 | }␊ + 5 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function a(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | function b(a) {␊ + 3 | bar(a)␊ + 4 | }␊ + 5 | })␊ + ` + +## Invalid #68 1 | foo.forEach(function(element) { 2 | bar(this) 3 | }) @@ -503,7 +974,21 @@ Generated by [AVA](https://avajs.dev). 3 | })␊ ` -## Invalid #36 +## Invalid #69 + 1 | foo?.forEach(function(element) { + 2 | bar(this) + 3 | }) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(this)␊ + 3 | })␊ + ` + +## Invalid #70 1 | foo.forEach(function(element) { 2 | function b() { 3 | bar(this) @@ -531,9 +1016,9 @@ Generated by [AVA](https://avajs.dev). 5 | })␊ ` -## Invalid #37 - 1 | foo.forEach(function(element) { - 2 | const x = b => { +## Invalid #71 + 1 | foo?.forEach(function(element) { + 2 | function b() { 3 | bar(this) 4 | } 5 | }) @@ -541,8 +1026,8 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo) {␊ - 2 | const x = b => {␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | function b() {␊ 3 | bar(this)␊ 4 | }␊ 5 | }␊ @@ -551,42 +1036,120 @@ Generated by [AVA](https://avajs.dev). > Error 1/1 `␊ - > 1 | foo.forEach(function(element) {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | const x = b => {␊ + > 1 | foo?.forEach(function(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | function b() {␊ 3 | bar(this)␊ 4 | }␊ 5 | })␊ ` -## Invalid #38 - 1 | foo.forEach((element) => { - 2 | bar(this) - 3 | }) +## Invalid #72 + 1 | foo.forEach(function(element) { + 2 | const x = b => { + 3 | bar(this) + 4 | } + 5 | }) > Output `␊ 1 | for (const element of foo) {␊ - 2 | bar(this)␊ - 3 | }␊ + 2 | const x = b => {␊ + 3 | bar(this)␊ + 4 | }␊ + 5 | }␊ ` > Error 1/1 `␊ - > 1 | foo.forEach((element) => {␊ + > 1 | foo.forEach(function(element) {␊ | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | bar(this)␊ - 3 | })␊ + 2 | const x = b => {␊ + 3 | bar(this)␊ + 4 | }␊ + 5 | })␊ ` -## Invalid #39 - 1 | foo.forEach(function(element) { - 2 | bar(arguments) - 3 | }) +## Invalid #73 + 1 | foo?.forEach(function(element) { + 2 | const x = b => { + 3 | bar(this) + 4 | } + 5 | }) -> Error 1/1 +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | const x = b => {␊ + 3 | bar(this)␊ + 4 | }␊ + 5 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | const x = b => {␊ + 3 | bar(this)␊ + 4 | }␊ + 5 | })␊ + ` + +## Invalid #74 + 1 | foo.forEach((element) => { + 2 | bar(this) + 3 | }) + +> Output + + `␊ + 1 | for (const element of foo) {␊ + 2 | bar(this)␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(this)␊ + 3 | })␊ + ` + +## Invalid #75 + 1 | foo?.forEach((element) => { + 2 | bar(this) + 3 | }) + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | bar(this)␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(this)␊ + 3 | })␊ + ` + +## Invalid #76 + 1 | foo.forEach(function(element) { + 2 | bar(arguments) + 3 | }) + +> Error 1/1 `␊ > 1 | foo.forEach(function(element) {␊ @@ -595,7 +1158,21 @@ Generated by [AVA](https://avajs.dev). 3 | })␊ ` -## Invalid #40 +## Invalid #77 + 1 | foo?.forEach(function(element) { + 2 | bar(arguments) + 3 | }) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(arguments)␊ + 3 | })␊ + ` + +## Invalid #78 1 | foo.forEach(function(element) { 2 | function b() { 3 | bar(arguments) @@ -615,295 +1192,803 @@ Generated by [AVA](https://avajs.dev). > Error 1/1 `␊ - > 1 | foo.forEach(function(element) {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | function b() {␊ - 3 | bar(arguments)␊ - 4 | }␊ - 5 | })␊ + > 1 | foo.forEach(function(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | function b() {␊ + 3 | bar(arguments)␊ + 4 | }␊ + 5 | })␊ + ` + +## Invalid #79 + 1 | foo?.forEach(function(element) { + 2 | function b() { + 3 | bar(arguments) + 4 | } + 5 | }) + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | function b() {␊ + 3 | bar(arguments)␊ + 4 | }␊ + 5 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | function b() {␊ + 3 | bar(arguments)␊ + 4 | }␊ + 5 | })␊ + ` + +## Invalid #80 + 1 | foo.forEach(function(element) { + 2 | const b = () => { + 3 | bar(arguments) + 4 | } + 5 | }) + +> Output + + `␊ + 1 | for (const element of foo) {␊ + 2 | const b = () => {␊ + 3 | bar(arguments)␊ + 4 | }␊ + 5 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach(function(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | const b = () => {␊ + 3 | bar(arguments)␊ + 4 | }␊ + 5 | })␊ + ` + +## Invalid #81 + 1 | foo?.forEach(function(element) { + 2 | const b = () => { + 3 | bar(arguments) + 4 | } + 5 | }) + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | const b = () => {␊ + 3 | bar(arguments)␊ + 4 | }␊ + 5 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | const b = () => {␊ + 3 | bar(arguments)␊ + 4 | }␊ + 5 | })␊ + ` + +## Invalid #82 + 1 | foo.forEach((element) => { + 2 | bar(arguments) + 3 | }) + +> Output + + `␊ + 1 | for (const element of foo) {␊ + 2 | bar(arguments)␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(arguments)␊ + 3 | })␊ + ` + +## Invalid #83 + 1 | foo?.forEach((element) => { + 2 | bar(arguments) + 3 | }) + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | bar(arguments)␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(arguments)␊ + 3 | })␊ + ` + +## Invalid #84 + 1 | a = foo?.bar.forEach((element) => bar(element)); + +> Error 1/1 + + `␊ + > 1 | a = foo?.bar.forEach((element) => bar(element));␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #85 + 1 | a = foo?.bar?.forEach((element) => bar(element)); + +> Error 1/1 + + `␊ + > 1 | a = foo?.bar?.forEach((element) => bar(element));␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #86 + 1 | foo.forEach(function (element) { + 2 | bar(element); + 3 | }); + +> Output + + `␊ + 1 | for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach(function (element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | });␊ + ` + +## Invalid #87 + 1 | foo?.forEach(function (element) { + 2 | bar(element); + 3 | }); + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function (element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | });␊ + ` + +## Invalid #88 + 1 | foo.forEach(function withName(element) { + 2 | bar(element); + 3 | }); + +> Output + + `␊ + 1 | for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach(function withName(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | });␊ + ` + +## Invalid #89 + 1 | foo?.forEach(function withName(element) { + 2 | bar(element); + 3 | }); + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function withName(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | });␊ + ` + +## Invalid #90 + 1 | foo.forEach((element) => { + 2 | bar(element); + 3 | }); + +> Output + + `␊ + 1 | for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | });␊ + ` + +## Invalid #91 + 1 | foo?.forEach((element) => { + 2 | bar(element); + 3 | }); + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | });␊ + ` + +## Invalid #92 + 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(…)\`.␊ + ` + +## Invalid #93 + 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(…)\`.␊ + ` + +## Invalid #94 + 1 | foo.forEach(function (element, index) { + 2 | bar(element, index); + 3 | }); + +> Output + + `␊ + 1 | for (const [index, element] of foo.entries()) {␊ + 2 | bar(element, index);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach(function (element, index) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element, index);␊ + 3 | });␊ + ` + +## Invalid #95 + 1 | foo?.forEach(function (element, index) { + 2 | bar(element, index); + 3 | }); + +> Output + + `␊ + 1 | if (foo) for (const [index, element] of foo.entries()) {␊ + 2 | bar(element, index);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function (element, index) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element, index);␊ + 3 | });␊ + ` + +## Invalid #96 + 1 | foo.forEach(function withName(element, index) { + 2 | bar(element, index); + 3 | }); + +> Output + + `␊ + 1 | for (const [index, element] of foo.entries()) {␊ + 2 | bar(element, index);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach(function withName(element, index) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element, index);␊ + 3 | });␊ + ` + +## Invalid #97 + 1 | foo?.forEach(function withName(element, index) { + 2 | bar(element, index); + 3 | }); + +> Output + + `␊ + 1 | if (foo) for (const [index, element] of foo.entries()) {␊ + 2 | bar(element, index);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function withName(element, index) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element, index);␊ + 3 | });␊ + ` + +## Invalid #98 + 1 | foo.forEach((element, index) => { + 2 | bar(element, index); + 3 | }); + +> Output + + `␊ + 1 | for (const [index, element] of foo.entries()) {␊ + 2 | bar(element, index);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach((element, index) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element, index);␊ + 3 | });␊ + ` + +## Invalid #99 + 1 | foo?.forEach((element, index) => { + 2 | bar(element, index); + 3 | }); + +> Output + + `␊ + 1 | if (foo) for (const [index, element] of foo.entries()) {␊ + 2 | bar(element, index);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element, index) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element, index);␊ + 3 | });␊ + ` + +## Invalid #100 + 1 | foo.forEach((element, index) => bar(element, index)); + +> Output + + `␊ + 1 | for (const [index, element] of foo.entries()) bar(element, index);␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach((element, index) => bar(element, index));␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #101 + 1 | foo?.forEach((element, index) => bar(element, index)); + +> Output + + `␊ + 1 | if (foo) for (const [index, element] of foo.entries()) bar(element, index);␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element, index) => bar(element, index));␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #102 + 1 | foo?.bar.forEach((element) => bar(element)); + +> Output + + `␊ + 1 | for (const element of foo?.bar) bar(element);␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.bar.forEach((element) => bar(element));␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #103 + 1 | foo?.bar?.forEach((element) => bar(element)); + +> Output + + `␊ + 1 | if (foo?.bar) for (const element of foo?.bar) bar(element);␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.bar?.forEach((element) => bar(element));␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #104 + 1 | foo.bar.forEach((element) => log(element)) + +> Output + + `␊ + 1 | for (const element of foo.bar) log(element)␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.bar.forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #41 - 1 | foo.forEach(function(element) { - 2 | const b = () => { - 3 | bar(arguments) - 4 | } - 5 | }) +## Invalid #105 + 1 | foo.bar?.forEach((element) => log(element)) > Output `␊ - 1 | for (const element of foo) {␊ - 2 | const b = () => {␊ - 3 | bar(arguments)␊ - 4 | }␊ - 5 | }␊ + 1 | if (foo.bar) for (const element of foo.bar) log(element)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach(function(element) {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | const b = () => {␊ - 3 | bar(arguments)␊ - 4 | }␊ - 5 | })␊ + > 1 | foo.bar?.forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #42 - 1 | foo.forEach((element) => { - 2 | bar(arguments) - 3 | }) +## Invalid #106 + 1 | foo.bar().forEach((element) => log(element)) > Output `␊ - 1 | for (const element of foo) {␊ - 2 | bar(arguments)␊ - 3 | }␊ + 1 | for (const element of foo.bar()) log(element)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach((element) => {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | bar(arguments)␊ - 3 | })␊ + > 1 | foo.bar().forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #43 - 1 | a = foo?.bar.forEach((element) => bar(element)); +## Invalid #107 + 1 | foo.bar()?.forEach((element) => log(element)) > Error 1/1 `␊ - > 1 | a = foo?.bar.forEach((element) => bar(element));␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | foo.bar()?.forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/1: Switch to \`for…of\`.␊ + 1 | if (foo.bar()) for (const element of foo.bar()) log(element)␊ ` -## Invalid #44 - 1 | foo.forEach(function (element) { - 2 | bar(element); - 3 | }); +## Invalid #108 + 1 | (a ? b : c).forEach((element) => log(element)) > Output `␊ - 1 | for (const element of foo) {␊ - 2 | bar(element);␊ - 3 | }␊ + 1 | for (const element of (a ? b : c)) log(element)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach(function (element) {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | bar(element);␊ - 3 | });␊ + > 1 | (a ? b : c).forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #45 - 1 | foo.forEach(function withName(element) { - 2 | bar(element); - 3 | }); +## Invalid #109 + 1 | (a ? b : c)?.forEach((element) => log(element)) > Output `␊ - 1 | for (const element of foo) {␊ - 2 | bar(element);␊ - 3 | }␊ + 1 | if (a ? b : c) for (const element of (a ? b : c)) log(element)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach(function withName(element) {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | bar(element);␊ - 3 | });␊ + > 1 | (a ? b : c)?.forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #46 - 1 | foo.forEach((element) => { - 2 | bar(element); - 3 | }); +## Invalid #110 + 1 | (a ? b : c()).forEach((element) => log(element)) > Output `␊ - 1 | for (const element of foo) {␊ - 2 | bar(element);␊ - 3 | }␊ + 1 | for (const element of (a ? b : c())) log(element)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach((element) => {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | bar(element);␊ - 3 | });␊ + > 1 | (a ? b : c()).forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #47 - 1 | foo.forEach((element) => bar(element)); +## Invalid #111 + 1 | (a ? b : c())?.forEach((element) => log(element)) + +> Error 1/1 + + `␊ + > 1 | (a ? b : c())?.forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/1: Switch to \`for…of\`.␊ + 1 | if (a ? b : c()) for (const element of (a ? b : c())) log(element)␊ + ` + +## Invalid #112 + 1 | (foo || bar).forEach((element) => log(element)) > Output `␊ - 1 | for (const element of foo) bar(element);␊ + 1 | for (const element of (foo || bar)) log(element)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach((element) => bar(element));␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | (foo || bar).forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #48 - 1 | foo.forEach(function (element, index) { - 2 | bar(element, index); - 3 | }); +## Invalid #113 + 1 | (foo || bar)?.forEach((element) => log(element)) > Output `␊ - 1 | for (const [index, element] of foo.entries()) {␊ - 2 | bar(element, index);␊ - 3 | }␊ + 1 | if (foo || bar) for (const element of (foo || bar)) log(element)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach(function (element, index) {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | bar(element, index);␊ - 3 | });␊ + > 1 | (foo || bar)?.forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #49 - 1 | foo.forEach(function withName(element, index) { - 2 | bar(element, index); - 3 | }); +## Invalid #114 + 1 | (foo || bar()).forEach((element) => log(element)) > Output `␊ - 1 | for (const [index, element] of foo.entries()) {␊ - 2 | bar(element, index);␊ - 3 | }␊ + 1 | for (const element of (foo || bar())) log(element)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach(function withName(element, index) {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | bar(element, index);␊ - 3 | });␊ + > 1 | (foo || bar()).forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #50 - 1 | foo.forEach((element, index) => { - 2 | bar(element, index); - 3 | }); +## Invalid #115 + 1 | (foo || bar())?.forEach((element) => log(element)) + +> Error 1/1 + + `␊ + > 1 | (foo || bar())?.forEach((element) => log(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/1: Switch to \`for…of\`.␊ + 1 | if (foo || bar()) for (const element of (foo || bar())) log(element)␊ + ` + +## Invalid #116 + 1 | (foo).forEach((element, index) => bar(element, index)) > Output `␊ - 1 | for (const [index, element] of foo.entries()) {␊ - 2 | bar(element, index);␊ - 3 | }␊ + 1 | for (const [index, element] of (foo).entries()) bar(element, index)␊ ` > Error 1/1 `␊ - > 1 | foo.forEach((element, index) => {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | bar(element, index);␊ - 3 | });␊ + > 1 | (foo).forEach((element, index) => bar(element, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #51 - 1 | foo.forEach((element, index) => bar(element, index)); +## Invalid #117 + 1 | (foo)?.forEach((element, index) => bar(element, index)) > Output `␊ - 1 | 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 `␊ - > 1 | foo.forEach((element, index) => bar(element, index));␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | (foo)?.forEach((element, index) => bar(element, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #52 - 1 | foo?.bar.forEach((element) => bar(element)); +## Invalid #118 + 1 | (0, foo).forEach((element, index) => bar(element, index)) > Output `␊ - 1 | for (const element of foo?.bar) bar(element);␊ + 1 | for (const [index, element] of (0, foo).entries()) bar(element, index)␊ ` > Error 1/1 `␊ - > 1 | foo?.bar.forEach((element) => bar(element));␊ + > 1 | (0, foo).forEach((element, index) => bar(element, index))␊ | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #53 - 1 | (foo).forEach((element, index) => bar(element, index)) +## Invalid #119 + 1 | (0, foo)?.forEach((element, index) => bar(element, index)) > Output `␊ - 1 | for (const [index, element] of (foo).entries()) bar(element, index)␊ + 1 | if (0, foo) for (const [index, element] of (0, foo).entries()) bar(element, index)␊ ` > Error 1/1 `␊ - > 1 | (foo).forEach((element, index) => bar(element, index))␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | (0, foo)?.forEach((element, index) => bar(element, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #54 - 1 | (0, foo).forEach((element, index) => bar(element, index)) +## Invalid #120 + 1 | foo.forEach(function (element) { + 2 | bar(element); + 3 | },); > Output `␊ - 1 | for (const [index, element] of (0, foo).entries()) bar(element, index)␊ + 1 | for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ ` > Error 1/1 `␊ - > 1 | (0, foo).forEach((element, index) => bar(element, index))␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | foo.forEach(function (element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | },);␊ ` -## Invalid #55 - 1 | foo.forEach(function (element) { +## Invalid #121 + 1 | foo?.forEach(function (element) { 2 | bar(element); 3 | },); > Output `␊ - 1 | for (const element of foo) {␊ + 1 | if (foo) for (const element of foo) {␊ 2 | bar(element);␊ 3 | }␊ ` @@ -911,13 +1996,13 @@ Generated by [AVA](https://avajs.dev). > Error 1/1 `␊ - > 1 | foo.forEach(function (element) {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | foo?.forEach(function (element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ 2 | bar(element);␊ 3 | },);␊ ` -## Invalid #56 +## Invalid #122 1 | foo.forEach(function withName(element) { 2 | bar(element); 3 | },); @@ -939,7 +2024,29 @@ Generated by [AVA](https://avajs.dev). 3 | },);␊ ` -## Invalid #57 +## Invalid #123 + 1 | foo?.forEach(function withName(element) { + 2 | bar(element); + 3 | },); + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function withName(element) {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | },);␊ + ` + +## Invalid #124 1 | foo.forEach((element) => { 2 | bar(element); 3 | },); @@ -961,7 +2068,29 @@ Generated by [AVA](https://avajs.dev). 3 | },);␊ ` -## Invalid #58 +## Invalid #125 + 1 | foo?.forEach((element) => { + 2 | bar(element); + 3 | },); + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | bar(element);␊ + 3 | },);␊ + ` + +## Invalid #126 1 | foo.forEach((element) => bar(element),); > Output @@ -977,27 +2106,99 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #59 +## Invalid #127 + 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(…)\`.␊ + ` + +## Invalid #128 1 | foo.forEach((element) => bar(element)) 2 | ;[foo].pop(); > Output `␊ - 1 | for (const element of foo) bar(element)␊ - 2 | ;[foo].pop();␊ + 1 | for (const element of foo) bar(element)␊ + 2 | ;[foo].pop();␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach((element) => bar(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | ;[foo].pop();␊ + ` + +## Invalid #129 + 1 | foo?.forEach((element) => bar(element)) + 2 | ;[foo].pop(); + +> Output + + `␊ + 1 | if (foo) for (const element of foo) bar(element)␊ + 2 | ;[foo].pop();␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element) => bar(element))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | ;[foo].pop();␊ + ` + +## Invalid #130 + 1 | foo.forEach((element) => { + 2 | bar(element); + 3 | }); + 4 | function noneRelatedFunction() { + 5 | while (element) { + 6 | return; + 7 | } + 8 | } + +> Output + + `␊ + 1 | for (const element of foo) {␊ + 2 | bar(element);␊ + 3 | }␊ + 4 | function noneRelatedFunction() {␊ + 5 | while (element) {␊ + 6 | return;␊ + 7 | }␊ + 8 | }␊ ` > Error 1/1 `␊ - > 1 | foo.forEach((element) => bar(element))␊ + > 1 | foo.forEach((element) => {␊ | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | ;[foo].pop();␊ + 2 | bar(element);␊ + 3 | });␊ + 4 | function noneRelatedFunction() {␊ + 5 | while (element) {␊ + 6 | return;␊ + 7 | }␊ + 8 | }␊ ` -## Invalid #60 - 1 | foo.forEach((element) => { +## Invalid #131 + 1 | foo?.forEach((element) => { 2 | bar(element); 3 | }); 4 | function noneRelatedFunction() { @@ -1009,7 +2210,7 @@ Generated by [AVA](https://avajs.dev). > Output `␊ - 1 | for (const element of foo) {␊ + 1 | if (foo) for (const element of foo) {␊ 2 | bar(element);␊ 3 | }␊ 4 | function noneRelatedFunction() {␊ @@ -1022,8 +2223,8 @@ Generated by [AVA](https://avajs.dev). > Error 1/1 `␊ - > 1 | foo.forEach((element) => {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | foo?.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ 2 | bar(element);␊ 3 | });␊ 4 | function noneRelatedFunction() {␊ @@ -1033,7 +2234,7 @@ Generated by [AVA](https://avajs.dev). 8 | }␊ ` -## Invalid #61 +## Invalid #132 1 | foo.forEach(element => ({})) > Output @@ -1049,7 +2250,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #62 +## Invalid #133 + 1 | foo?.forEach(element => ({})) + +> Output + + `␊ + 1 | if (foo) for (const element of foo) ({})␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => ({}))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #134 1 | foo.forEach((((((element => bar(element))))))); > Output @@ -1065,7 +2282,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #63 +## Invalid #135 + 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(…)\`.␊ + ` + +## Invalid #136 1 | foo.forEach((element) => { 2 | if (1) { 3 | return; @@ -1201,7 +2434,143 @@ Generated by [AVA](https://avajs.dev). 41 | });␊ ` -## Invalid #64 +## Invalid #137 + 1 | foo?.forEach((element) => { + 2 | if (1) { + 3 | return; + 4 | } + 5 | if (1) { + 6 | return + 7 | } + 8 | if (1) { + 9 | return!true; + 10 | } + 11 | if (1) { + 12 | return!true + 13 | } + 14 | if (1) { + 15 | return bar(); + 16 | } + 17 | if (1) { + 18 | return bar() + 19 | unreachable(); + 20 | } + 21 | if (1) { + 22 | return {}; + 23 | } + 24 | if (1) { + 25 | return ({}); + 26 | } + 27 | if (1) { + 28 | return {a} = a; + 29 | } + 30 | if (1) { + 31 | return [a] = a; + 32 | } + 33 | if (1) { + 34 | foo + 35 | return [] + 36 | } + 37 | if (1) { + 38 | foo + 39 | return [foo] = bar; + 40 | } + 41 | }); + +> Output + + `␊ + 1 | if (foo) for (const element of foo) {␊ + 2 | if (1) {␊ + 3 | continue;␊ + 4 | }␊ + 5 | if (1) {␊ + 6 | continue␊ + 7 | }␊ + 8 | if (1) {␊ + 9 | !true; continue;␊ + 10 | }␊ + 11 | if (1) {␊ + 12 | !true; continue;␊ + 13 | }␊ + 14 | if (1) {␊ + 15 | bar(); continue;␊ + 16 | }␊ + 17 | if (1) {␊ + 18 | bar(); continue;␊ + 19 | unreachable();␊ + 20 | }␊ + 21 | if (1) {␊ + 22 | ({}); continue;␊ + 23 | }␊ + 24 | if (1) {␊ + 25 | ({}); continue;␊ + 26 | }␊ + 27 | if (1) {␊ + 28 | ({a} = a); continue;␊ + 29 | }␊ + 30 | if (1) {␊ + 31 | ([a] = a); continue;␊ + 32 | }␊ + 33 | if (1) {␊ + 34 | foo␊ + 35 | ;[]; continue;␊ + 36 | }␊ + 37 | if (1) {␊ + 38 | foo␊ + 39 | ;([foo] = bar); continue;␊ + 40 | }␊ + 41 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | if (1) {␊ + 3 | return;␊ + 4 | }␊ + 5 | if (1) {␊ + 6 | return␊ + 7 | }␊ + 8 | if (1) {␊ + 9 | return!true;␊ + 10 | }␊ + 11 | if (1) {␊ + 12 | return!true␊ + 13 | }␊ + 14 | if (1) {␊ + 15 | return bar();␊ + 16 | }␊ + 17 | if (1) {␊ + 18 | return bar()␊ + 19 | unreachable();␊ + 20 | }␊ + 21 | if (1) {␊ + 22 | return {};␊ + 23 | }␊ + 24 | if (1) {␊ + 25 | return ({});␊ + 26 | }␊ + 27 | if (1) {␊ + 28 | return {a} = a;␊ + 29 | }␊ + 30 | if (1) {␊ + 31 | return [a] = a;␊ + 32 | }␊ + 33 | if (1) {␊ + 34 | foo␊ + 35 | return []␊ + 36 | }␊ + 37 | if (1) {␊ + 38 | foo␊ + 39 | return [foo] = bar;␊ + 40 | }␊ + 41 | });␊ + ` + +## Invalid #138 1 | node.children.index.forEach((children, index) => process(children, index)) > Output @@ -1217,7 +2586,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #65 +## Invalid #139 + 1 | node.children.index?.forEach((children, index) => process(children, index)) + +> Output + + `␊ + 1 | if (node.children.index) for (const [index, children] of node.children.index.entries()) process(children, index)␊ + ` + +> Error 1/1 + + `␊ + > 1 | node.children.index?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #140 1 | (node?.children?.index).forEach((children, index) => process(children, index)) > Output @@ -1233,7 +2618,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #66 +## Invalid #141 + 1 | (node?.children?.index)?.forEach((children, index) => process(children, index)) + +> Output + + `␊ + 1 | if (node?.children?.index) for (const [index, children] of (node?.children?.index).entries()) process(children, index)␊ + ` + +> Error 1/1 + + `␊ + > 1 | (node?.children?.index)?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #142 1 | node[children].index.forEach((children, index) => process(children, index)) > Error 1/1 @@ -1243,7 +2644,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #67 +## Invalid #143 + 1 | node[children].index?.forEach((children, index) => process(children, index)) + +> Error 1/1 + + `␊ + > 1 | node[children].index?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #144 1 | (node.children?.[index]).forEach((children, index) => process(children, index)) > Error 1/1 @@ -1253,7 +2664,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #68 +## Invalid #145 + 1 | (node.children?.[index])?.forEach((children, index) => process(children, index)) + +> Error 1/1 + + `␊ + > 1 | (node.children?.[index])?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #146 1 | [{children: 1, index: 1}].forEach((children, index) => process(children, index)) > Output @@ -1265,21 +2686,47 @@ Generated by [AVA](https://avajs.dev). > Error 1/1 `␊ - > 1 | [{children: 1, index: 1}].forEach((children, index) => process(children, index))␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | [{children: 1, index: 1}].forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #147 + 1 | [{children: 1, index: 1}]?.forEach((children, index) => process(children, index)) + +> Output + + `␊ + 1 | if ([{children: 1, index: 1}]) for (const [index, children] of [{children: 1, index: 1}].entries()) process(children, index)␊ + ` + +> Error 1/1 + + `␊ + > 1 | [{children: 1, index: 1}]?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #148 + 1 | [{[children]: 1, index: 1}].forEach((children, index) => process(children, index)) + +> Error 1/1 + + `␊ + > 1 | [{[children]: 1, index: 1}].forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #69 - 1 | [{[children]: 1, index: 1}].forEach((children, index) => process(children, index)) +## Invalid #149 + 1 | [{[children]: 1, index: 1}]?.forEach((children, index) => process(children, index)) > Error 1/1 `␊ - > 1 | [{[children]: 1, index: 1}].forEach((children, index) => process(children, index))␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | [{[children]: 1, index: 1}]?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #70 +## Invalid #150 1 | [{[children]: 1, [index]: 1}].forEach((children, index) => process(children, index)) > Error 1/1 @@ -1289,7 +2736,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #71 +## Invalid #151 + 1 | [{[children]: 1, [index]: 1}]?.forEach((children, index) => process(children, index)) + +> Error 1/1 + + `␊ + > 1 | [{[children]: 1, [index]: 1}]?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #152 1 | [{children, index: 1}].forEach((children, index) => process(children, index)) > Error 1/1 @@ -1299,7 +2756,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #72 +## Invalid #153 + 1 | [{children, index: 1}]?.forEach((children, index) => process(children, index)) + +> Error 1/1 + + `␊ + > 1 | [{children, index: 1}]?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #154 1 | [{children: 1, index}].forEach((children, index) => process(children, index)) > Error 1/1 @@ -1309,7 +2776,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #73 +## Invalid #155 + 1 | [{children: 1, index}]?.forEach((children, index) => process(children, index)) + +> Error 1/1 + + `␊ + > 1 | [{children: 1, index}]?.forEach((children, index) => process(children, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #156 1 | [function name() {}].forEach((name, index) => process(name, index)) > Output @@ -1325,7 +2802,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #74 +## Invalid #157 + 1 | [function name() {}]?.forEach((name, index) => process(name, index)) + +> Output + + `␊ + 1 | if ([function name() {}]) for (const [index, name] of [function name() {}].entries()) process(name, index)␊ + ` + +> Error 1/1 + + `␊ + > 1 | [function name() {}]?.forEach((name, index) => process(name, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #158 1 | [ 2 | function () { 3 | function index() {} @@ -1353,7 +2846,39 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #75 +## Invalid #159 + 1 | [ + 2 | function () { + 3 | function index() {} + 4 | } + 5 | ]?.forEach((name, index) => process(name, index)) + +> Output + + `␊ + 1 | if ([␊ + 2 | function () {␊ + 3 | function index() {}␊ + 4 | }␊ + 5 | ]) for (const [index, name] of [␊ + 6 | function () {␊ + 7 | function index() {}␊ + 8 | }␊ + 9 | ].entries()) process(name, index)␊ + ` + +> Error 1/1 + + `␊ + 1 | [␊ + 2 | function () {␊ + 3 | function index() {}␊ + 4 | }␊ + > 5 | ]?.forEach((name, index) => process(name, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #160 1 | [ 2 | function () { 3 | class index {} @@ -1381,7 +2906,39 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #76 +## Invalid #161 + 1 | [ + 2 | function () { + 3 | class index {} + 4 | } + 5 | ]?.forEach((name, index) => process(name, index)) + +> Output + + `␊ + 1 | if ([␊ + 2 | function () {␊ + 3 | class index {}␊ + 4 | }␊ + 5 | ]) for (const [index, name] of [␊ + 6 | function () {␊ + 7 | class index {}␊ + 8 | }␊ + 9 | ].entries()) process(name, index)␊ + ` + +> Error 1/1 + + `␊ + 1 | [␊ + 2 | function () {␊ + 3 | class index {}␊ + 4 | }␊ + > 5 | ]?.forEach((name, index) => process(name, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #162 1 | [class Foo{}].forEach((Foo, index) => process(Foo, index)) > Output @@ -1397,7 +2954,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #77 +## Invalid #163 + 1 | [class Foo{}]?.forEach((Foo, index) => process(Foo, index)) + +> Output + + `␊ + 1 | if ([class Foo{}]) for (const [index, Foo] of [class Foo{}].entries()) process(Foo, index)␊ + ` + +> Error 1/1 + + `␊ + > 1 | [class Foo{}]?.forEach((Foo, index) => process(Foo, index))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #164 1 | [class Foo{}].forEach((X, Foo) => process(X, Foo)) > Output @@ -1413,7 +2986,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #78 +## Invalid #165 + 1 | [class Foo{}]?.forEach((X, Foo) => process(X, Foo)) + +> Output + + `␊ + 1 | if ([class Foo{}]) for (const [Foo, X] of [class Foo{}].entries()) process(X, Foo)␊ + ` + +> Error 1/1 + + `␊ + > 1 | [class Foo{}]?.forEach((X, Foo) => process(X, Foo))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #166 1 | [ 2 | class Foo { 3 | bar() {} @@ -1441,7 +3030,39 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #79 +## Invalid #167 + 1 | [ + 2 | class Foo { + 3 | bar() {} + 4 | } + 5 | ]?.forEach((Foo, bar) => process(Foo, bar)) + +> Output + + `␊ + 1 | if ([␊ + 2 | class Foo {␊ + 3 | bar() {}␊ + 4 | }␊ + 5 | ]) for (const [bar, Foo] of [␊ + 6 | class Foo {␊ + 7 | bar() {}␊ + 8 | }␊ + 9 | ].entries()) process(Foo, bar)␊ + ` + +> Error 1/1 + + `␊ + 1 | [␊ + 2 | class Foo {␊ + 3 | bar() {}␊ + 4 | }␊ + > 5 | ]?.forEach((Foo, bar) => process(Foo, bar))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #168 1 | foo.React.Children.forEach(bar) > Error 1/1 @@ -1451,7 +3072,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #80 +## Invalid #169 + 1 | foo.React.Children?.forEach(bar) + +> Error 1/1 + + `␊ + > 1 | foo.React.Children?.forEach(bar)␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #170 1 | NotReact.Children.forEach(bar) > Error 1/1 @@ -1461,7 +3092,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #81 +## Invalid #171 + 1 | NotReact.Children?.forEach(bar) + +> Error 1/1 + + `␊ + > 1 | NotReact.Children?.forEach(bar)␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #172 1 | React.NotChildren.forEach(bar) > Error 1/1 @@ -1471,7 +3112,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #82 +## Invalid #173 + 1 | React.NotChildren?.forEach(bar) + +> Error 1/1 + + `␊ + > 1 | React.NotChildren?.forEach(bar)␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #174 1 | React?.Children.forEach(bar) > Error 1/1 @@ -1481,7 +3132,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #83 +## Invalid #175 + 1 | React?.Children?.forEach(bar) + +> Error 1/1 + + `␊ + > 1 | React?.Children?.forEach(bar)␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #176 1 | NotChildren.forEach(bar) > Error 1/1 @@ -1491,7 +3152,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #84 +## Invalid #177 + 1 | NotChildren?.forEach(bar) + +> Error 1/1 + + `␊ + > 1 | NotChildren?.forEach(bar)␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #178 1 | foo.forEach(element => { 2 | element ++; 3 | }) @@ -1513,7 +3184,29 @@ Generated by [AVA](https://avajs.dev). 3 | })␊ ` -## Invalid #85 +## Invalid #179 + 1 | foo?.forEach(element => { + 2 | element ++; + 3 | }) + +> Output + + `␊ + 1 | if (foo) for (let element of foo) {␊ + 2 | element ++;␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | element ++;␊ + 3 | })␊ + ` + +## Invalid #180 1 | foo.forEach(element => { 2 | const a = -- element; 3 | }) @@ -1535,7 +3228,29 @@ Generated by [AVA](https://avajs.dev). 3 | })␊ ` -## Invalid #86 +## Invalid #181 + 1 | foo?.forEach(element => { + 2 | const a = -- element; + 3 | }) + +> Output + + `␊ + 1 | if (foo) for (let element of foo) {␊ + 2 | const a = -- element;␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(element => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | const a = -- element;␊ + 3 | })␊ + ` + +## Invalid #182 1 | foo.forEach((element, index) => { 2 | index ++; 3 | element = 2 @@ -1545,9 +3260,57 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | for (let [index, element] of foo.entries()) {␊ - 2 | index ++;␊ - 3 | element = 2␊ - 4 | }␊ + 2 | index ++;␊ + 3 | element = 2␊ + 4 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.forEach((element, index) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | index ++;␊ + 3 | element = 2␊ + 4 | });␊ + ` + +## Invalid #183 + 1 | foo?.forEach((element, index) => { + 2 | index ++; + 3 | element = 2 + 4 | }); + +> Output + + `␊ + 1 | if (foo) for (let [index, element] of foo.entries()) {␊ + 2 | index ++;␊ + 3 | element = 2␊ + 4 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element, index) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | index ++;␊ + 3 | element = 2␊ + 4 | });␊ + ` + +## Invalid #184 + 1 | foo.forEach((element, index) => { + 2 | element >>>= 2; + 3 | }); + +> Output + + `␊ + 1 | for (let [index, element] of foo.entries()) {␊ + 2 | element >>>= 2;␊ + 3 | }␊ ` > Error 1/1 @@ -1555,20 +3318,19 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo.forEach((element, index) => {␊ | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ - 2 | index ++;␊ - 3 | element = 2␊ - 4 | });␊ + 2 | element >>>= 2;␊ + 3 | });␊ ` -## Invalid #87 - 1 | foo.forEach((element, index) => { +## Invalid #185 + 1 | foo?.forEach((element, index) => { 2 | element >>>= 2; 3 | }); > Output `␊ - 1 | for (let [index, element] of foo.entries()) {␊ + 1 | if (foo) for (let [index, element] of foo.entries()) {␊ 2 | element >>>= 2;␊ 3 | }␊ ` @@ -1576,13 +3338,13 @@ Generated by [AVA](https://avajs.dev). > Error 1/1 `␊ - > 1 | foo.forEach((element, index) => {␊ - | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + > 1 | foo?.forEach((element, index) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ 2 | element >>>= 2;␊ 3 | });␊ ` -## Invalid #88 +## Invalid #186 1 | foo.forEach((element, index) => { 2 | const a = element = 1; 3 | }); @@ -1604,7 +3366,29 @@ Generated by [AVA](https://avajs.dev). 3 | });␊ ` -## Invalid #89 +## Invalid #187 + 1 | foo?.forEach((element, index) => { + 2 | const a = element = 1; + 3 | }); + +> Output + + `␊ + 1 | if (foo) for (let [index, element] of foo.entries()) {␊ + 2 | const a = element = 1;␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element, index) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | const a = element = 1;␊ + 3 | });␊ + ` + +## Invalid #188 1 | foo.forEach((element, index) => { 2 | let a; 3 | a >>>= element; @@ -1629,7 +3413,32 @@ Generated by [AVA](https://avajs.dev). 4 | });␊ ` -## Invalid #90 +## Invalid #189 + 1 | foo?.forEach((element, index) => { + 2 | let a; + 3 | a >>>= element; + 4 | }); + +> Output + + `␊ + 1 | if (foo) for (const [index, element] of foo.entries()) {␊ + 2 | let a;␊ + 3 | a >>>= element;␊ + 4 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element, index) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | let a;␊ + 3 | a >>>= element;␊ + 4 | });␊ + ` + +## Invalid #190 1 | foo.forEach(({property}) => {bar(property)}) > Output @@ -1645,7 +3454,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #91 +## Invalid #191 + 1 | foo?.forEach(({property}) => {bar(property)}) + +> Output + + `␊ + 1 | if (foo) for (const {property} of foo) {bar(property)}␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(({property}) => {bar(property)})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #192 1 | foo.forEach(({foo: {foo: [property]}}) => {bar(property, index)}) > Output @@ -1661,7 +3486,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #92 +## Invalid #193 + 1 | foo?.forEach(({foo: {foo: [property]}}) => {bar(property, index)}) + +> Output + + `␊ + 1 | if (foo) for (const {foo: {foo: [property]}} of foo) {bar(property, index)}␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(({foo: {foo: [property]}}) => {bar(property, index)})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #194 1 | foo.forEach((element, {bar: {bar: [index]}}) => {bar(element, index)}) > Output @@ -1677,7 +3518,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #93 +## Invalid #195 + 1 | foo?.forEach((element, {bar: {bar: [index]}}) => {bar(element, index)}) + +> Output + + `␊ + 1 | if (foo) for (const [{bar: {bar: [index]}}, element] of foo.entries()) {bar(element, index)}␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element, {bar: {bar: [index]}}) => {bar(element, index)})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #196 1 | foo.forEach((element = elementDefaultValue, index = indexDefaultValue) => {}) > Output @@ -1693,7 +3550,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #94 +## Invalid #197 + 1 | foo?.forEach((element = elementDefaultValue, index = indexDefaultValue) => {}) + +> Output + + `␊ + 1 | if (foo) for (const [index = indexDefaultValue, element = elementDefaultValue] of foo.entries()) {}␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach((element = elementDefaultValue, index = indexDefaultValue) => {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #198 1 | foo.forEach(({}) => {}) > Output @@ -1709,7 +3582,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #95 +## Invalid #199 + 1 | foo?.forEach(({}) => {}) + +> Output + + `␊ + 1 | if (foo) for (const {} of foo) {}␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(({}) => {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #200 1 | foo.forEach(function foo({a, b, c, d}) {}) > Output @@ -1725,7 +3614,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #96 +## Invalid #201 + 1 | foo?.forEach(function foo({a, b, c, d}) {}) + +> Output + + `␊ + 1 | if (foo) for (const {a, b, c, d} of foo) {}␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function foo({a, b, c, d}) {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #202 1 | foo.forEach(function foo({a, b, c, d, foo}) {}) > Error 1/1 @@ -1735,7 +3640,17 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #97 +## Invalid #203 + 1 | foo?.forEach(function foo({a, b, c, d, foo}) {}) + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(function foo({a, b, c, d, foo}) {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #204 1 | foo.forEach(({foo: property}) => {bar(property)}) > Output @@ -1751,7 +3666,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #98 +## Invalid #205 + 1 | foo?.forEach(({foo: property}) => {bar(property)}) + +> Output + + `␊ + 1 | if (foo) for (const {foo: property} of foo) {bar(property)}␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(({foo: property}) => {bar(property)})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #206 1 | foo.forEach(({[foo]: property}) => {bar(property)}) > Output @@ -1767,7 +3698,23 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` -## Invalid #99 +## Invalid #207 + 1 | foo?.forEach(({[foo]: property}) => {bar(property)}) + +> Output + + `␊ + 1 | if (foo) for (const {[foo]: property} of foo) {bar(property)}␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(({[foo]: property}) => {bar(property)})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #208 1 | foo.forEach(({element}, index) => { 2 | element &&= 2; 3 | }); @@ -1789,7 +3736,29 @@ Generated by [AVA](https://avajs.dev). 3 | });␊ ` -## Invalid #100 +## Invalid #209 + 1 | foo?.forEach(({element}, index) => { + 2 | element &&= 2; + 3 | }); + +> Output + + `␊ + 1 | if (foo) for (let [index, {element}] of foo.entries()) {␊ + 2 | element &&= 2;␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(({element}, index) => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | element &&= 2;␊ + 3 | });␊ + ` + +## Invalid #210 1 | foo.forEach(_ => { 2 | if (true) return {}; 3 | }) @@ -1811,7 +3780,29 @@ Generated by [AVA](https://avajs.dev). 3 | })␊ ` -## Invalid #101 +## Invalid #211 + 1 | foo?.forEach(_ => { + 2 | if (true) return {}; + 3 | }) + +> Output + + `␊ + 1 | if (foo) for (const _ of foo) {␊ + 2 | if (true) { ({}); continue; }␊ + 3 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(_ => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | if (true) return {};␊ + 3 | })␊ + ` + +## Invalid #212 1 | foo.forEach(_ => { 2 | if (true); 3 | else return {}; @@ -1836,7 +3827,32 @@ Generated by [AVA](https://avajs.dev). 4 | })␊ ` -## Invalid #102 +## Invalid #213 + 1 | foo?.forEach(_ => { + 2 | if (true); + 3 | else return {}; + 4 | }) + +> Output + + `␊ + 1 | if (foo) for (const _ of foo) {␊ + 2 | if (true);␊ + 3 | else { ({}); continue; }␊ + 4 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo?.forEach(_ => {␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + 2 | if (true);␊ + 3 | else return {};␊ + 4 | })␊ + ` + +## Invalid #214 1 | if (true) {} else[foo].forEach((element) => {}) > Output @@ -1851,3 +3867,35 @@ Generated by [AVA](https://avajs.dev). > 1 | if (true) {} else[foo].forEach((element) => {})␊ | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ ` + +## Invalid #215 + 1 | if (true) {} else[foo]?.forEach((element) => {}) + +> Output + + `␊ + 1 | if (true) {} else if ([foo]) for (const element of [foo]) {}␊ + ` + +> Error 1/1 + + `␊ + > 1 | if (true) {} else[foo]?.forEach((element) => {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` + +## Invalid #216 + 1 | 1?.forEach((a, b) => call(a, b)) + +> Output + + `␊ + 1 | if (1) for (const [b, a] of (1).entries()) call(a, b)␊ + ` + +> Error 1/1 + + `␊ + > 1 | 1?.forEach((a, b) => call(a, b))␊ + | ^^^^^^^ Use \`for…of\` instead of \`Array#forEach(…)\`.␊ + ` diff --git a/test/snapshots/no-array-for-each.mjs.snap b/test/snapshots/no-array-for-each.mjs.snap index c83076f71b..65c89fabc4 100644 Binary files a/test/snapshots/no-array-for-each.mjs.snap and b/test/snapshots/no-array-for-each.mjs.snap differ