Skip to content

Commit

Permalink
support for import type in typescript definition files
Browse files Browse the repository at this point in the history
  • Loading branch information
zxbodya committed Jul 25, 2020
1 parent 19662b2 commit 823d624
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/__snapshots__/imports.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ exports[`should handle imports inside module 1`] = `
}
"
`;

exports[`should handle type imports 1`] = `
"import type { GeneratorOptions } from \\"@babel/generator\\";
import traverse from \\"@babel/traverse\\";
import type { Visitor as NewVisitor } from \\"@babel/traverse\\";
"
`;
9 changes: 9 additions & 0 deletions src/__tests__/imports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ type S = typeof import('http')
const result = compiler.compileDefinitionString(ts, { quiet: true });
expect(beautify(result)).toMatchSnapshot();
});

it("should handle type imports", () => {
const ts = `import type { GeneratorOptions } from "@babel/generator";
import type traverse from "@babel/traverse";
import type { Visitor as NewVisitor } from "@babel/traverse";
`;
const result = compiler.compileDefinitionString(ts, { quiet: true });
expect(beautify(result)).toMatchSnapshot();
});
9 changes: 7 additions & 2 deletions src/nodes/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export default class Import extends Node {
if (this.raw.importClause) {
const bindings = this.raw.importClause.namedBindings;
const name = this.raw.importClause.name;
const isTypeImport = this.raw.importClause.isTypeOnly;
if (name && bindings) {
const elements = bindings.elements;
if (elements) {
return `import${this.module === "root" ? "" : " type"} ${name.text}, {
return `import${
this.module === "root" && !isTypeImport ? "" : " type"
} ${name.text}, {
${elements.map(node => printers.node.printType(node))}
} from '${this.raw.moduleSpecifier.text}';\n`;
} else {
Expand All @@ -33,7 +36,9 @@ export default class Import extends Node {
if (bindings) {
const elements = bindings.elements;
if (elements) {
return `import${this.module === "root" ? "" : " type"} {
return `import${
this.module === "root" && !isTypeImport ? "" : " type"
} {
${elements.map(node => printers.node.printType(node))}
} from '${this.raw.moduleSpecifier.text}';\n`;
} else {
Expand Down

0 comments on commit 823d624

Please sign in to comment.