Skip to content

Commit

Permalink
fix: ts import type and func with duplicate name (#15284)
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
liuxingbaoyu committed Dec 16, 2022
1 parent 2dd8254 commit 87e48e7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
@@ -0,0 +1,4 @@
import type {Foo} from 'foo';
import {type Foo2} from 'foo';
function Foo(){}
function Foo2(){}
@@ -0,0 +1,3 @@
function Foo() {}
function Foo2() {}
export {};
10 changes: 9 additions & 1 deletion packages/babel-traverse/src/scope/index.ts
Expand Up @@ -733,9 +733,17 @@ export default class Scope {
if (path.node.declare) return;
this.registerBinding("let", path);
} else if (path.isImportDeclaration()) {
const isTypeDeclaration =
path.node.importKind === "type" || path.node.importKind === "typeof";
const specifiers = path.get("specifiers");
for (const specifier of specifiers) {
this.registerBinding("module", specifier);
const isTypeSpecifier =
isTypeDeclaration ||
(specifier.isImportSpecifier() &&
(specifier.node.importKind === "type" ||
specifier.node.importKind === "typeof"));

this.registerBinding(isTypeSpecifier ? "unknown" : "module", specifier);
}
} else if (path.isExportDeclaration()) {
// todo: improve babel-types
Expand Down
17 changes: 17 additions & 0 deletions packages/babel-traverse/test/scope.js
Expand Up @@ -386,6 +386,23 @@ describe("scope", () => {
).toBe("ImportSpecifier");
});

it("import type and func with duplicate name", function () {
expect(() => {
getPath(
`
import type {Foo} from 'foo';
import {type Foo2} from 'foo';
function Foo(){}
function Foo2(){}
`,
{
plugins: ["typescript"],
sourceType: "module",
},
);
}).not.toThrow();
});

it("variable constantness", function () {
expect(getPath("var a = 1;").scope.getBinding("a").constant).toBe(true);
expect(getPath("var a = 1; a = 2;").scope.getBinding("a").constant).toBe(
Expand Down

0 comments on commit 87e48e7

Please sign in to comment.