Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-array-for-each: Skip fix for some edge cases #1979

Merged
merged 2 commits into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions rules/no-array-for-each.js
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
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
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.