Skip to content

Commit

Permalink
no-array-for-each: Skip fix for some edge cases (#1979)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Nov 18, 2022
1 parent effcad6 commit 48efc7a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
2 changes: 2 additions & 0 deletions rules/no-array-for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
) {
Expand Down
4 changes: 4 additions & 0 deletions test/no-array-for-each.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {})',
],
});

Expand Down
64 changes: 40 additions & 24 deletions test/snapshots/no-array-for-each.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`␊
Expand All @@ -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
`␊
Expand All @@ -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
`␊
Expand All @@ -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
`␊
Expand Down Expand Up @@ -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(…)\`.␊
`
Binary file modified test/snapshots/no-array-for-each.mjs.snap
Binary file not shown.

0 comments on commit 48efc7a

Please sign in to comment.