diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index a2826e139f..202d7cc7e0 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -919,7 +919,10 @@ export default class ExpressionParser extends LValParser { this.expectPlugin("dynamicImport", node.start); if (!this.match(tt.parenL)) { - this.unexpected(null, tt.parenL); + this.raise( + this.state.lastTokStart, + "import can only be used in import() or import.meta", + ); } return this.finishNode(node, "Import"); case tt._this: diff --git a/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/input.js b/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/input.js new file mode 100644 index 0000000000..477eec2654 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/input.js @@ -0,0 +1 @@ +(import) diff --git a/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/options.json b/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/options.json new file mode 100644 index 0000000000..1629df1f36 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["dynamicImport"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/output.json b/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/output.json new file mode 100644 index 0000000000..fe56e0811a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/output.json @@ -0,0 +1,72 @@ +{ + "type": "File", + "start": 0, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "errors": [ + "SyntaxError: import can only be used in import() or import.meta (1:1)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "expression": { + "type": "Import", + "start": 1, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file