From 38ebe1342b244167b7ab3e99a4945093af2e0ac2 Mon Sep 17 00:00:00 2001 From: Armano Date: Thu, 18 Nov 2021 00:12:56 +0100 Subject: [PATCH 1/3] fix(eslint-plugin): [prefer-for-of] do not error when used on this expression --- packages/eslint-plugin/src/rules/prefer-for-of.ts | 8 ++++++++ packages/eslint-plugin/tests/rules/prefer-for-of.test.ts | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/packages/eslint-plugin/src/rules/prefer-for-of.ts b/packages/eslint-plugin/src/rules/prefer-for-of.ts index e1c3a0580166..c9f604bcd4e2 100644 --- a/packages/eslint-plugin/src/rules/prefer-for-of.ts +++ b/packages/eslint-plugin/src/rules/prefer-for-of.ts @@ -120,6 +120,14 @@ export default util.createRule({ return true; } + // this[i] + if ( + node.type === AST_NODE_TYPES.MemberExpression && + node.object.type === AST_NODE_TYPES.ThisExpression + ) { + return true; + } + // delete a[i] if ( parent.type === AST_NODE_TYPES.UnaryExpression && diff --git a/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts b/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts index d2d7319b3b1d..81ac196d266a 100644 --- a/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts @@ -180,6 +180,11 @@ 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]; +} + `, ], invalid: [ { From 26e5b763a45081895009aa17ae77bd7bb691a44b Mon Sep 17 00:00:00 2001 From: Armano Date: Thu, 18 Nov 2021 00:34:05 +0100 Subject: [PATCH 2/3] fix(eslint-plugin): [prefer-for-of] add missing test case --- packages/eslint-plugin/src/rules/prefer-for-of.ts | 5 +++-- .../eslint-plugin/tests/rules/prefer-for-of.test.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/prefer-for-of.ts b/packages/eslint-plugin/src/rules/prefer-for-of.ts index c9f604bcd4e2..9762c2c3ecd4 100644 --- a/packages/eslint-plugin/src/rules/prefer-for-of.ts +++ b/packages/eslint-plugin/src/rules/prefer-for-of.ts @@ -122,8 +122,9 @@ export default util.createRule({ // this[i] if ( - node.type === AST_NODE_TYPES.MemberExpression && - node.object.type === AST_NODE_TYPES.ThisExpression + parent.type === AST_NODE_TYPES.MemberExpression && + parent.object.type === AST_NODE_TYPES.ThisExpression && + parent.property === node ) { return true; } diff --git a/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts b/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts index 81ac196d266a..4fd2ac5ce5a7 100644 --- a/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts @@ -362,6 +362,18 @@ 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: [ From a5720e28a7f6f24c4e9cc7ca1e1cf9f524f0b017 Mon Sep 17 00:00:00 2001 From: Armano Date: Thu, 18 Nov 2021 00:51:38 +0100 Subject: [PATCH 3/3] fix(eslint-plugin): [prefer-for-of] do nor error when iterating over this --- .../eslint-plugin/src/rules/prefer-for-of.ts | 10 +--------- .../tests/rules/prefer-for-of.test.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/eslint-plugin/src/rules/prefer-for-of.ts b/packages/eslint-plugin/src/rules/prefer-for-of.ts index 9762c2c3ecd4..453d126c813e 100644 --- a/packages/eslint-plugin/src/rules/prefer-for-of.ts +++ b/packages/eslint-plugin/src/rules/prefer-for-of.ts @@ -120,15 +120,6 @@ export default util.createRule({ return true; } - // this[i] - if ( - parent.type === AST_NODE_TYPES.MemberExpression && - parent.object.type === AST_NODE_TYPES.ThisExpression && - parent.property === node - ) { - return true; - } - // delete a[i] if ( parent.type === AST_NODE_TYPES.UnaryExpression && @@ -183,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)) diff --git a/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts b/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts index 4fd2ac5ce5a7..02c7d6478f03 100644 --- a/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts @@ -183,6 +183,11 @@ 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]; } `, ], @@ -374,6 +379,18 @@ for (let i = 0; i < arr.length; i++) { 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: [