From a943f576d1f7181abc593f97b174fdcf8a6c421d Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Sun, 5 Dec 2021 22:54:56 +0530 Subject: [PATCH] Fix duplicate declaration error on ambient class declarations (#14016) --- packages/babel-traverse/src/scope/index.ts | 1 + packages/babel-traverse/test/scope.js | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/packages/babel-traverse/src/scope/index.ts b/packages/babel-traverse/src/scope/index.ts index 93aa94101602..e2eb17d8f724 100644 --- a/packages/babel-traverse/src/scope/index.ts +++ b/packages/babel-traverse/src/scope/index.ts @@ -706,6 +706,7 @@ export default class Scope { this.registerBinding(path.node.kind, declar); } } else if (path.isClassDeclaration()) { + if (path.node.declare) return; this.registerBinding("let", path); } else if (path.isImportDeclaration()) { const specifiers = path.get("specifiers"); diff --git a/packages/babel-traverse/test/scope.js b/packages/babel-traverse/test/scope.js index 9ed72bef2690..1ecc76346792 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;`