diff --git a/packages/babel-generator/src/generators/modules.js b/packages/babel-generator/src/generators/modules.js index a7418b2cff..0906f6829f 100644 --- a/packages/babel-generator/src/generators/modules.js +++ b/packages/babel-generator/src/generators/modules.js @@ -53,6 +53,7 @@ export function ExportAllDeclaration(node: Object) { this.word("from"); this.space(); this.print(node.source, node); + this.printAssertions(node); this.semicolon(); } @@ -131,6 +132,7 @@ function ExportDeclaration(node: Object) { this.word("from"); this.space(); this.print(node.source, node); + this.printAssertions(node); } this.semicolon(); @@ -180,19 +182,10 @@ export function ImportDeclaration(node: Object) { this.print(node.source, node); - if (node.assertions?.length) { - this.space(); - this.word("assert"); - this.space(); - this.token("{"); - this.space(); - this.printList(node.assertions, node); - this.space(); - this.token("}"); - } + this.printAssertions(node); // todo(Babel 8): remove this if branch // `module-attributes` support is discontinued, use `import-assertions` instead. - else if (node.attributes?.length) { + if (node.attributes?.length) { this.space(); this.word("with"); this.space(); diff --git a/packages/babel-generator/src/printer.js b/packages/babel-generator/src/printer.js index 83a0321a0a..4c2dd405ec 100644 --- a/packages/babel-generator/src/printer.js +++ b/packages/babel-generator/src/printer.js @@ -646,6 +646,19 @@ export default class Printer { } } } + + printAssertions(node: Node) { + if (node.assertions?.length) { + this.space(); + this.word("assert"); + this.space(); + this.token("{"); + this.space(); + this.printList(node.assertions, node); + this.space(); + this.token("}"); + } + } } // Expose the node type functions and helpers on the prototype for easy usage. diff --git a/packages/babel-generator/test/fixtures/types/ImportAssertion/input.js b/packages/babel-generator/test/fixtures/types/ImportAssertion/input.js index 7b43091236..0ebcdd146c 100644 --- a/packages/babel-generator/test/fixtures/types/ImportAssertion/input.js +++ b/packages/babel-generator/test/fixtures/types/ImportAssertion/input.js @@ -1 +1,4 @@ -import foo from "foo.json" assert { type: "json" }; +import foo1 from "foo.json" assert { type: "json" }; +export { foo2 } from "foo.json" assert { type: "json" }; +export * from "foo.json" assert { type: "json" }; +export * as foo3 from "foo.json" assert { type: "json" }; diff --git a/packages/babel-generator/test/fixtures/types/ImportAssertion/output.js b/packages/babel-generator/test/fixtures/types/ImportAssertion/output.js index 7b43091236..973863a3e4 100644 --- a/packages/babel-generator/test/fixtures/types/ImportAssertion/output.js +++ b/packages/babel-generator/test/fixtures/types/ImportAssertion/output.js @@ -1 +1,4 @@ -import foo from "foo.json" assert { type: "json" }; +import foo1 from "foo.json" assert { type: "json" }; +export { foo2 } from "foo.json" assert { type: "json" }; +export * from "foo.json" assert { type: "json" }; +export * as foo3 from "foo.json" assert { type: "json" }; \ No newline at end of file diff --git a/packages/babel-parser/ast/spec.md b/packages/babel-parser/ast/spec.md index 85bbbf20cf..e86d9098bd 100644 --- a/packages/babel-parser/ast/spec.md +++ b/packages/babel-parser/ast/spec.md @@ -1364,6 +1364,7 @@ interface ExportNamedDeclaration <: ModuleDeclaration { declaration: Declaration | null; specifiers: [ ExportSpecifier ]; source: StringLiteral | null; + assertions?: [ ImportAttribute ]; } ``` diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 2156734b74..e11e68f5b1 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -1924,6 +1924,11 @@ export default class StatementParser extends ExpressionParser { } } + const assertions = this.maybeParseImportAssertions(); + if (assertions) { + node.assertions = assertions; + } + this.semicolon(); } diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index b245b20bf8..0587b30afe 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -360,6 +360,12 @@ export type Directive = NodeBase & { export type DirectiveLiteral = StringLiteral & { type: "DirectiveLiteral" }; +export type ImportAttribute = NodeBase & { + type: "ImportAttribute", + key: Identifier | StringLiteral, + value: StringLiteral, +}; + // Expressions export type Super = NodeBase & { type: "Super" }; @@ -867,6 +873,8 @@ export type ExportNamedDeclaration = NodeBase & { source: ?Literal, exportKind?: "type" | "value", // TODO: Not in spec + + assertions?: $ReadOnlyArray, }; export type ExportSpecifier = NodeBase & { diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/input.js new file mode 100644 index 0000000000..6af100604f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/input.js @@ -0,0 +1,2 @@ +export { x } from "foo" assert +{ type: "json" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/output.json new file mode 100644 index 0000000000..ceb260046a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/output.json @@ -0,0 +1,63 @@ +{ + "type": "File", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}}, + "program": { + "type": "Program", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}}, + "local": { + "type": "Identifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"x"}, + "name": "x" + }, + "exported": { + "type": "Identifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"x"}, + "name": "x" + } + } + ], + "source": { + "type": "StringLiteral", + "start":18,"end":23,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":23}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "declaration": null, + "assertions": [ + { + "type": "ImportAttribute", + "start":33,"end":45,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}}, + "key": { + "type": "Identifier", + "start":33,"end":37,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":39,"end":45,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":14}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-without-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-without-attributes-identifier/input.js new file mode 100644 index 0000000000..a6db9b5aac --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-without-attributes-identifier/input.js @@ -0,0 +1 @@ +export { x } from "foo" assert; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-without-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-without-attributes-identifier/options.json new file mode 100644 index 0000000000..cf68988c8d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-without-attributes-identifier/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [["importAssertions"]], + "sourceType": "module", + "throws": "Unexpected token (1:30)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/input.js index 647ce93679..1c1e4b79e7 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/input.js +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/input.js @@ -1 +1,2 @@ import foo from "foo.json" assert { for: "for" } +export { foo } from "foo.json" assert { for: "for" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json index f300725af5..31e3705afb 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json @@ -1,12 +1,13 @@ { "type": "File", - "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, + "start":0,"end":101,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":52}}, "errors": [ - "SyntaxError: The only accepted module attribute is `type` (1:36)" + "SyntaxError: The only accepted module attribute is `type` (1:36)", + "SyntaxError: The only accepted module attribute is `type` (2:40)" ], "program": { "type": "Program", - "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, + "start":0,"end":101,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":52}}, "sourceType": "module", "interpreter": null, "body": [ @@ -53,6 +54,56 @@ } } ] + }, + { + "type": "ExportNamedDeclaration", + "start":49,"end":101,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":52}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":58,"end":61,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12}}, + "local": { + "type": "Identifier", + "start":58,"end":61,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":58,"end":61,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":69,"end":79,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":30}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "assertions": [ + { + "type": "ImportAttribute", + "start":89,"end":99,"loc":{"start":{"line":2,"column":40},"end":{"line":2,"column":50}}, + "key": { + "type": "Identifier", + "start":89,"end":92,"loc":{"start":{"line":2,"column":40},"end":{"line":2,"column":43},"identifierName":"for"}, + "name": "for" + }, + "value": { + "type": "StringLiteral", + "start":94,"end":99,"loc":{"start":{"line":2,"column":45},"end":{"line":2,"column":50}}, + "extra": { + "rawValue": "for", + "raw": "\"for\"" + }, + "value": "for" + } + } + ] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js index 7a70cbae8d..910b9aa32d 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js @@ -1 +1,2 @@ import foo from "foo" assert { type: "json", } +export { foo } from "foo" assert { type: "json", } diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/output.json index 30bc21d725..40e9f85e44 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/output.json @@ -1,9 +1,9 @@ { "type": "File", - "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "start":0,"end":97,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}}, "program": { "type": "Program", - "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "start":0,"end":97,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}}, "sourceType": "module", "interpreter": null, "body": [ @@ -50,6 +50,56 @@ } } ] + }, + { + "type": "ExportNamedDeclaration", + "start":47,"end":97,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":50}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":56,"end":59,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12}}, + "local": { + "type": "Identifier", + "start":56,"end":59,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":56,"end":59,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":67,"end":72,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":25}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "declaration": null, + "assertions": [ + { + "type": "ImportAttribute", + "start":82,"end":94,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":47}}, + "key": { + "type": "Identifier", + "start":82,"end":86,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":39},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":88,"end":94,"loc":{"start":{"line":2,"column":41},"end":{"line":2,"column":47}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-as-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-as-with-attributes/input.js new file mode 100644 index 0000000000..f97e9ee257 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-as-with-attributes/input.js @@ -0,0 +1 @@ +export * as foo from "foo.json" assert { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-as-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-as-with-attributes/output.json new file mode 100644 index 0000000000..ca6e53a5a6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-as-with-attributes/output.json @@ -0,0 +1,57 @@ +{ + "type": "File", + "start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}}, + "program": { + "type": "Program", + "start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}}, + "specifiers": [ + { + "type": "ExportNamespaceSpecifier", + "start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}}, + "exported": { + "type": "Identifier", + "start":12,"end":15,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":15},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":21,"end":31,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":31}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start":41,"end":53,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":53}}, + "key": { + "type": "Identifier", + "start":41,"end":45,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":45},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":47,"end":53,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":53}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-with-attributes/input.js new file mode 100644 index 0000000000..b8a5d65808 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-with-attributes/input.js @@ -0,0 +1 @@ +export * from "foo.json" assert { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-with-attributes/output.json new file mode 100644 index 0000000000..0296b20eff --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-star-with-attributes/output.json @@ -0,0 +1,46 @@ +{ + "type": "File", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}}, + "program": { + "type": "Program", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportAllDeclaration", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}}, + "source": { + "type": "StringLiteral", + "start":14,"end":24,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":24}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start":34,"end":46,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":46}}, + "key": { + "type": "Identifier", + "start":34,"end":38,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":38},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":40,"end":46,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":46}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes-and-value/input.js new file mode 100644 index 0000000000..92c6eae4f0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes-and-value/input.js @@ -0,0 +1,2 @@ +export { x } from "foo" assert { type: "json" } +[0] diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes-and-value/output.json new file mode 100644 index 0000000000..94c0069548 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes-and-value/output.json @@ -0,0 +1,82 @@ +{ + "type": "File", + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}}, + "program": { + "type": "Program", + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":47}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}}, + "local": { + "type": "Identifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"x"}, + "name": "x" + }, + "exported": { + "type": "Identifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"x"}, + "name": "x" + } + } + ], + "source": { + "type": "StringLiteral", + "start":18,"end":23,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":23}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "declaration": null, + "assertions": [ + { + "type": "ImportAttribute", + "start":33,"end":45,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":45}}, + "key": { + "type": "Identifier", + "start":33,"end":37,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":37},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":39,"end":45,"loc":{"start":{"line":1,"column":39},"end":{"line":1,"column":45}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + }, + { + "type": "ExpressionStatement", + "start":48,"end":51,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "expression": { + "type": "ArrayExpression", + "start":48,"end":51,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "elements": [ + { + "type": "NumericLiteral", + "start":49,"end":50,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes/input.js new file mode 100644 index 0000000000..598db769da --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes/input.js @@ -0,0 +1 @@ +export { foo } from "foo.json" assert { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes/output.json new file mode 100644 index 0000000000..c9661d2765 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-attributes/output.json @@ -0,0 +1,63 @@ +{ + "type": "File", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}}, + "program": { + "type": "Program", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}}, + "local": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":20,"end":30,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":30}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "assertions": [ + { + "type": "ImportAttribute", + "start":40,"end":52,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":52}}, + "key": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":44},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":46,"end":52,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":52}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-invalid-value/input.js new file mode 100644 index 0000000000..c2aec91e1b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-invalid-value/input.js @@ -0,0 +1 @@ +export { foo } from "foo.json" assert { type: "json", lazy: true, startAtLine: 1 }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-invalid-value/options.json new file mode 100644 index 0000000000..8fde8e5bc0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-invalid-value/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + [ + "importAssertions" + ] + ], + "sourceType": "module", + "throws": "Only string literals are allowed as module attribute values (1:60)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/input.js new file mode 100644 index 0000000000..49542fec11 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/input.js @@ -0,0 +1 @@ +export { foo } from "foo.json" assert { lazy: "true" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/output.json new file mode 100644 index 0000000000..c728fc2231 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/output.json @@ -0,0 +1,66 @@ +{ + "type": "File", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}}, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:40)" + ], + "program": { + "type": "Program", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}}, + "local": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":20,"end":30,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":30}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "assertions": [ + { + "type": "ImportAttribute", + "start":40,"end":52,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":52}}, + "key": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":44},"identifierName":"lazy"}, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start":46,"end":52,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":52}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/input.js new file mode 100644 index 0000000000..408a6c237a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/input.js @@ -0,0 +1 @@ +export { foo } from "foo.json" assert { type: "json", hasOwnProperty: "true" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/output.json new file mode 100644 index 0000000000..7a44aa2b8d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/output.json @@ -0,0 +1,84 @@ +{ + "type": "File", + "start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":79}}, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:54)" + ], + "program": { + "type": "Program", + "start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":79}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":79}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}}, + "local": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":20,"end":30,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":30}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "assertions": [ + { + "type": "ImportAttribute", + "start":40,"end":52,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":52}}, + "key": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":44},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":46,"end":52,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":52}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":54,"end":76,"loc":{"start":{"line":1,"column":54},"end":{"line":1,"column":76}}, + "key": { + "type": "Identifier", + "start":54,"end":68,"loc":{"start":{"line":1,"column":54},"end":{"line":1,"column":68},"identifierName":"hasOwnProperty"}, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start":70,"end":76,"loc":{"start":{"line":1,"column":70},"end":{"line":1,"column":76}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-repeated-type/input.js new file mode 100644 index 0000000000..26c046c2e8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-repeated-type/input.js @@ -0,0 +1 @@ +export { foo } from "foo.json" assert { type: "json", type: "html" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-repeated-type/output.json new file mode 100644 index 0000000000..4c24d416cc --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-repeated-type/output.json @@ -0,0 +1,84 @@ +{ + "type": "File", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "errors": [ + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:54)" + ], + "program": { + "type": "Program", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}}, + "local": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":20,"end":30,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":30}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "assertions": [ + { + "type": "ImportAttribute", + "start":40,"end":52,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":52}}, + "key": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":44},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":46,"end":52,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":52}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":54,"end":66,"loc":{"start":{"line":1,"column":54},"end":{"line":1,"column":66}}, + "key": { + "type": "Identifier", + "start":54,"end":58,"loc":{"start":{"line":1,"column":54},"end":{"line":1,"column":58},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":60,"end":66,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":66}}, + "extra": { + "rawValue": "html", + "raw": "\"html\"" + }, + "value": "html" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-without-attributes/input.js new file mode 100644 index 0000000000..9408920b3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-without-attributes/input.js @@ -0,0 +1 @@ +export { foo } from "foo.json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-without-attributes/output.json new file mode 100644 index 0000000000..bf0d24c287 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-without-attributes/output.json @@ -0,0 +1,44 @@ +{ + "type": "File", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "program": { + "type": "Program", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}}, + "local": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":20,"end":30,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":30}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "assertions": [] + } + ], + "directives": [] + } +} \ No newline at end of file