Skip to content

Commit 4dedd57

Browse files
authored
fix: parse import module, ... (#15198)
1 parent 31386b9 commit 4dedd57

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

packages/babel-parser/src/parser/statement.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,8 +2747,9 @@ export default abstract class StatementParser extends ExpressionParser {
27472747
let isImportReflection = false;
27482748
if (this.isContextual(tt._module)) {
27492749
const lookahead = this.lookahead();
2750-
if (tokenIsIdentifier(lookahead.type)) {
2751-
if (lookahead.type !== tt._from) {
2750+
const nextType = lookahead.type;
2751+
if (tokenIsIdentifier(nextType)) {
2752+
if (nextType !== tt._from) {
27522753
// import module x
27532754
isImportReflection = true;
27542755
} else {
@@ -2760,9 +2761,10 @@ export default abstract class StatementParser extends ExpressionParser {
27602761
isImportReflection = true;
27612762
}
27622763
}
2763-
} else {
2764+
} else if (nextType !== tt.comma) {
27642765
// import module { x } ...
2765-
// This is invalid, we will continue parsing and throw
2766+
// import module "foo"
2767+
// They are invalid, we will continue parsing and throw
27662768
// a recoverable error later
27672769
isImportReflection = true;
27682770
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import module, { createRequire } from "module";
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}},
7+
"sourceType": "module",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "ImportDeclaration",
12+
"start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}},
13+
"specifiers": [
14+
{
15+
"type": "ImportDefaultSpecifier",
16+
"start":7,"end":13,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":13,"index":13}},
17+
"local": {
18+
"type": "Identifier",
19+
"start":7,"end":13,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":13,"index":13},"identifierName":"module"},
20+
"name": "module"
21+
}
22+
},
23+
{
24+
"type": "ImportSpecifier",
25+
"start":17,"end":30,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":30,"index":30}},
26+
"imported": {
27+
"type": "Identifier",
28+
"start":17,"end":30,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":30,"index":30},"identifierName":"createRequire"},
29+
"name": "createRequire"
30+
},
31+
"local": {
32+
"type": "Identifier",
33+
"start":17,"end":30,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":30,"index":30},"identifierName":"createRequire"},
34+
"name": "createRequire"
35+
}
36+
}
37+
],
38+
"module": false,
39+
"source": {
40+
"type": "StringLiteral",
41+
"start":38,"end":46,"loc":{"start":{"line":1,"column":38,"index":38},"end":{"line":1,"column":46,"index":46}},
42+
"extra": {
43+
"rawValue": "module",
44+
"raw": "\"module\""
45+
},
46+
"value": "module"
47+
}
48+
}
49+
],
50+
"directives": []
51+
}
52+
}

0 commit comments

Comments
 (0)