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

fix: allow liberal named type-as imports #14522

Merged
merged 1 commit into from May 4, 2022
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
16 changes: 13 additions & 3 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -3696,9 +3696,19 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} 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(
Expand Down
@@ -0,0 +1 @@
export { type if } from "./mode.js"
@@ -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": []
}
}
@@ -0,0 +1 @@
import { type if } from "./mod.js";
@@ -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": []
}
}
@@ -0,0 +1 @@
import { type default as a, type if as b } from "./mod.js";
@@ -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": []
}
}