Add import type and export type support to TypeScript (#11171)

* Add support for type only imports in TS (#11053)
* Implement "export type {}" (#11122)
* Add "exportKind: type" when needed with TS (#11157)
* Add `onlyRemoveTypeImports` option to `transform-typescript` (#11173)
* Add onlyRemoveTypeImports to preset-typescript (#11179)

Co-authored-by: Brian Ng <bng412@gmail.com>
Co-authored-by: Raja Sekar <rajasekarm.dev@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Henry Zhu <smiley.we@gmail.com>
Co-authored-by: Siddhant N Trivedi <sidntrivedi012@gmail.com>
This commit is contained in:
Nicolò Ribaudo
2020-03-16 22:58:51 +01:00
committed by GitHub
parent 20d9a10186
commit 740260b236
68 changed files with 1799 additions and 44 deletions

View File

@@ -1865,7 +1865,30 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (this.match(tt.name) && this.lookahead().type === tt.eq) {
return this.tsParseImportEqualsDeclaration(node);
}
return super.parseImport(node);
if (this.eatContextual("type")) {
node.importKind = "type";
} else {
node.importKind = "value";
}
const importNode = super.parseImport(node);
/*:: invariant(importNode.type !== "TSImportEqualsDeclaration") */
// `import type` can only be used on imports with named imports or with a
// default import - but not both
if (
importNode.importKind === "type" &&
importNode.specifiers.length > 1 &&
importNode.specifiers[0].type === "ImportDefaultSpecifier"
) {
this.raise(
importNode.start,
"A type-only import can specify a default import or named bindings, but not both.",
);
}
return importNode;
}
parseExport(node: N.Node): N.AnyExport {
@@ -1888,6 +1911,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.semicolon();
return this.finishNode(decl, "TSNamespaceExportDeclaration");
} else {
if (this.isContextual("type") && this.lookahead().type === tt.braceL) {
this.next();
node.exportKind = "type";
} else {
node.exportKind = "value";
}
return super.parseExport(node);
}
}
@@ -2110,6 +2140,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (!declaration) {
declaration = super.parseExportDeclaration(node);
}
if (
declaration &&
(declaration.type === "TSInterfaceDeclaration" ||
declaration.type === "TSTypeAliasDeclaration" ||
isDeclare)
) {
node.exportKind = "type";
}
if (declaration && isDeclare) {
// Reset location to include `declare` in range