From dbc8b035ea6ef87ae39a60b2e6ffba340fb88c7e Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Thu, 2 Dec 2021 13:21:08 +0530 Subject: [PATCH 1/3] Fix duplicate declaration error --- packages/babel-traverse/src/scope/index.ts | 2 ++ packages/babel-traverse/test/scope.js | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/packages/babel-traverse/src/scope/index.ts b/packages/babel-traverse/src/scope/index.ts index 93aa94101602..648c342e323a 100644 --- a/packages/babel-traverse/src/scope/index.ts +++ b/packages/babel-traverse/src/scope/index.ts @@ -705,6 +705,8 @@ export default class Scope { for (const declar of declarations) { this.registerBinding(path.node.kind, declar); } + } else if(path.isClassDeclaration() && path.node.declare) { + this.registerBinding("class", path); } else if (path.isClassDeclaration()) { this.registerBinding("let", path); } else if (path.isImportDeclaration()) { diff --git a/packages/babel-traverse/test/scope.js b/packages/babel-traverse/test/scope.js index 1045afa0a3bc..e1b0d7e868ce 100644 --- a/packages/babel-traverse/test/scope.js +++ b/packages/babel-traverse/test/scope.js @@ -655,6 +655,18 @@ describe("scope", () => { }); }); + describe('duplicate declaration', () => { + it('should not throw error on duplicate class and function declaration', () => { + const ast = [ + t.classDeclaration(t.identifier('A'), t.super(), t.classBody([]), []), + t.functionDeclaration(t.identifier('A'), [], t.blockStatement([])) + ] + + ast[0].declare = true; + expect(() => getPath(ast)).not.toThrowError() + }) + }) + describe("global", () => { // node1, node2, success // every line will run 2 tests `node1;node2;` and `node2;node1;` From b48b88043d0b0def9e5c0b1cc5f22f5444afac33 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Thu, 2 Dec 2021 13:42:20 +0530 Subject: [PATCH 2/3] Fix lint error --- packages/babel-traverse/src/scope/index.ts | 2 +- packages/babel-traverse/test/scope.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/babel-traverse/src/scope/index.ts b/packages/babel-traverse/src/scope/index.ts index 648c342e323a..e9a207eb9226 100644 --- a/packages/babel-traverse/src/scope/index.ts +++ b/packages/babel-traverse/src/scope/index.ts @@ -705,7 +705,7 @@ export default class Scope { for (const declar of declarations) { this.registerBinding(path.node.kind, declar); } - } else if(path.isClassDeclaration() && path.node.declare) { + } else if (path.isClassDeclaration() && path.node.declare) { this.registerBinding("class", path); } else if (path.isClassDeclaration()) { this.registerBinding("let", path); diff --git a/packages/babel-traverse/test/scope.js b/packages/babel-traverse/test/scope.js index e1b0d7e868ce..89d56fb49efd 100644 --- a/packages/babel-traverse/test/scope.js +++ b/packages/babel-traverse/test/scope.js @@ -655,17 +655,17 @@ describe("scope", () => { }); }); - describe('duplicate declaration', () => { - it('should not throw error on duplicate class and function declaration', () => { + describe("duplicate declaration", () => { + it("should not throw error on duplicate class and function declaration", () => { const ast = [ - t.classDeclaration(t.identifier('A'), t.super(), t.classBody([]), []), - t.functionDeclaration(t.identifier('A'), [], t.blockStatement([])) - ] + t.classDeclaration(t.identifier("A"), t.super(), t.classBody([]), []), + t.functionDeclaration(t.identifier("A"), [], t.blockStatement([])), + ]; ast[0].declare = true; - expect(() => getPath(ast)).not.toThrowError() - }) - }) + expect(() => getPath(ast)).not.toThrowError(); + }); + }); describe("global", () => { // node1, node2, success From ec242bc24373fbd2d342014b9784c0f9a760d047 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Sat, 4 Dec 2021 12:54:20 +0530 Subject: [PATCH 3/3] Fix ambient classDeclaration in registerDeclaration --- packages/babel-traverse/src/scope/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/babel-traverse/src/scope/index.ts b/packages/babel-traverse/src/scope/index.ts index e9a207eb9226..e2eb17d8f724 100644 --- a/packages/babel-traverse/src/scope/index.ts +++ b/packages/babel-traverse/src/scope/index.ts @@ -705,9 +705,8 @@ export default class Scope { for (const declar of declarations) { this.registerBinding(path.node.kind, declar); } - } else if (path.isClassDeclaration() && path.node.declare) { - this.registerBinding("class", path); } else if (path.isClassDeclaration()) { + if (path.node.declare) return; this.registerBinding("let", path); } else if (path.isImportDeclaration()) { const specifiers = path.get("specifiers");