Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ts import type and func with duplicate name #15284

Merged
merged 1 commit into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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