Skip to content

Commit

Permalink
fix: module is not a reserved JS keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Sep 20, 2022
1 parent c62766d commit cc49e97
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2652,7 +2652,30 @@ export default abstract class StatementParser extends ExpressionParser {
}

parseMaybeImportReflection(node: Undone<N.ImportDeclaration>) {
if (this.match(tt._module)) {
let isImportReflection = false;
if (this.match(tt._module) && !this.state.containsEsc) {
const lookahead = this.lookahead();
if (tokenIsIdentifier(lookahead.type)) {
if (lookahead.type !== tt._from) {
// import module x
isImportReflection = true;
} else {
const nextNextTokenFirstChar = this.input.charCodeAt(
this.nextTokenStartSince(lookahead.end),
);
if (nextNextTokenFirstChar === charCodes.lowercaseF) {
// import module from from ...
isImportReflection = true;
}
}
} else {
// import module { x } ...
// This is invalid, we will continue parsing and throw
// recoverable errors later
isImportReflection = true;
}
}
if (isImportReflection) {
this.expectPlugin("importReflection");
this.next(); // eat tt._module;
node.module = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import modul\u0065 foo from "./module.wasm";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \"from\" (1:19)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module \u0066rom from "./module.wasm";
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"type": "File",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"program": {
"type": "Program",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":14,"end":23,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":23,"index":23}},
"local": {
"type": "Identifier",
"start":14,"end":23,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":23,"index":23},"identifierName":"from"},
"name": "from"
}
}
],
"module": true,
"source": {
"type": "StringLiteral",
"start":29,"end":44,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":44,"index":44}},
"extra": {
"rawValue": "./module.wasm",
"raw": "\"./module.wasm\""
},
"value": "./module.wasm"
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from from "./module.wasm";
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"type": "File",
"start":0,"end":40,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":40,"index":40}},
"program": {
"type": "Program",
"start":0,"end":40,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":40,"index":40}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":40,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":40,"index":40}},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":14,"end":18,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":18,"index":18}},
"local": {
"type": "Identifier",
"start":14,"end":18,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":18,"index":18},"identifierName":"from"},
"name": "from"
}
}
],
"module": true,
"source": {
"type": "StringLiteral",
"start":24,"end":39,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":39,"index":39}},
"extra": {
"rawValue": "./module.wasm",
"raw": "\"./module.wasm\""
},
"value": "./module.wasm"
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from "./module.wasm";
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"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": "ImportDeclaration",
"start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":35,"index":35}},
"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"
}
}
],
"module": false,
"source": {
"type": "StringLiteral",
"start":19,"end":34,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":34,"index":34}},
"extra": {
"rawValue": "./module.wasm",
"raw": "\"./module.wasm\""
},
"value": "./module.wasm"
}
}
],
"directives": []
}
}

0 comments on commit cc49e97

Please sign in to comment.