Skip to content

Commit

Permalink
Add import type and export type support to TypeScript (#11171)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
7 people committed Mar 16, 2020
1 parent 20d9a10 commit 740260b
Show file tree
Hide file tree
Showing 68 changed files with 1,799 additions and 44 deletions.
@@ -0,0 +1,2 @@
export interface A {}
export type B = string;
@@ -0,0 +1,2 @@
export interface A {}
export type B = string;
@@ -0,0 +1,3 @@
type A = 2;
export type { A };
export type { B } from "./mod";
@@ -0,0 +1,3 @@
type A = 2;
export type { A };
export type { B } from "./mod";
@@ -0,0 +1,3 @@
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
@@ -0,0 +1,3 @@
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
40 changes: 39 additions & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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
Expand Down
@@ -0,0 +1 @@
export type from 'test';
@@ -0,0 +1,5 @@
{
"plugins": ["exportDefaultFrom", "typescript"],
"sourceType": "module",
"throws": "Unexpected token, expected \"=\" (1:17)"
}
Expand Up @@ -43,6 +43,7 @@
"column": 26
}
},
"exportKind": "value",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -110,6 +111,7 @@
"column": 29
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -176,6 +178,7 @@
"column": 16
}
},
"exportKind": "value",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -226,6 +229,7 @@
"column": 21
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -291,6 +295,7 @@
"column": 18
}
},
"exportKind": "value",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -356,6 +361,7 @@
"column": 21
}
},
"exportKind": "value",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -421,6 +427,7 @@
"column": 27
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down
Expand Up @@ -43,6 +43,7 @@
"column": 24
}
},
"exportKind": "value",
"declaration": {
"type": "Identifier",
"start": 78,
Expand Down
Expand Up @@ -144,6 +144,7 @@
"column": 27
}
},
"exportKind": "value",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -229,6 +230,7 @@
"column": 33
}
},
"exportKind": "value",
"declaration": {
"type": "ClassDeclaration",
"start": 148,
Expand Down Expand Up @@ -296,6 +298,7 @@
"column": 36
}
},
"exportKind": "value",
"declaration": {
"type": "ClassDeclaration",
"start": 182,
Expand Down
Expand Up @@ -43,6 +43,7 @@
"column": 22
}
},
"exportKind": "value",
"specifiers": [],
"source": null,
"declaration": {
Expand Down
Expand Up @@ -43,6 +43,7 @@
"column": 30
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down
Expand Up @@ -43,6 +43,7 @@
"column": 16
}
},
"exportKind": "value",
"specifiers": [],
"source": null,
"declaration": {
Expand Down
Expand Up @@ -43,6 +43,7 @@
"column": 31
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -142,6 +143,7 @@
"column": 34
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -225,6 +227,7 @@
"column": 25
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -292,6 +295,7 @@
"column": 29
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -358,6 +362,7 @@
"column": 31
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -423,6 +428,7 @@
"column": 26
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -489,6 +495,7 @@
"column": 29
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -555,6 +562,7 @@
"column": 26
}
},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
Expand Down
@@ -0,0 +1,4 @@
export type A = 2;
export interface B {}
export declare function a(): string;
export declare var b: string;

0 comments on commit 740260b

Please sign in to comment.