From 108fb65451fd1cdd4561235e6e4b801404b444e0 Mon Sep 17 00:00:00 2001 From: Srijan Saurav <68371686+srijan-deepsource@users.noreply.github.com> Date: Mon, 30 May 2022 16:19:26 +0530 Subject: [PATCH] Revert "feat: fix no-eval logic for `this` in arrow functions (#15755)" This reverts commit 74d1513701eb79645a4766d7ddafed6ad75d7e65. --- lib/rules/no-eval.js | 33 ++++++++++++++++----------------- tests/lib/rules/no-eval.js | 7 ------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/rules/no-eval.js b/lib/rules/no-eval.js index 7af8dfac7fa..dab04dd77e2 100644 --- a/lib/rules/no-eval.js +++ b/lib/rules/no-eval.js @@ -72,18 +72,15 @@ module.exports = { let funcInfo = null; /** - * Pushs a `this` scope (non-arrow function, class static block, or class field initializer) information to the stack. - * Top-level scopes are handled separately. + * Pushs a variable scope (Program or Function) information to the stack. * * This is used in order to check whether or not `this` binding is a * reference to the global object. - * @param {ASTNode} node A node of the scope. - * For functions, this is one of FunctionDeclaration, FunctionExpression. - * For class static blocks, this is StaticBlock. - * For class field initializers, this can be any node that is PropertyDefinition#value. + * @param {ASTNode} node A node of the scope. This is one of Program, + * FunctionDeclaration, FunctionExpression, and ArrowFunctionExpression. * @returns {void} */ - function enterThisScope(node) { + function enterVarScope(node) { const strict = context.getScope().isStrict; funcInfo = { @@ -100,7 +97,7 @@ module.exports = { * Pops a variable scope from the stack. * @returns {void} */ - function exitThisScope() { + function exitVarScope() { funcInfo = funcInfo.upper; } @@ -242,19 +239,21 @@ module.exports = { "Program:exit"() { const globalScope = context.getScope(); - exitThisScope(); + exitVarScope(); reportAccessingEval(globalScope); reportAccessingEvalViaGlobalObject(globalScope); }, - FunctionDeclaration: enterThisScope, - "FunctionDeclaration:exit": exitThisScope, - FunctionExpression: enterThisScope, - "FunctionExpression:exit": exitThisScope, - "PropertyDefinition > *.value": enterThisScope, - "PropertyDefinition > *.value:exit": exitThisScope, - StaticBlock: enterThisScope, - "StaticBlock:exit": exitThisScope, + FunctionDeclaration: enterVarScope, + "FunctionDeclaration:exit": exitVarScope, + FunctionExpression: enterVarScope, + "FunctionExpression:exit": exitVarScope, + ArrowFunctionExpression: enterVarScope, + "ArrowFunctionExpression:exit": exitVarScope, + "PropertyDefinition > *.value": enterVarScope, + "PropertyDefinition > *.value:exit": exitVarScope, + StaticBlock: enterVarScope, + "StaticBlock:exit": exitVarScope, ThisExpression(node) { if (!isMember(node.parent, "eval")) { diff --git a/tests/lib/rules/no-eval.js b/tests/lib/rules/no-eval.js index 840bcb20f1d..fdd961f6546 100644 --- a/tests/lib/rules/no-eval.js +++ b/tests/lib/rules/no-eval.js @@ -48,9 +48,6 @@ ruleTester.run("no-eval", rule, { { code: "function foo() { this.eval('foo'); }", parserOptions: { ecmaFeatures: { impliedStrict: true } } }, "var obj = {foo: function() { this.eval('foo'); }}", "var obj = {}; obj.foo = function() { this.eval('foo'); }", - { code: "() => { this.eval('foo') }", parserOptions: { ecmaVersion: 6, sourceType: "module" } }, - { code: "function f() { 'use strict'; () => { this.eval('foo') } }", parserOptions: { ecmaVersion: 6 } }, - { code: "(function f() { 'use strict'; () => { this.eval('foo') } })", parserOptions: { ecmaVersion: 6 } }, { code: "class A { foo() { this.eval(); } }", parserOptions: { ecmaVersion: 6 } }, { code: "class A { static foo() { this.eval(); } }", parserOptions: { ecmaVersion: 6 } }, { code: "class A { field = this.eval(); }", parserOptions: { ecmaVersion: 2022 } }, @@ -98,10 +95,6 @@ ruleTester.run("no-eval", rule, { { code: "var EVAL = eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "Identifier", column: 12, endColumn: 16 }] }, { code: "var EVAL = this.eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "MemberExpression", column: 17, endColumn: 21 }] }, { code: "'use strict'; var EVAL = this.eval; EVAL('foo')", errors: [{ messageId: "unexpected", type: "MemberExpression", column: 31, endColumn: 35 }] }, - { code: "() => { this.eval('foo'); }", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 14, endColumn: 18 }] }, - { code: "() => { 'use strict'; this.eval('foo'); }", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 28, endColumn: 32 }] }, - { code: "'use strict'; () => { this.eval('foo'); }", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 28, endColumn: 32 }] }, - { code: "() => { 'use strict'; () => { this.eval('foo'); } }", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 36, endColumn: 40 }] }, { code: "(function(exe){ exe('foo') })(eval);", errors: [{ messageId: "unexpected", type: "Identifier", column: 31, endColumn: 35 }] }, { code: "window.eval('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 8, endColumn: 12 }] }, { code: "window.window.eval('foo')", env: { browser: true }, errors: [{ messageId: "unexpected", type: "CallExpression", column: 15, endColumn: 19 }] },