diff --git a/rules/no-array-for-each.js b/rules/no-array-for-each.js index 8df8ee66ea..eb863f86be 100644 --- a/rules/no-array-for-each.js +++ b/rules/no-array-for-each.js @@ -351,6 +351,8 @@ function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context !(parameters.length === 1 || parameters.length === 2) // `array.forEach((element = defaultValue) => {})` || (parameters.length === 1 && parameters[0].type === 'AssignmentPattern') + // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1814 + || (parameters.length === 2 && parameters[1].type !== 'Identifier') || parameters.some(({type, typeAnnotation}) => type === 'RestElement' || typeAnnotation) || !isFunctionParametersSafeToFix(callback, {scope, callExpression, allIdentifiers, context}) ) { diff --git a/test/no-array-for-each.mjs b/test/no-array-for-each.mjs index 400965b490..1d16ba62cc 100644 --- a/test/no-array-for-each.mjs +++ b/test/no-array-for-each.mjs @@ -523,6 +523,10 @@ test.snapshot({ for (index of bar); }); `, + 'array.forEach((element, index = element) => {})', + 'array.forEach(({foo}, index = foo) => {})', + 'array.forEach((element, {bar = element}) => {})', + 'array.forEach(({foo}, {bar = foo}) => {})', ], }); diff --git a/test/snapshots/no-array-for-each.mjs.md b/test/snapshots/no-array-for-each.mjs.md index cb052a94d8..0337e67e2d 100644 --- a/test/snapshots/no-array-for-each.mjs.md +++ b/test/snapshots/no-array-for-each.mjs.md @@ -3537,12 +3537,6 @@ Generated by [AVA](https://avajs.dev). ## Invalid #196 1 | foo.forEach((element, {bar: {bar: [index]}}) => {bar(element, index)}) -> Output - - `␊ - 1 | for (const [{bar: {bar: [index]}}, element] of foo.entries()) {bar(element, index)}␊ - ` - > Error 1/1 `␊ @@ -3553,12 +3547,6 @@ Generated by [AVA](https://avajs.dev). ## Invalid #197 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 `␊ @@ -3569,12 +3557,6 @@ Generated by [AVA](https://avajs.dev). ## Invalid #198 1 | foo.forEach((element = elementDefaultValue, index = indexDefaultValue) => {}) -> Output - - `␊ - 1 | for (const [index = indexDefaultValue, element = elementDefaultValue] of foo.entries()) {}␊ - ` - > Error 1/1 `␊ @@ -3585,12 +3567,6 @@ Generated by [AVA](https://avajs.dev). ## Invalid #199 1 | foo?.forEach((element = elementDefaultValue, index = indexDefaultValue) => {}) -> Output - - `␊ - 1 | if (foo) for (const [index = indexDefaultValue, element = elementDefaultValue] of foo.entries()) {}␊ - ` - > Error 1/1 `␊ @@ -4966,3 +4942,43 @@ Generated by [AVA](https://avajs.dev). 2 | for (index of bar);␊ 3 | });␊ ` + +## Invalid #268 + 1 | array.forEach((element, index = element) => {}) + +> Error 1/1 + + `␊ + > 1 | array.forEach((element, index = element) => {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊ + ` + +## Invalid #269 + 1 | array.forEach(({foo}, index = foo) => {}) + +> Error 1/1 + + `␊ + > 1 | array.forEach(({foo}, index = foo) => {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊ + ` + +## Invalid #270 + 1 | array.forEach((element, {bar = element}) => {}) + +> Error 1/1 + + `␊ + > 1 | array.forEach((element, {bar = element}) => {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊ + ` + +## Invalid #271 + 1 | array.forEach(({foo}, {bar = foo}) => {}) + +> Error 1/1 + + `␊ + > 1 | array.forEach(({foo}, {bar = foo}) => {})␊ + | ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊ + ` diff --git a/test/snapshots/no-array-for-each.mjs.snap b/test/snapshots/no-array-for-each.mjs.snap index 9d44859e01..4d8097a871 100644 Binary files a/test/snapshots/no-array-for-each.mjs.snap and b/test/snapshots/no-array-for-each.mjs.snap differ