From 7bc72bb451e71d7d8964efdf4438d5a907aae655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 5 Apr 2021 16:02:02 -0400 Subject: [PATCH] fix: do not filter report from functions within class elements (#13106) --- .../src/rules/no-invalid-this.js | 8 +++- .../test/rules/no-invalid-this.js | 48 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/eslint/babel-eslint-plugin/src/rules/no-invalid-this.js b/eslint/babel-eslint-plugin/src/rules/no-invalid-this.js index b51b5af6362a..7d62d1388a76 100644 --- a/eslint/babel-eslint-plugin/src/rules/no-invalid-this.js +++ b/eslint/babel-eslint-plugin/src/rules/no-invalid-this.js @@ -17,7 +17,13 @@ export default ruleComposer.filterReports(noInvalidThisRule, problem => { node.key.type === "PrivateIdentifier") ) { inClassMember = true; - return; + break; + } else if ( + node.type === "FunctionDeclaration" || + node.type === "FunctionExpression" + ) { + inClassMember = false; + break; } node = node.parent; diff --git a/eslint/babel-eslint-plugin/test/rules/no-invalid-this.js b/eslint/babel-eslint-plugin/test/rules/no-invalid-this.js index ff732a9850bf..8ae7ed73fb60 100644 --- a/eslint/babel-eslint-plugin/test/rules/no-invalid-this.js +++ b/eslint/babel-eslint-plugin/test/rules/no-invalid-this.js @@ -93,6 +93,30 @@ const patterns = [ invalid: [], }, + { + code: "class A {a = () => { function b() { return this.b;} };};", + parserOptions: { ecmaVersion: 6 }, + valid: [], + invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + errors: [ + { + message: "Unexpected 'this'.", + }, + ], + }, + + { + code: "class A {a = () => { (function b() { return this.b;}); };};", + parserOptions: { ecmaVersion: 6 }, + valid: [], + invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + errors: [ + { + message: "Unexpected 'this'.", + }, + ], + }, + // Class Private methods { code: "class A {#a = this.b;};", @@ -108,6 +132,30 @@ const patterns = [ invalid: [], }, + { + code: "class A {#a = () => { function b() { return this.b;} };};", + parserOptions: { ecmaVersion: 6 }, + valid: [], + invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + errors: [ + { + message: "Unexpected 'this'.", + }, + ], + }, + + { + code: "class A {#a = () => { (function b() { return this.b;}); };};", + parserOptions: { ecmaVersion: 6 }, + valid: [], + invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + errors: [ + { + message: "Unexpected 'this'.", + }, + ], + }, + { code: "class A {#a() {return this.b;};};", parserOptions: { ecmaVersion: 6 },