Skip to content

Commit

Permalink
Import type for TS (#11053)
Browse files Browse the repository at this point in the history
* Add parser support for type only imports in TS

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>

* Add transform and printer test for type only imports in TS

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>

* Update packages/babel-parser/src/plugins/typescript/index.js

* Fix flow

Co-authored-by: Brian Ng <bng412@gmail.com>
Co-authored-by: Raja Sekar <rajasekar.murugan@omio.com>
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
  • Loading branch information
5 people committed Feb 26, 2020
1 parent 0018881 commit ed2af92
Show file tree
Hide file tree
Showing 17 changed files with 571 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
25 changes: 24 additions & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"column": 27
}
},
"importKind": "value",
"specifiers": [
{
"type": "ImportNamespaceSpecifier",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"column": 38
}
},
"importKind": "value",
"specifiers": [
{
"type": "ImportDefaultSpecifier",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import type FooDefault, { Bar, Baz } from "module";
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"type": "File",
"start": 0,
"end": 51,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 51
}
},
"errors": [
"SyntaxError: A type-only import can specify a default import or named bindings, but not both. (1:0)"
],
"program": {
"type": "Program",
"start": 0,
"end": 51,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 51
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start": 0,
"end": 51,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 51
}
},
"importKind": "type",
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start": 12,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 22
}
},
"local": {
"type": "Identifier",
"start": 12,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 22
},
"identifierName": "FooDefault"
},
"name": "FooDefault"
}
},
{
"type": "ImportSpecifier",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 29
}
},
"imported": {
"type": "Identifier",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 29
},
"identifierName": "Bar"
},
"name": "Bar"
},
"local": {
"type": "Identifier",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 29
},
"identifierName": "Bar"
},
"name": "Bar"
}
},
{
"type": "ImportSpecifier",
"start": 31,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 31
},
"end": {
"line": 1,
"column": 34
}
},
"imported": {
"type": "Identifier",
"start": 31,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 31
},
"end": {
"line": 1,
"column": 34
},
"identifierName": "Baz"
},
"name": "Baz"
},
"local": {
"type": "Identifier",
"start": 31,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 31
},
"end": {
"line": 1,
"column": 34
},
"identifierName": "Baz"
},
"name": "Baz"
}
}
],
"source": {
"type": "StringLiteral",
"start": 42,
"end": 50,
"loc": {
"start": {
"line": 1,
"column": 42
},
"end": {
"line": 1,
"column": 50
}
},
"extra": {
"rawValue": "module",
"raw": "\"module\""
},
"value": "module"
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';

0 comments on commit ed2af92

Please sign in to comment.