Skip to content

Commit d3b2548

Browse files
authored
no-array-for-each: Check reassign in for..in and for..of (#1824)
1 parent e7306e5 commit d3b2548

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

rules/no-array-for-each.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ function isAssignmentLeftHandSide(node) {
314314

315315
switch (parent.type) {
316316
case 'AssignmentExpression':
317+
case 'ForInStatement':
318+
case 'ForOfStatement':
317319
return parent.left === node;
318320
case 'UpdateExpression':
319321
return parent.argument === node;

test/no-array-for-each.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,27 @@ test.snapshot({
502502
] = baz;
503503
});
504504
`,
505+
outdent`
506+
foo.forEach(element => {
507+
for (element in bar);
508+
});
509+
`,
510+
outdent`
511+
foo.forEach(element => {
512+
for ([{element}] of bar);
513+
});
514+
`,
515+
outdent`
516+
foo.forEach(element => {
517+
for (key in element);
518+
for (item of element);
519+
});
520+
`,
521+
outdent`
522+
foo.forEach((element, index) => {
523+
for (index of bar);
524+
});
525+
`,
505526
],
506527
});
507528

test/snapshots/no-array-for-each.mjs.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,3 +4875,94 @@ Generated by [AVA](https://avajs.dev).
48754875
4 | ] = baz;␊
48764876
5 | });␊
48774877
`
4878+
4879+
## Invalid #264
4880+
1 | foo.forEach(element => {
4881+
2 | for (element in bar);
4882+
3 | });
4883+
4884+
> Output
4885+
4886+
`␊
4887+
1 | for (let element of foo) {␊
4888+
2 | for (element in bar);␊
4889+
3 | }␊
4890+
`
4891+
4892+
> Error 1/1
4893+
4894+
`␊
4895+
> 1 | foo.forEach(element => {␊
4896+
| ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊
4897+
2 | for (element in bar);␊
4898+
3 | });␊
4899+
`
4900+
4901+
## Invalid #265
4902+
1 | foo.forEach(element => {
4903+
2 | for ([{element}] of bar);
4904+
3 | });
4905+
4906+
> Output
4907+
4908+
`␊
4909+
1 | for (let element of foo) {␊
4910+
2 | for ([{element}] of bar);␊
4911+
3 | }␊
4912+
`
4913+
4914+
> Error 1/1
4915+
4916+
`␊
4917+
> 1 | foo.forEach(element => {␊
4918+
| ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊
4919+
2 | for ([{element}] of bar);␊
4920+
3 | });␊
4921+
`
4922+
4923+
## Invalid #266
4924+
1 | foo.forEach(element => {
4925+
2 | for (key in element);
4926+
3 | for (item of element);
4927+
4 | });
4928+
4929+
> Output
4930+
4931+
`␊
4932+
1 | for (const element of foo) {␊
4933+
2 | for (key in element);␊
4934+
3 | for (item of element);␊
4935+
4 | }␊
4936+
`
4937+
4938+
> Error 1/1
4939+
4940+
`␊
4941+
> 1 | foo.forEach(element => {␊
4942+
| ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊
4943+
2 | for (key in element);␊
4944+
3 | for (item of element);␊
4945+
4 | });␊
4946+
`
4947+
4948+
## Invalid #267
4949+
1 | foo.forEach((element, index) => {
4950+
2 | for (index of bar);
4951+
3 | });
4952+
4953+
> Output
4954+
4955+
`␊
4956+
1 | for (let [index, element] of foo.entries()) {␊
4957+
2 | for (index of bar);␊
4958+
3 | }␊
4959+
`
4960+
4961+
> Error 1/1
4962+
4963+
`␊
4964+
> 1 | foo.forEach((element, index) => {␊
4965+
| ^^^^^^^ Use \`for…of\` instead of \`.forEach(…)\`.␊
4966+
2 | for (index of bar);␊
4967+
3 | });␊
4968+
`
232 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)