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 import type/typeof printing with no specifiers #14309

Merged
merged 2 commits into from Feb 28, 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
49 changes: 26 additions & 23 deletions packages/babel-generator/src/generators/modules.ts
Expand Up @@ -177,38 +177,41 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {
this.word("import");
this.space();

if (node.importKind === "type" || node.importKind === "typeof") {
const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
if (isTypeKind) {
this.word(node.importKind);
this.space();
}

const specifiers = node.specifiers.slice(0);
if (specifiers?.length) {
// print "special" specifiers first
for (;;) {
const first = specifiers[0];
if (
isImportDefaultSpecifier(first) ||
isImportNamespaceSpecifier(first)
) {
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.token(",");
this.space();
}
} else {
break;
const hasSpecifiers = !!specifiers.length;
// print "special" specifiers first. The loop condition is constant,
// but there is a "break" in the body.
while (hasSpecifiers) {
const first = specifiers[0];
if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) {
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.token(",");
this.space();
}
} else {
break;
}
}

if (specifiers.length) {
this.token("{");
this.space();
this.printList(specifiers, node);
this.space();
this.token("}");
}
if (specifiers.length) {
this.token("{");
this.space();
this.printList(specifiers, node);
this.space();
this.token("}");
} else if (isTypeKind && !hasSpecifiers) {
this.token("{");
this.token("}");
}

if (hasSpecifiers || isTypeKind) {
this.space();
this.word("from");
this.space();
Expand Down
@@ -0,0 +1 @@
import typeof U from "x"
@@ -0,0 +1 @@
import typeof U from "x";
@@ -0,0 +1,2 @@
import type {} from 'some-module';
export type {} from "some-module"
@@ -0,0 +1,2 @@
import type {} from 'some-module';
export type {} from "some-module";