diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 6369cad4b5..4addc19315 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -1893,6 +1893,9 @@ export default class StatementParser extends ExpressionParser { node.source = null; node.declaration = null; + if (this.hasPlugin("importAssertions")) { + node.assertions = []; + } return true; } @@ -1903,6 +1906,9 @@ export default class StatementParser extends ExpressionParser { if (this.shouldParseExportDeclaration()) { node.specifiers = []; node.source = null; + if (this.hasPlugin("importAssertions")) { + node.assertions = []; + } node.declaration = this.parseExportDeclaration(node); return true; } @@ -2015,12 +2021,8 @@ export default class StatementParser extends ExpressionParser { if (assertions) { node.assertions = assertions; } - } else { - if (expect) { - this.unexpected(); - } else { - node.source = null; - } + } else if (expect) { + this.unexpected(); } this.semicolon(); diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-export-without-from/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-export-without-from/output.json index 8e0546403d..fa605551fa 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-export-without-from/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-export-without-from/output.json @@ -56,7 +56,8 @@ } ], "source": null, - "declaration": null + "declaration": null, + "assertions": [] }, { "type": "ExpressionStatement", diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-class/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-class/input.js new file mode 100644 index 0000000000..223d0a8b39 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-class/input.js @@ -0,0 +1 @@ +export class Foo {} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-class/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-class/output.json new file mode 100644 index 0000000000..44c625806f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-class/output.json @@ -0,0 +1,35 @@ +{ + "type": "File", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "program": { + "type": "Program", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start":7,"end":19,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":19}}, + "id": { + "type": "Identifier", + "start":13,"end":16,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":16},"identifierName":"Foo"}, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":17,"end":19,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":19}}, + "body": [] + } + }, + "assertions": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-function/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-function/input.js new file mode 100644 index 0000000000..f99d427777 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-function/input.js @@ -0,0 +1 @@ +export function foo() {} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-function/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-function/output.json new file mode 100644 index 0000000000..bbcc0210b4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-function/output.json @@ -0,0 +1,38 @@ +{ + "type": "File", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}}, + "program": { + "type": "Program", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}}, + "specifiers": [], + "source": null, + "declaration": { + "type": "FunctionDeclaration", + "start":7,"end":24,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":24}}, + "id": { + "type": "Identifier", + "start":16,"end":19,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":19},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":22,"end":24,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":24}}, + "body": [], + "directives": [] + } + }, + "assertions": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-variable/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-variable/input.js new file mode 100644 index 0000000000..b30bd489f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-variable/input.js @@ -0,0 +1 @@ +export const foo = ""; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-variable/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-variable/output.json new file mode 100644 index 0000000000..715b312bb9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-variable/output.json @@ -0,0 +1,45 @@ +{ + "type": "File", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "program": { + "type": "Program", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start":7,"end":22,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":22}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":13,"end":21,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":21}}, + "id": { + "type": "Identifier", + "start":13,"end":16,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":16},"identifierName":"foo"}, + "name": "foo" + }, + "init": { + "type": "StringLiteral", + "start":19,"end":21,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":21}}, + "extra": { + "rawValue": "", + "raw": "\"\"" + }, + "value": "" + } + } + ], + "kind": "const" + }, + "assertions": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-without-from/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-without-from/input.js new file mode 100644 index 0000000000..b8017e6930 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-without-from/input.js @@ -0,0 +1,3 @@ +const foo = ""; + +export { foo }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-without-from/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-without-from/output.json new file mode 100644 index 0000000000..b6f5226f33 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-export-without-from/output.json @@ -0,0 +1,61 @@ +{ + "type": "File", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":15}}, + "program": { + "type": "Program", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":15}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":14,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":14}}, + "id": { + "type": "Identifier", + "start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"foo"}, + "name": "foo" + }, + "init": { + "type": "StringLiteral", + "start":12,"end":14,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":14}}, + "extra": { + "rawValue": "", + "raw": "\"\"" + }, + "value": "" + } + } + ], + "kind": "const" + }, + { + "type": "ExportNamedDeclaration", + "start":17,"end":32,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":15}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":26,"end":29,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":12}}, + "local": { + "type": "Identifier", + "start":26,"end":29,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":26,"end":29,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": null, + "declaration": null, + "assertions": [] + } + ], + "directives": [] + } +} \ No newline at end of file