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

[wip] add type in converted type imports #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/__tests__/__snapshots__/imports.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ declare type S = typeof $Flowgen$Import$http;
`;

exports[`should handle imports 1`] = `
"import { GeneratorOptions } from \\"@babel/generator\\";
import traverse, { Visitor, NodePath } from \\"@babel/traverse\\";
import { Visitor as NewVisitor } from \\"@babel/traverse\\";
"import { type GeneratorOptions } from \\"@babel/generator\\";
import traverse, { type Visitor, NodePath } from \\"@babel/traverse\\";
import { type Visitor as NewVisitor } from \\"@babel/traverse\\";
import template from \\"@babel/template\\";
import * as t from \\"@babel/types\\";
import v, * as d from \\"typescript\\";
Expand Down
30 changes: 28 additions & 2 deletions src/nodes/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ import Node from "./node";
import * as printers from "../printers";
import { checker } from "../checker";
import * as ts from "typescript";
import { Identifier, SymbolFlags } from "typescript";

const typeFlags = new Set([
SymbolFlags.Interface,
SymbolFlags.Type,
SymbolFlags.TypeAlias,
SymbolFlags.TypeLiteral,
]);

function isType(name: Identifier) {
return typeFlags.has(
checker.current.getAliasedSymbol(checker.current.getSymbolAtLocation(name))
.flags,
);
}
function printSpecifier(node) {
return (isType(node.name) ? "type " : "") + printers.node.printType(node);
}

export default class Import extends Node {
constructor(node: RawNode) {
Expand Down Expand Up @@ -53,7 +71,11 @@ export default class Import extends Node {
result += `import${
this.module === "root" && !isTypeImport ? "" : " type"
} ${name.text}, {
${elements.map(node => printers.node.printType(node))}
${elements.map(node =>
this.module !== "root" || isTypeImport
? printers.node.printType(node)
: printSpecifier(node),
)}
} from '${this.raw.moduleSpecifier.text}';\n`;
}
if (enumElems.length > 0) {
Expand Down Expand Up @@ -85,7 +107,11 @@ export default class Import extends Node {
result += `import${
this.module === "root" && !isTypeImport ? "" : " type"
} {
${regularElems.map(node => printers.node.printType(node))}
${regularElems.map(node =>
this.module !== "root" || isTypeImport
? printers.node.printType(node)
: printSpecifier(node),
)}
} from '${this.raw.moduleSpecifier.text}';\n`;
}
if (enumElems.length > 0) {
Expand Down