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

Add import type and export type support to TypeScript #11171

Merged
merged 5 commits into from Mar 16, 2020
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
@@ -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 @@ -1839,7 +1839,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 @@ -1862,6 +1885,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 @@ -2097,6 +2127,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;