Skip to content

Commit

Permalink
fix: parse import module, ... (#15198)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 16, 2022
1 parent 31386b9 commit 4dedd57
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -2747,8 +2747,9 @@ export default abstract class StatementParser extends ExpressionParser {
let isImportReflection = false;
if (this.isContextual(tt._module)) {
const lookahead = this.lookahead();
if (tokenIsIdentifier(lookahead.type)) {
if (lookahead.type !== tt._from) {
const nextType = lookahead.type;
if (tokenIsIdentifier(nextType)) {
if (nextType !== tt._from) {
// import module x
isImportReflection = true;
} else {
Expand All @@ -2760,9 +2761,10 @@ export default abstract class StatementParser extends ExpressionParser {
isImportReflection = true;
}
}
} else {
} else if (nextType !== tt.comma) {
// import module { x } ...
// This is invalid, we will continue parsing and throw
// import module "foo"
// They are invalid, we will continue parsing and throw
// a recoverable error later
isImportReflection = true;
}
Expand Down
@@ -0,0 +1 @@
import module, { createRequire } from "module";
@@ -0,0 +1,52 @@
{
"type": "File",
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}},
"program": {
"type": "Program",
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":7,"end":13,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":13,"index":13}},
"local": {
"type": "Identifier",
"start":7,"end":13,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":13,"index":13},"identifierName":"module"},
"name": "module"
}
},
{
"type": "ImportSpecifier",
"start":17,"end":30,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":30,"index":30}},
"imported": {
"type": "Identifier",
"start":17,"end":30,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":30,"index":30},"identifierName":"createRequire"},
"name": "createRequire"
},
"local": {
"type": "Identifier",
"start":17,"end":30,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":30,"index":30},"identifierName":"createRequire"},
"name": "createRequire"
}
}
],
"module": false,
"source": {
"type": "StringLiteral",
"start":38,"end":46,"loc":{"start":{"line":1,"column":38,"index":38},"end":{"line":1,"column":46,"index":46}},
"extra": {
"rawValue": "module",
"raw": "\"module\""
},
"value": "module"
}
}
],
"directives": []
}
}

0 comments on commit 4dedd57

Please sign in to comment.