diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 05525510791c..0170a3b8165f 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -3696,9 +3696,19 @@ export default (superClass: Class): Class => } else if (tokenIsKeywordOrIdentifier(this.state.type)) { // { type something ...? } hasTypeSpecifier = true; - leftOfAs = isImport - ? this.parseIdentifier() - : this.parseModuleExportName(); + if (isImport) { + leftOfAs = this.parseIdentifier(true); + if (!this.isContextual(tt._as)) { + this.checkReservedWord( + leftOfAs.name, + leftOfAs.loc.start, + true, + true, + ); + } + } else { + leftOfAs = this.parseModuleExportName(); + } } if (hasTypeSpecifier && isInTypeOnlyImportExport) { this.raise( diff --git a/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword/input.ts b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-invalid-type-only-keyword/input.ts similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword/input.ts rename to packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-invalid-type-only-keyword/input.ts diff --git a/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword/output.json b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-invalid-type-only-keyword/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword/output.json rename to packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-invalid-type-only-keyword/output.json diff --git a/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword-from/input.ts b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword-from/input.ts new file mode 100644 index 000000000000..25a613f1aca8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword-from/input.ts @@ -0,0 +1 @@ +export { type if } from "./mode.js" diff --git a/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword-from/output.json b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword-from/output.json new file mode 100644 index 000000000000..78497f7669ff --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/export-type-only-keyword-from/output.json @@ -0,0 +1,45 @@ +{ + "type": "File", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "program": { + "type": "Program", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "exportKind": "value", + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16}}, + "local": { + "type": "Identifier", + "start":14,"end":16,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":16,"index":16},"identifierName":"if"}, + "name": "if" + }, + "exported": { + "type": "Identifier", + "start":14,"end":16,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":16,"index":16},"identifierName":"if"}, + "name": "if" + }, + "exportKind": "type" + } + ], + "source": { + "type": "StringLiteral", + "start":24,"end":35,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":35,"index":35}}, + "extra": { + "rawValue": "./mode.js", + "raw": "\"./mode.js\"" + }, + "value": "./mode.js" + }, + "declaration": null + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-invalid-type-named-keywords/input.ts b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-invalid-type-named-keywords/input.ts new file mode 100644 index 000000000000..c09bf9c9ff9b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-invalid-type-named-keywords/input.ts @@ -0,0 +1 @@ +import { type if } from "./mod.js"; diff --git a/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-invalid-type-named-keywords/output.json b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-invalid-type-named-keywords/output.json new file mode 100644 index 000000000000..842081fef6ca --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-invalid-type-named-keywords/output.json @@ -0,0 +1,47 @@ +{ + "type": "File", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "errors": [ + "SyntaxError: Unexpected keyword 'if'. (1:14)" + ], + "program": { + "type": "Program", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}}, + "importKind": "value", + "specifiers": [ + { + "type": "ImportSpecifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16}}, + "imported": { + "type": "Identifier", + "start":14,"end":16,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":16,"index":16},"identifierName":"if"}, + "name": "if" + }, + "local": { + "type": "Identifier", + "start":14,"end":16,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":16,"index":16},"identifierName":"if"}, + "name": "if" + }, + "importKind": "type" + } + ], + "source": { + "type": "StringLiteral", + "start":24,"end":34,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":34,"index":34}}, + "extra": { + "rawValue": "./mod.js", + "raw": "\"./mod.js\"" + }, + "value": "./mod.js" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-type-only-named-keywords-as/input.ts b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-type-only-named-keywords-as/input.ts new file mode 100644 index 000000000000..bf9b66fd9d9b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-type-only-named-keywords-as/input.ts @@ -0,0 +1 @@ +import { type default as a, type if as b } from "./mod.js"; diff --git a/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-type-only-named-keywords-as/output.json b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-type-only-named-keywords-as/output.json new file mode 100644 index 000000000000..46758849b9bd --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-only-import-export-specifiers/import-type-only-named-keywords-as/output.json @@ -0,0 +1,59 @@ +{ + "type": "File", + "start":0,"end":59,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":59,"index":59}}, + "program": { + "type": "Program", + "start":0,"end":59,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":59,"index":59}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":59,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":59,"index":59}}, + "importKind": "value", + "specifiers": [ + { + "type": "ImportSpecifier", + "start":9,"end":26,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":26,"index":26}}, + "imported": { + "type": "Identifier", + "start":14,"end":21,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":21,"index":21},"identifierName":"default"}, + "name": "default" + }, + "local": { + "type": "Identifier", + "start":25,"end":26,"loc":{"start":{"line":1,"column":25,"index":25},"end":{"line":1,"column":26,"index":26},"identifierName":"a"}, + "name": "a" + }, + "importKind": "type" + }, + { + "type": "ImportSpecifier", + "start":28,"end":40,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":40,"index":40}}, + "imported": { + "type": "Identifier", + "start":33,"end":35,"loc":{"start":{"line":1,"column":33,"index":33},"end":{"line":1,"column":35,"index":35},"identifierName":"if"}, + "name": "if" + }, + "local": { + "type": "Identifier", + "start":39,"end":40,"loc":{"start":{"line":1,"column":39,"index":39},"end":{"line":1,"column":40,"index":40},"identifierName":"b"}, + "name": "b" + }, + "importKind": "type" + } + ], + "source": { + "type": "StringLiteral", + "start":48,"end":58,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":58,"index":58}}, + "extra": { + "rawValue": "./mod.js", + "raw": "\"./mod.js\"" + }, + "value": "./mod.js" + } + } + ], + "directives": [] + } +}