From 0c538c2dad781666c45e7e65c279cce2f8bc79a4 Mon Sep 17 00:00:00 2001 From: edgarlli Date: Sat, 9 Dec 2023 22:33:02 +0800 Subject: [PATCH 1/3] fix bug: https://github.com/eslint/eslint-scope/issues/112 or https://github.com/eslint/eslint-scope/issues/59 --- lib/referencer.js | 5 +- tests/es6-class-name-lex-open-heritage.js | 57 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/es6-class-name-lex-open-heritage.js diff --git a/lib/referencer.js b/lib/referencer.js index 6a5da52..27583c7 100644 --- a/lib/referencer.js +++ b/lib/referencer.js @@ -272,7 +272,8 @@ class Referencer extends esrecurse.Visitor { )); } - this.visit(node.superClass); + let may_ref_classid = node.superClass && node.superClass.type!=='Identifier' + if(!may_ref_classid) this.visit(node.superClass); this.scopeManager.__nestClassScope(node); @@ -284,6 +285,8 @@ class Referencer extends esrecurse.Visitor { node )); } + + if(may_ref_classid) this.visit(node.superClass); // visit superClass here with class-id defined for current scope this.visit(node.body); this.close(node); diff --git a/tests/es6-class-name-lex-open-heritage.js b/tests/es6-class-name-lex-open-heritage.js new file mode 100644 index 0000000..7e44c87 --- /dev/null +++ b/tests/es6-class-name-lex-open-heritage.js @@ -0,0 +1,57 @@ +// -*- coding: utf-8 -*- +// Copyright (C) 2014 Yusuke Suzuki +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +/* eslint-disable no-unused-expressions */ + +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; + +describe("ES6 class", () => { + it("declaration name creates class scope", () => { + const ast = espree(`var C = 'outside'; + let cls = class C extends ( + probeHeritage = function() { return C; }, // this C should be class C, not var C + setHeritage = function() { C = null; } + ) { + method() { + return C; + } + };`); + + const scopeManager = analyze(ast, { ecmaVersion: 6 }); + + let probeHeritageFuncScope = scopeManager.scopes[2] + expect(probeHeritageFuncScope.references[0].resolved?.identifiers?.length>0).to.be.true; + + let resovledOfC = probeHeritageFuncScope.references[0].resolved.identifiers[0] + + let classScope = scopeManager.scopes[1] + expect(classScope.type).to.be.equal('class'); + + let classId = classScope.block.id + expect(classId).to.be.equal(resovledOfC); + }); +}); + +// vim: set sw=4 ts=4 et tw=80 : From ff7744139e842b9ac7cb4acb182c2e860bb5fc82 Mon Sep 17 00:00:00 2001 From: zhihao132 <1127365587@qq.com> Date: Sat, 9 Dec 2023 22:58:32 +0800 Subject: [PATCH 2/3] add test case --- tests/es6-class-name-lex-open-heritage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/es6-class-name-lex-open-heritage.js b/tests/es6-class-name-lex-open-heritage.js index 7e44c87..845ccd6 100644 --- a/tests/es6-class-name-lex-open-heritage.js +++ b/tests/es6-class-name-lex-open-heritage.js @@ -49,7 +49,7 @@ describe("ES6 class", () => { let classScope = scopeManager.scopes[1] expect(classScope.type).to.be.equal('class'); - let classId = classScope.block.id + let classId = classScope.block.id; expect(classId).to.be.equal(resovledOfC); }); }); From 80b149ae060ca5ef01c3ed51c9364ca445b8a872 Mon Sep 17 00:00:00 2001 From: lizhihao <1127365587@qq.com> Date: Sat, 16 Dec 2023 01:20:41 +0800 Subject: [PATCH 3/3] fix: use naming camelCase and change test case name --- lib/referencer.js | 7 ++++--- tests/es6-class-name-lex-open-heritage.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/referencer.js b/lib/referencer.js index 27583c7..f5488ee 100644 --- a/lib/referencer.js +++ b/lib/referencer.js @@ -272,8 +272,8 @@ class Referencer extends esrecurse.Visitor { )); } - let may_ref_classid = node.superClass && node.superClass.type!=='Identifier' - if(!may_ref_classid) this.visit(node.superClass); + let mayRefClassId = node.superClass && node.superClass.type !== 'Identifier' + if(!mayRefClassId) this.visit(node.superClass); this.scopeManager.__nestClassScope(node); @@ -286,7 +286,8 @@ class Referencer extends esrecurse.Visitor { )); } - if(may_ref_classid) this.visit(node.superClass); // visit superClass here with class-id defined for current scope + // visit superClass here with class-id defined for current scope + if(mayRefClassId) this.visit(node.superClass); this.visit(node.body); this.close(node); diff --git a/tests/es6-class-name-lex-open-heritage.js b/tests/es6-class-name-lex-open-heritage.js index 845ccd6..3fb85ef 100644 --- a/tests/es6-class-name-lex-open-heritage.js +++ b/tests/es6-class-name-lex-open-heritage.js @@ -27,7 +27,7 @@ import { expect } from "chai"; import espree from "./util/espree.js"; import { analyze } from "../lib/index.js"; -describe("ES6 class", () => { +describe("ES6 class-name-lex-open-heritage", () => { it("declaration name creates class scope", () => { const ast = espree(`var C = 'outside'; let cls = class C extends (