diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 95f2e39727..338ef9309b 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1862,14 +1862,25 @@ export default (superClass: Class): Class => checkDuplicateExports() {} parseImport(node: N.Node): N.AnyImport { - if (this.match(tt.name) && this.lookahead().type === tt.eq) { - return this.tsParseImportEqualsDeclaration(node); - } + if (this.match(tt.name) || this.match(tt.star) || this.match(tt.braceL)) { + const ahead = this.lookahead(); - if (this.eatContextual("type")) { - node.importKind = "type"; - } else { - node.importKind = "value"; + if (this.match(tt.name) && ahead.type === tt.eq) { + return this.tsParseImportEqualsDeclaration(node); + } + + if ( + this.isContextual("type") && + // import type, { a } from "b"; + ahead.type !== tt.comma && + // import type from "a"; + !(ahead.type === tt.name && ahead.value === "from") + ) { + node.importKind = "type"; + this.next(); + } else { + node.importKind = "value"; + } } const importNode = super.parseImport(node); diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/input.ts b/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/input.ts new file mode 100644 index 0000000000..64f64cd72b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/input.ts @@ -0,0 +1 @@ +import type, { bar } from 'foo'; diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/options.json b/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/options.json new file mode 100644 index 0000000000..d1b7c8ebea --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/options.json @@ -0,0 +1,6 @@ +{ + "sourceType": "module", + "plugins": [ + "typescript" + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/output.json b/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/output.json new file mode 100644 index 0000000000..1140ce07c2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/output.json @@ -0,0 +1,154 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 32 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 32 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 32 + } + }, + "importKind": "value", + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "type" + }, + "name": "type" + } + }, + { + "type": "ImportSpecifier", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "imported": { + "type": "Identifier", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "bar" + }, + "name": "bar" + }, + "local": { + "type": "Identifier", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "bar" + }, + "name": "bar" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 26, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "extra": { + "rawValue": "foo", + "raw": "'foo'" + }, + "value": "foo" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/input.ts b/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/input.ts new file mode 100644 index 0000000000..23d4b6f9bc --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/input.ts @@ -0,0 +1 @@ +import type from 'foo'; diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/options.json b/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/options.json new file mode 100644 index 0000000000..d1b7c8ebea --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/options.json @@ -0,0 +1,6 @@ +{ + "sourceType": "module", + "plugins": [ + "typescript" + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/output.json b/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/output.json new file mode 100644 index 0000000000..a340498869 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/output.json @@ -0,0 +1,105 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "importKind": "value", + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "local": { + "type": "Identifier", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "type" + }, + "name": "type" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 17, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "extra": { + "rawValue": "foo", + "raw": "'foo'" + }, + "value": "foo" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-named/input.ts b/packages/babel-parser/test/fixtures/typescript/import/import-named/input.ts new file mode 100644 index 0000000000..6a9d7a7aed --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-named/input.ts @@ -0,0 +1 @@ +import { foo } from "bar"; diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-named/output.json b/packages/babel-parser/test/fixtures/typescript/import/import-named/output.json new file mode 100644 index 0000000000..cbad2bcfaa --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-named/output.json @@ -0,0 +1,122 @@ +{ + "type": "File", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "importKind": "value", + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "imported": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "local": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "extra": { + "rawValue": "bar", + "raw": "\"bar\"" + }, + "value": "bar" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-star-as/input.ts b/packages/babel-parser/test/fixtures/typescript/import/import-star-as/input.ts new file mode 100644 index 0000000000..02f2ec844a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-star-as/input.ts @@ -0,0 +1 @@ +import * as a from "a"; diff --git a/packages/babel-parser/test/fixtures/typescript/import/import-star-as/output.json b/packages/babel-parser/test/fixtures/typescript/import/import-star-as/output.json new file mode 100644 index 0000000000..3a27b871b8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/import/import-star-as/output.json @@ -0,0 +1,105 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "importKind": "value", + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start": 7, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "local": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "a" + }, + "name": "a" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "extra": { + "rawValue": "a", + "raw": "\"a\"" + }, + "value": "a" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/input.ts b/packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/input.ts new file mode 100644 index 0000000000..a0c06da79d --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/input.ts @@ -0,0 +1 @@ +import typ\u{65} typescript from "typescript"; diff --git a/packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/options.json b/packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/options.json new file mode 100644 index 0000000000..620770863a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/options.json @@ -0,0 +1,7 @@ +{ + "sourceType": "module", + "plugins": [ + "typescript" + ], + "throws": "Unexpected token (1:17)" +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/input.ts new file mode 100644 index 0000000000..64f64cd72b --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/input.ts @@ -0,0 +1 @@ +import type, { bar } from 'foo'; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/options.json new file mode 100644 index 0000000000..ce280969f6 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [[ + "transform-typescript", + { + "onlyRemoveTypeImports": true + } + ]] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/output.mjs new file mode 100644 index 0000000000..64f64cd72b --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type-default-and-named/output.mjs @@ -0,0 +1 @@ +import type, { bar } from 'foo'; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/input.ts new file mode 100644 index 0000000000..23d4b6f9bc --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/input.ts @@ -0,0 +1 @@ +import type from 'foo'; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/options.json new file mode 100644 index 0000000000..ce280969f6 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [[ + "transform-typescript", + { + "onlyRemoveTypeImports": true + } + ]] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/output.mjs new file mode 100644 index 0000000000..23d4b6f9bc --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/output.mjs @@ -0,0 +1 @@ +import type from 'foo';