From 5c062afca55a18704a1640095a76129ed908c652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 15 Nov 2022 20:39:19 -0500 Subject: [PATCH] fix: parse `import module, ...` --- packages/babel-parser/src/parser/statement.ts | 8 +-- .../input.mjs | 1 + .../output.json | 52 +++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding-2/input.mjs create mode 100644 packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding-2/output.json diff --git a/packages/babel-parser/src/parser/statement.ts b/packages/babel-parser/src/parser/statement.ts index 60e01e8725ba..b1a54495f771 100644 --- a/packages/babel-parser/src/parser/statement.ts +++ b/packages/babel-parser/src/parser/statement.ts @@ -2747,7 +2747,8 @@ export default abstract class StatementParser extends ExpressionParser { let isImportReflection = false; if (this.isContextual(tt._module)) { const lookahead = this.lookahead(); - if (tokenIsIdentifier(lookahead.type)) { + const nextType = lookahead.type; + if (tokenIsIdentifier(nextType)) { if (lookahead.type !== tt._from) { // import module x isImportReflection = true; @@ -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; } diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding-2/input.mjs b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding-2/input.mjs new file mode 100644 index 000000000000..5ed2aa67042b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding-2/input.mjs @@ -0,0 +1 @@ +import module, { createRequire } from "module"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding-2/output.json b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding-2/output.json new file mode 100644 index 000000000000..862a9f03d3a3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-reflection/valid-module-as-default-binding-2/output.json @@ -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": [] + } +}