Skip to content

Commit

Permalink
no-array-for-each: Fix auto-fix causing SyntaxError (#1813)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker Cheung <lionkay@gmail.com>
  • Loading branch information
TimShilov and fisker committed May 17, 2022
1 parent 166524a commit 47c8337
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 204 deletions.
2 changes: 2 additions & 0 deletions rules/no-array-for-each.js
Expand Up @@ -361,6 +361,8 @@ function isFixable(callExpression, {scope, functionInfo, allIdentifiers, context
const parameters = callback.params;
if (
!(parameters.length === 1 || parameters.length === 2)
// `array.forEach((element = defaultValue) => {})`
|| (parameters.length === 1 && parameters[0].type === 'AssignmentPattern')
|| parameters.some(({type, typeAnnotation}) => type === 'RestElement' || typeAnnotation)
|| !isFunctionParametersSafeToFix(callback, {scope, callExpression, allIdentifiers, context})
) {
Expand Down
1 change: 1 addition & 0 deletions test/no-array-for-each.mjs
Expand Up @@ -34,6 +34,7 @@ test.snapshot({
'foo.forEach((element, index, array) => bar())',
'property.forEach(({property}) => bar(property))',
'() => foo.forEach()',
'foo.forEach((element = {}) => call(element))',

// Can't turn `return` to `continue`
outdent`
Expand Down

0 comments on commit 47c8337

Please sign in to comment.