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: Check reassign in for..in and for..of #1824

Merged
merged 5 commits into from May 26, 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 @@ -314,6 +314,8 @@ function isAssignmentLeftHandSide(node) {

switch (parent.type) {
case 'AssignmentExpression':
case 'ForInStatement':
case 'ForOfStatement':
return parent.left === node;
case 'UpdateExpression':
return parent.argument === node;
Expand Down
21 changes: 21 additions & 0 deletions test/no-array-for-each.mjs
Expand Up @@ -502,6 +502,27 @@ test.snapshot({
] = baz;
});
`,
outdent`
foo.forEach(element => {
for (element in bar);
});
`,
outdent`
foo.forEach(element => {
for ([{element}] of bar);
});
`,
outdent`
foo.forEach(element => {
for (key in element);
for (item of element);
});
`,
outdent`
foo.forEach((element, index) => {
for (index of bar);
});
`,
],
});

Expand Down
91 changes: 91 additions & 0 deletions test/snapshots/no-array-for-each.mjs.md
Expand Up @@ -4875,3 +4875,94 @@ Generated by [AVA](https://avajs.dev).
4 | ] = baz;␊
5 | });␊
`

## Invalid #264
1 | foo.forEach(element => {
2 | for (element in bar);
3 | });

> Output

`␊
1 | for (let element of foo) {␊
2 | for (element in bar);␊
3 | }␊
`

> Error 1/1

`␊
> 1 | foo.forEach(element => {␊
| ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊
2 | for (element in bar);␊
3 | });␊
`

## Invalid #265
1 | foo.forEach(element => {
2 | for ([{element}] of bar);
3 | });

> Output

`␊
1 | for (let element of foo) {␊
2 | for ([{element}] of bar);␊
3 | }␊
`

> Error 1/1

`␊
> 1 | foo.forEach(element => {␊
| ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊
2 | for ([{element}] of bar);␊
3 | });␊
`

## Invalid #266
1 | foo.forEach(element => {
2 | for (key in element);
3 | for (item of element);
4 | });

> Output

`␊
1 | for (const element of foo) {␊
2 | for (key in element);␊
3 | for (item of element);␊
4 | }␊
`

> Error 1/1

`␊
> 1 | foo.forEach(element => {␊
| ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊
2 | for (key in element);␊
3 | for (item of element);␊
4 | });␊
`

## Invalid #267
1 | foo.forEach((element, index) => {
2 | for (index of bar);
3 | });

> Output

`␊
1 | for (let [index, element] of foo.entries()) {␊
2 | for (index of bar);␊
3 | }␊
`

> Error 1/1

`␊
> 1 | foo.forEach((element, index) => {␊
| ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊
2 | for (index of bar);␊
3 | });␊
`
Binary file modified test/snapshots/no-array-for-each.mjs.snap
Binary file not shown.