Skip to content

Commit 47c8337

Browse files
TimShilovfisker
andauthored
no-array-for-each: Fix auto-fix causing SyntaxError (#1813)
Co-authored-by: fisker Cheung <[email protected]>
1 parent 166524a commit 47c8337

File tree

4 files changed

+227
-204
lines changed

4 files changed

+227
-204
lines changed

rules/no-array-for-each.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context
361361
const parameters = callback.params;
362362
if (
363363
!(parameters.length === 1 || parameters.length === 2)
364+
// `array.forEach((element = defaultValue) => {})`
365+
|| (parameters.length === 1 && parameters[0].type === 'AssignmentPattern')
364366
|| parameters.some(({type, typeAnnotation}) => type === 'RestElement' || typeAnnotation)
365367
|| !isFunctionParametersSafeToFix(callback, {scope, callExpression, allIdentifiers, context})
366368
) {

test/no-array-for-each.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ test.snapshot({
3434
'foo.forEach((element, index, array) => bar())',
3535
'property.forEach(({property}) => bar(property))',
3636
'() => foo.forEach()',
37+
'foo.forEach((element = {}) => call(element))',
3738

3839
// Can't turn `return` to `continue`
3940
outdent`

0 commit comments

Comments
 (0)