Skip to content

Commit

Permalink
no-array-for-each: Check reassign in for..in and for..of (#1824)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 26, 2022
1 parent e7306e5 commit d3b2548
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
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.

0 comments on commit d3b2548

Please sign in to comment.