Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-for-of] do nor error when iterating over …
Browse files Browse the repository at this point in the history
…this (#4176)

* fix(eslint-plugin): [prefer-for-of] do not error when used on this expression

* fix(eslint-plugin): [prefer-for-of] add missing test case

* fix(eslint-plugin): [prefer-for-of] do nor error when iterating over this
  • Loading branch information
armano2 committed Nov 23, 2021
1 parent f8e9125 commit 258ddb0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/prefer-for-of.ts
Expand Up @@ -174,6 +174,7 @@ export default util.createRule({
!contains(body, id) ||
(node !== undefined &&
node.type === AST_NODE_TYPES.MemberExpression &&
node.object.type !== AST_NODE_TYPES.ThisExpression &&
node.property === id &&
sourceCode.getText(node.object) === arrayText &&
!isAssignee(node))
Expand Down
34 changes: 34 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-for-of.test.ts
Expand Up @@ -180,6 +180,16 @@ for (var c = 0; c < arr.length; c++) {
`
for (var d = 0; d < arr.length; d++) doMath?.(d);
`,
`
for (let i = 0; i < test.length; ++i) {
this[i];
}
`,
`
for (let i = 0; i < this.length; ++i) {
yield this[i];
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -357,6 +367,30 @@ for (let i = 0; i < arr.length; i++) {
code: `
for (let i = 0; i < arr.length; i++) {
({ foo: obj[arr[i]] } = { foo: 1 });
}
`,
errors: [
{
messageId: 'preferForOf',
},
],
},
{
code: `
for (let i = 0; i < this.item.length; ++i) {
this.item[i];
}
`,
errors: [
{
messageId: 'preferForOf',
},
],
},
{
code: `
for (let i = 0; i < this.array.length; ++i) {
yield this.array[i];
}
`,
errors: [
Expand Down

0 comments on commit 258ddb0

Please sign in to comment.