From ed15a1301b182ac3cd5afcd78872c00ff9f7af00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 8 Oct 2020 10:57:36 -0400 Subject: [PATCH] fix: restore moduleAttributes support --- .../babel-generator/src/generators/modules.js | 9 ++ .../fixtures/types/ModuleAttributes/input.js | 3 + .../options.json | 4 +- .../{Import2 => ModuleAttributes}/output.js | 1 + .../babel-parser/src/parser/expression.js | 15 ++- packages/babel-parser/src/parser/statement.js | 59 ++++++++++ packages/babel-parser/src/plugin-utils.js | 20 ++++ .../_no-plugin/module-attributes/input.js | 2 +- .../_no-plugin/module-attributes/options.json | 3 + .../dynamic-import-with-valid-syntax/input.js | 1 + .../options.json | 11 ++ .../output.json | 81 +++++++++++++ .../import-with-statement/input.js | 2 + .../import-with-statement/options.json | 12 ++ .../import-with-statement/output.json | 40 +++++++ .../incorrect-arity/input.js | 2 + .../incorrect-arity/options.json | 11 ++ .../incorrect-arity/output.json | 107 +++++++++++++++++ .../input.js | 1 + .../options.json | 11 ++ .../output.json | 48 ++++++++ .../input.js | 2 + .../options.json | 11 ++ .../output.json | 47 ++++++++ .../input.js | 1 + .../options.json | 12 ++ .../trailing-comma}/input.js | 1 + .../trailing-comma/options.json | 11 ++ .../trailing-comma/output.json | 110 ++++++++++++++++++ .../input.js | 2 + .../options.json | 11 ++ .../output.json | 66 +++++++++++ .../valid-syntax-with-attributes/input.js | 1 + .../valid-syntax-with-attributes/options.json | 11 ++ .../valid-syntax-with-attributes/output.json | 57 +++++++++ .../input.js | 1 + .../options.json | 12 ++ .../valid-syntax-with-invalid-value/input.js | 1 + .../options.json | 12 ++ .../input.js | 1 + .../options.json | 11 ++ .../output.json | 60 ++++++++++ .../input.js | 1 + .../options.json | 11 ++ .../output.json | 78 +++++++++++++ .../valid-syntax-with-repeated-type/input.js | 1 + .../options.json | 11 ++ .../output.json | 78 +++++++++++++ .../valid-syntax-without-attributes/input.js | 1 + .../options.json | 11 ++ .../output.json | 38 ++++++ .../module-attributes/without-plugin/input.js | 1 + .../without-plugin/options.json | 4 + 53 files changed, 1113 insertions(+), 6 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/types/ModuleAttributes/input.js rename packages/babel-generator/test/fixtures/types/{Import2 => ModuleAttributes}/options.json (55%) rename packages/babel-generator/test/fixtures/types/{Import2 => ModuleAttributes}/output.js (60%) create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json rename packages/{babel-generator/test/fixtures/types/Import2 => babel-parser/test/fixtures/experimental/module-attributes/trailing-comma}/input.js (72%) create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json diff --git a/packages/babel-generator/src/generators/modules.js b/packages/babel-generator/src/generators/modules.js index 97b7ceeedb03..09733a310e3e 100644 --- a/packages/babel-generator/src/generators/modules.js +++ b/packages/babel-generator/src/generators/modules.js @@ -190,6 +190,15 @@ export function ImportDeclaration(node: Object) { this.space(); this.token("}"); } + // todo(Babel 8): remove this if branch + // `module-attributes` support is discontinued, use `import-assertions` instead. + else if (node.attributes?.length) { + this.space(); + this.word("with"); + this.space(); + this.space(); + this.printList(node.attributes, node); + } this.semicolon(); } diff --git a/packages/babel-generator/test/fixtures/types/ModuleAttributes/input.js b/packages/babel-generator/test/fixtures/types/ModuleAttributes/input.js new file mode 100644 index 000000000000..d415f18fe52c --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ModuleAttributes/input.js @@ -0,0 +1,3 @@ +import "foo.json" with type: "json"; +import("foo.json", { with: { type: "json" } },); + diff --git a/packages/babel-generator/test/fixtures/types/Import2/options.json b/packages/babel-generator/test/fixtures/types/ModuleAttributes/options.json similarity index 55% rename from packages/babel-generator/test/fixtures/types/Import2/options.json rename to packages/babel-generator/test/fixtures/types/ModuleAttributes/options.json index 640775892178..14244aa04de3 100644 --- a/packages/babel-generator/test/fixtures/types/Import2/options.json +++ b/packages/babel-generator/test/fixtures/types/ModuleAttributes/options.json @@ -1,9 +1,9 @@ { "plugins": [ [ - "importAssertions", + "moduleAttributes", { - "version": "september-2020" + "version": "may-2020" } ] ], diff --git a/packages/babel-generator/test/fixtures/types/Import2/output.js b/packages/babel-generator/test/fixtures/types/ModuleAttributes/output.js similarity index 60% rename from packages/babel-generator/test/fixtures/types/Import2/output.js rename to packages/babel-generator/test/fixtures/types/ModuleAttributes/output.js index bee81287dbea..22f50c539fe4 100644 --- a/packages/babel-generator/test/fixtures/types/Import2/output.js +++ b/packages/babel-generator/test/fixtures/types/ModuleAttributes/output.js @@ -1,3 +1,4 @@ +import "foo.json" with type: "json"; import("foo.json", { with: { type: "json" diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 5f1a278bc5fa..0b1be49330de 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -829,13 +829,18 @@ export default class ExpressionParser extends LValParser { ): N.Expression { if (node.callee.type === "Import") { if (node.arguments.length === 2) { - this.expectPlugin("importAssertions"); + // todo(Babel 8): remove the if condition, + // moduleAttributes is renamed to importAssertions + if (!this.hasPlugin("moduleAttributes")) { + this.expectPlugin("importAssertions"); + } } if (node.arguments.length === 0 || node.arguments.length > 2) { this.raise( node.start, Errors.ImportCallArity, - this.hasPlugin("importAssertions") + this.hasPlugin("importAssertions") || + this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument", ); @@ -872,7 +877,11 @@ export default class ExpressionParser extends LValParser { } else { this.expect(tt.comma); if (this.match(close)) { - if (dynamicImport && !this.hasPlugin("importAssertions")) { + if ( + dynamicImport && + !this.hasPlugin("importAssertions") && + !this.hasPlugin("moduleAttributes") + ) { this.raise( this.state.lastTokStart, Errors.ImportCallArgumentTrailingComma, diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index d29dd66d14b5..0c433e614c3c 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2084,6 +2084,14 @@ export default class StatementParser extends ExpressionParser { if (assertions) { node.assertions = assertions; } + // todo(Babel 8): remove module attributes support + else { + const attributes = this.maybeParseModuleAttributes(); + if (attributes) { + node.attributes = attributes; + } + } + this.semicolon(); return this.finishNode(node, "ImportDeclaration"); } @@ -2171,6 +2179,57 @@ export default class StatementParser extends ExpressionParser { return attrs; } + /** + * parse module attributes + * @deprecated It will be removed in Babel 8 + * @returns + * @memberof StatementParser + */ + maybeParseModuleAttributes() { + if (this.match(tt._with) && !this.hasPrecedingLineBreak()) { + this.expectPlugin("moduleAttributes"); + this.next(); + } else { + if (this.hasPlugin("moduleAttributes")) return []; + return null; + } + const attrs = []; + const attributes = new Set(); + do { + const node = this.startNode(); + node.key = this.parseIdentifier(true); + + if (node.key.name !== "type") { + this.raise( + node.key.start, + Errors.ModuleAttributeDifferentFromType, + node.key.name, + ); + } + + if (attributes.has(node.key.name)) { + this.raise( + node.key.start, + Errors.ModuleAttributesWithDuplicateKeys, + node.key.name, + ); + } + attributes.add(node.key.name); + this.expect(tt.colon); + if (!this.match(tt.string)) { + throw this.unexpected( + this.state.start, + Errors.ModuleAttributeInvalidValue, + ); + } + node.value = this.parseLiteral(this.state.value, "StringLiteral"); + this.finishNode(node, "ImportAttribute"); + attrs.push(node); + } while (this.eat(tt.comma)); + + return attrs; + } + maybeParseImportAssertions() { if ( this.match(tt.name) && diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index 1320659ef7c6..9c7198d81076 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -86,6 +86,21 @@ export function validatePlugins(plugins: PluginList) { ); } + if (hasPlugin(plugins, "moduleAttributes")) { + const moduleAttributesVerionPluginOption = getPluginOption( + plugins, + "moduleAttributes", + "version", + ); + if (moduleAttributesVerionPluginOption !== "may-2020") { + throw new Error( + "The 'moduleAttributes' plugin requires a 'version' option," + + " representing the last proposal update. Currently, the" + + " only supported value is 'may-2020'.", + ); + } + } + if (hasPlugin(plugins, "importAssertions")) { const importAssertionsVerionPluginOption = getPluginOption( plugins, @@ -99,6 +114,11 @@ export function validatePlugins(plugins: PluginList) { " only supported value is 'september-2020'.", ); } + if (hasPlugin(plugins, "moduleAttributes")) { + throw new Error( + "Cannot combine importAssertions and moduleAttributes plugins.", + ); + } } if ( hasPlugin(plugins, "recordAndTuple") && diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/input.js index 7b43091236b9..fb31508a0860 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/input.js +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/input.js @@ -1 +1 @@ -import foo from "foo.json" assert { type: "json" }; +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json new file mode 100644 index 000000000000..299e44f15cf8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js new file mode 100644 index 000000000000..baa60fc43c5f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js @@ -0,0 +1 @@ +import("foo.json", { with: { type: "json" } }) diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json new file mode 100644 index 000000000000..eef6b877df49 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json @@ -0,0 +1,81 @@ +{ + "type": "File", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "program": { + "type": "Program", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "expression": { + "type": "CallExpression", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":17,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":17}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + { + "type": "ObjectExpression", + "start":19,"end":45,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":21,"end":43,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":21,"end":25,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":25},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":27,"end":43,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":43}}, + "properties": [ + { + "type": "ObjectProperty", + "start":29,"end":41,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/input.js new file mode 100644 index 000000000000..bfa86f1acde6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/input.js @@ -0,0 +1,2 @@ +import "x" +with ({}); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json new file mode 100644 index 000000000000..54d338484c57 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json @@ -0,0 +1,12 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ], + "estree" + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/output.json new file mode 100644 index 000000000000..cccb9e1b329d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/output.json @@ -0,0 +1,40 @@ +{ + "type": "File", + "start":0,"end":21,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}}, + "errors": [ + "SyntaxError: 'with' in strict mode (2:0)" + ], + "program": { + "type": "Program", + "start":0,"end":21,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "specifiers": [], + "source": { + "type": "Literal", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "value": "x", + "raw": "\"x\"" + }, + "attributes": [] + }, + { + "type": "WithStatement", + "start":11,"end":21,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":10}}, + "object": { + "type": "ObjectExpression", + "start":17,"end":19,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":8}}, + "properties": [] + }, + "body": { + "type": "EmptyStatement", + "start":20,"end":21,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":10}} + } + } + ] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js new file mode 100644 index 000000000000..7f6df49300e8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js @@ -0,0 +1,2 @@ +import(); +import("./foo.json", { with: { type: "json"} }, "unsupported"); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json new file mode 100644 index 000000000000..fbc2d14ad598 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json @@ -0,0 +1,107 @@ +{ + "type": "File", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":63}}, + "errors": [ + "SyntaxError: import() requires exactly one or two arguments (1:0)", + "SyntaxError: import() requires exactly one or two arguments (2:0)" + ], + "program": { + "type": "Program", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":63}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}}, + "expression": { + "type": "CallExpression", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + }, + "arguments": [] + } + }, + { + "type": "ExpressionStatement", + "start":10,"end":73,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":63}}, + "expression": { + "type": "CallExpression", + "start":10,"end":72,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":62}}, + "callee": { + "type": "Import", + "start":10,"end":16,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":17,"end":29,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":19}}, + "extra": { + "rawValue": "./foo.json", + "raw": "\"./foo.json\"" + }, + "value": "./foo.json" + }, + { + "type": "ObjectExpression", + "start":31,"end":56,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":46}}, + "properties": [ + { + "type": "ObjectProperty", + "start":33,"end":54,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":44}}, + "method": false, + "key": { + "type": "Identifier", + "start":33,"end":37,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":27},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":39,"end":54,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":44}}, + "properties": [ + { + "type": "ObjectProperty", + "start":41,"end":53,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":41,"end":45,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":47,"end":53,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + }, + { + "type": "StringLiteral", + "start":58,"end":71,"loc":{"start":{"line":2,"column":48},"end":{"line":2,"column":61}}, + "extra": { + "rawValue": "unsupported", + "raw": "\"unsupported\"" + }, + "value": "unsupported" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js new file mode 100644 index 000000000000..0a9420c378f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js @@ -0,0 +1 @@ +import("./foo.json", ...[]); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json new file mode 100644 index 000000000000..f016f97336e9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json @@ -0,0 +1,48 @@ +{ + "type": "File", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "errors": [ + "SyntaxError: ... is not allowed in import() (1:21)" + ], + "program": { + "type": "Program", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "expression": { + "type": "CallExpression", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":19,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":19}}, + "extra": { + "rawValue": "./foo.json", + "raw": "\"./foo.json\"" + }, + "value": "./foo.json" + }, + { + "type": "SpreadElement", + "start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}}, + "argument": { + "type": "ArrayExpression", + "start":24,"end":26,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":26}}, + "elements": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js new file mode 100644 index 000000000000..4fb4f13bf6f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js @@ -0,0 +1,2 @@ +import "x" with +type: "json" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json new file mode 100644 index 000000000000..9dcc39ab57b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json @@ -0,0 +1,47 @@ +{ + "type": "File", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "program": { + "type": "Program", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":16,"end":28,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "key": { + "type": "Identifier", + "start":16,"end":20,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":4},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":22,"end":28,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":12}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js new file mode 100644 index 000000000000..af54005dade7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js @@ -0,0 +1 @@ +import "x" with; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json new file mode 100644 index 000000000000..49737184d7fd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json @@ -0,0 +1,12 @@ +{ + "throws": "Unexpected token (1:15)", + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-generator/test/fixtures/types/Import2/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js similarity index 72% rename from packages/babel-generator/test/fixtures/types/Import2/input.js rename to packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js index f01b3a7ec608..0be461789922 100644 --- a/packages/babel-generator/test/fixtures/types/Import2/input.js +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js @@ -1 +1,2 @@ +import("foo.js",); import("foo.json", { with: { type: "json" } },); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json new file mode 100644 index 000000000000..599f4fe5488d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json @@ -0,0 +1,110 @@ +{ + "type": "File", + "start":0,"end":67,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":48}}, + "program": { + "type": "Program", + "start":0,"end":67,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":48}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, + "expression": { + "type": "CallExpression", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + }, + "extra": { + "trailingComma": 15 + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}}, + "extra": { + "rawValue": "foo.js", + "raw": "\"foo.js\"" + }, + "value": "foo.js" + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":19,"end":67,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":48}}, + "expression": { + "type": "CallExpression", + "start":19,"end":66,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":47}}, + "callee": { + "type": "Import", + "start":19,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}} + }, + "extra": { + "trailingComma": 64 + }, + "arguments": [ + { + "type": "StringLiteral", + "start":26,"end":36,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":17}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + { + "type": "ObjectExpression", + "start":38,"end":64,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":40,"end":62,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":25},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":46,"end":62,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":43}}, + "properties": [ + { + "type": "ObjectProperty", + "start":48,"end":60,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":48,"end":52,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":33},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":54,"end":60,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":41}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js new file mode 100644 index 000000000000..ec62816617cd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js @@ -0,0 +1,2 @@ +import "x" with type: "json" +[0] diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json new file mode 100644 index 000000000000..e830da53a1c5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json @@ -0,0 +1,66 @@ +{ + "type": "File", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}}, + "program": { + "type": "Program", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":16,"end":28,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":28}}, + "key": { + "type": "Identifier", + "start":16,"end":20,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":20},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":22,"end":28,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":28}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + }, + { + "type": "ExpressionStatement", + "start":29,"end":32,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "expression": { + "type": "ArrayExpression", + "start":29,"end":32,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "elements": [ + { + "type": "NumericLiteral", + "start":30,"end":31,"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/module-attributes/valid-syntax-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json new file mode 100644 index 000000000000..dd150dd9dbb1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json @@ -0,0 +1,57 @@ +{ + "type": "File", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "program": { + "type": "Program", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, + "key": { + "type": "Identifier", + "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json new file mode 100644 index 000000000000..a8be45900fa0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json @@ -0,0 +1,12 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "jan-2020" + } + ] + ], + "sourceType": "module", + "throws": "The 'moduleAttributes' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'may-2020'." +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js new file mode 100644 index 000000000000..cfb5372f6b12 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", lazy: true, startAtLine: 1; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json new file mode 100644 index 000000000000..f21e649b0f1d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json @@ -0,0 +1,12 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module", + "throws": "Only string literals are allowed as module attribute values (1:52)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js new file mode 100644 index 000000000000..c2ae7496dcb7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with lazy: "true"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json new file mode 100644 index 000000000000..a3bbfd621562 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json @@ -0,0 +1,60 @@ +{ + "type": "File", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:32)" + ], + "program": { + "type": "Program", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, + "key": { + "type": "Identifier", + "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"lazy"}, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js new file mode 100644 index 000000000000..d027b991db49 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", hasOwnProperty: "true"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json new file mode 100644 index 000000000000..12af675ae8b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json @@ -0,0 +1,78 @@ +{ + "type": "File", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:46)" + ], + "program": { + "type": "Program", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, + "key": { + "type": "Identifier", + "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":46,"end":68,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":68}}, + "key": { + "type": "Identifier", + "start":46,"end":60,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":60},"identifierName":"hasOwnProperty"}, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start":62,"end":68,"loc":{"start":{"line":1,"column":62},"end":{"line":1,"column":68}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js new file mode 100644 index 000000000000..856373edcf34 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json", type: "html"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json new file mode 100644 index 000000000000..ad53ece6e4dc --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json @@ -0,0 +1,78 @@ +{ + "type": "File", + "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}}, + "errors": [ + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:46)" + ], + "program": { + "type": "Program", + "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, + "key": { + "type": "Identifier", + "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":46,"end":58,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":58}}, + "key": { + "type": "Identifier", + "start":46,"end":50,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":50},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":52,"end":58,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":58}}, + "extra": { + "rawValue": "html", + "raw": "\"html\"" + }, + "value": "html" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js new file mode 100644 index 000000000000..92901ed56b38 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json new file mode 100644 index 000000000000..14244aa04de3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "moduleAttributes", + { + "version": "may-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json new file mode 100644 index 000000000000..9a295afe63f1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json @@ -0,0 +1,38 @@ +{ + "type": "File", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, + "program": { + "type": "Program", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json new file mode 100644 index 000000000000..8645af2af8c7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json @@ -0,0 +1,4 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", + "plugins": [] +} \ No newline at end of file