From 452000cf732171a4bd4e944fb396e5d91106ee31 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Mon, 31 Aug 2020 12:34:32 +0100 Subject: [PATCH 01/10] Rename module-attributes to import-assertions --- .../src/parser/util/missing-plugin-helper.js | 6 +- .../babel-generator/src/generators/modules.js | 10 ++- .../test/fixtures/types/Import2/options.json | 4 +- .../fixtures/types/ImportAssertion/input.js | 1 + .../types/ImportAssertion}/options.json | 4 +- .../fixtures/types/ImportAssertion/output.js | 1 + .../fixtures/types/ImportAttribute/input.js | 1 - .../fixtures/types/ImportAttribute/output.js | 1 - packages/babel-parser/ast/spec.md | 2 +- .../babel-parser/src/parser/expression.js | 6 +- packages/babel-parser/src/parser/statement.js | 76 +++++++++---------- packages/babel-parser/src/plugin-utils.js | 12 +-- .../module-attributes-dynamic/options.json | 4 +- .../_no-plugin/module-attributes/input.js | 2 +- .../_no-plugin/module-attributes/options.json | 4 +- .../dynamic-import-with-valid-syntax/input.js | 1 + .../options.json | 4 +- .../output.json | 24 +++--- .../import-with-statement/input.js | 2 + .../import-with-statement/options.json | 4 +- .../import-with-statement/output.json | 45 +++++++++++ .../incorrect-arity/input.js | 2 + .../incorrect-arity}/options.json | 4 +- .../incorrect-arity/output.json | 26 +++---- .../input.js | 0 .../options.json | 4 +- .../output.json | 0 .../input.js | 2 + .../options.json | 11 +++ .../output.json | 14 ++-- .../input.js | 1 + .../options.json | 12 +++ .../import-assertions/trailing-comma/input.js | 2 + .../trailing-comma/options.json | 11 +++ .../trailing-comma/output.json | 26 +++---- .../input.js | 2 + .../options.json | 11 +++ .../output.json | 20 ++--- .../valid-syntax-with-attributes/input.js | 1 + .../valid-syntax-with-attributes/options.json | 11 +++ .../valid-syntax-with-attributes/output.json | 14 ++-- .../input.js | 1 + .../options.json | 6 +- .../valid-syntax-with-invalid-value/input.js | 1 + .../options.json | 8 +- .../input.js | 1 + .../options.json | 11 +++ .../output.json | 17 ++--- .../input.js | 1 + .../options.json | 11 +++ .../output.json | 23 +++--- .../valid-syntax-with-repeated-type/input.js | 1 + .../options.json | 11 +++ .../output.json | 23 +++--- .../valid-syntax-without-attributes/input.js | 0 .../options.json | 11 +++ .../output.json | 2 +- .../import-assertions/without-plugin/input.js | 1 + .../without-plugin/options.json | 4 +- .../dynamic-import-with-valid-syntax/input.js | 1 - .../import-with-statement/input.js | 2 - .../import-with-statement/output.json | 40 ---------- .../incorrect-arity/input.js | 2 - .../input.js | 2 - .../options.json | 11 --- .../input.js | 1 - .../options.json | 12 --- .../module-attributes/trailing-comma/input.js | 2 - .../trailing-comma/options.json | 11 --- .../input.js | 2 - .../options.json | 11 --- .../valid-syntax-with-attributes/input.js | 1 - .../valid-syntax-with-attributes/options.json | 11 --- .../input.js | 1 - .../valid-syntax-with-invalid-value/input.js | 1 - .../input.js | 1 - .../options.json | 11 --- .../input.js | 1 - .../options.json | 11 --- .../valid-syntax-with-repeated-type/input.js | 1 - .../options.json | 11 --- .../options.json | 11 --- .../module-attributes/without-plugin/input.js | 1 - .../babel-parser/typings/babel-parser.d.ts | 2 +- .../.npmignore | 0 .../README.md | 19 +++++ .../package.json | 6 +- .../src/index.js | 10 +-- .../README.md | 19 ----- packages/babel-standalone/package.json | 3 +- .../scripts/pluginConfig.json | 2 +- .../babel-standalone/src/generated/plugins.js | 6 +- .../babel-standalone/src/preset-stage-0.js | 4 +- .../babel-standalone/src/preset-stage-1.js | 6 +- packages/babel-standalone/test/babel.js | 2 +- .../src/definitions/experimental.js | 2 +- yarn.lock | 28 ++----- 97 files changed, 388 insertions(+), 401 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/types/ImportAssertion/input.js rename packages/{babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax => babel-generator/test/fixtures/types/ImportAssertion}/options.json (55%) create mode 100644 packages/babel-generator/test/fixtures/types/ImportAssertion/output.js delete mode 100644 packages/babel-generator/test/fixtures/types/ImportAttribute/input.js delete mode 100644 packages/babel-generator/test/fixtures/types/ImportAttribute/output.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/input.js rename packages/{babel-generator/test/fixtures/types/ImportAttribute => babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax}/options.json (55%) rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/dynamic-import-with-valid-syntax/output.json (62%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/input.js rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/import-with-statement/options.json (59%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/input.js rename packages/babel-parser/test/fixtures/experimental/{module-attributes/invalid-spread-element-import-call => import-assertions/incorrect-arity}/options.json (55%) rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/incorrect-arity/output.json (69%) rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/invalid-spread-element-import-call/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/{module-attributes/incorrect-arity => import-assertions/invalid-spread-element-import-call}/options.json (55%) rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/invalid-spread-element-import-call/output.json (100%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/options.json rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/invalid-syntax-with-attributes-multiple-lines/output.json (59%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/options.json rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/trailing-comma/output.json (70%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/options.json rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-with-attributes-and-value/output.json (64%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/options.json rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-with-attributes/output.json (67%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/input.js rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-with-invalid-plugin-option/options.json (56%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/input.js rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-with-invalid-value/options.json (63%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/options.json rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-with-no-type-attribute/output.json (64%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/options.json rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-with-object-method-attribute/output.json (62%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/options.json rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-with-repeated-type/output.json (61%) rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-without-attributes/input.js (100%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/options.json rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/valid-syntax-without-attributes/output.json (97%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/input.js rename packages/babel-parser/test/fixtures/experimental/{module-attributes => import-assertions}/without-plugin/options.json (66%) delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/output.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js rename packages/{babel-plugin-syntax-module-attributes => babel-plugin-syntax-import-assertions}/.npmignore (100%) create mode 100644 packages/babel-plugin-syntax-import-assertions/README.md rename packages/{babel-plugin-syntax-module-attributes => babel-plugin-syntax-import-assertions}/package.json (68%) rename packages/{babel-plugin-syntax-module-attributes => babel-plugin-syntax-import-assertions}/src/index.js (51%) delete mode 100644 packages/babel-plugin-syntax-module-attributes/README.md diff --git a/packages/babel-core/src/parser/util/missing-plugin-helper.js b/packages/babel-core/src/parser/util/missing-plugin-helper.js index 10b813ee756d..2a8b7ba68dfc 100644 --- a/packages/babel-core/src/parser/util/missing-plugin-helper.js +++ b/packages/babel-core/src/parser/util/missing-plugin-helper.js @@ -129,10 +129,10 @@ const pluginNameMap = { url: "https://git.io/JfeDR", }, }, - moduleAttributes: { + importAssertions: { syntax: { - name: "@babel/plugin-syntax-module-attributes", - url: "https://git.io/JfK3k", + name: "@babel/plugin-syntax-import-assertions", + url: "TODO", }, }, moduleStringNames: { diff --git a/packages/babel-generator/src/generators/modules.js b/packages/babel-generator/src/generators/modules.js index 0d398f801c53..97b7ceeedb03 100644 --- a/packages/babel-generator/src/generators/modules.js +++ b/packages/babel-generator/src/generators/modules.js @@ -180,11 +180,15 @@ export function ImportDeclaration(node: Object) { this.print(node.source, node); - if (node.attributes?.length) { + if (node.assertions?.length) { this.space(); - this.word("with"); + this.word("assert"); this.space(); - this.printList(node.attributes, node); + this.token("{"); + this.space(); + this.printList(node.assertions, node); + this.space(); + this.token("}"); } this.semicolon(); diff --git a/packages/babel-generator/test/fixtures/types/Import2/options.json b/packages/babel-generator/test/fixtures/types/Import2/options.json index 14244aa04de3..640775892178 100644 --- a/packages/babel-generator/test/fixtures/types/Import2/options.json +++ b/packages/babel-generator/test/fixtures/types/Import2/options.json @@ -1,9 +1,9 @@ { "plugins": [ [ - "moduleAttributes", + "importAssertions", { - "version": "may-2020" + "version": "september-2020" } ] ], diff --git a/packages/babel-generator/test/fixtures/types/ImportAssertion/input.js b/packages/babel-generator/test/fixtures/types/ImportAssertion/input.js new file mode 100644 index 000000000000..7b43091236b9 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAssertion/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json b/packages/babel-generator/test/fixtures/types/ImportAssertion/options.json similarity index 55% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json rename to packages/babel-generator/test/fixtures/types/ImportAssertion/options.json index 14244aa04de3..640775892178 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/options.json +++ b/packages/babel-generator/test/fixtures/types/ImportAssertion/options.json @@ -1,9 +1,9 @@ { "plugins": [ [ - "moduleAttributes", + "importAssertions", { - "version": "may-2020" + "version": "september-2020" } ] ], diff --git a/packages/babel-generator/test/fixtures/types/ImportAssertion/output.js b/packages/babel-generator/test/fixtures/types/ImportAssertion/output.js new file mode 100644 index 000000000000..7b43091236b9 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAssertion/output.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { type: "json" }; diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute/input.js b/packages/babel-generator/test/fixtures/types/ImportAttribute/input.js deleted file mode 100644 index fb31508a0860..000000000000 --- a/packages/babel-generator/test/fixtures/types/ImportAttribute/input.js +++ /dev/null @@ -1 +0,0 @@ -import foo from "foo.json" with type: "json"; diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute/output.js b/packages/babel-generator/test/fixtures/types/ImportAttribute/output.js deleted file mode 100644 index fb31508a0860..000000000000 --- a/packages/babel-generator/test/fixtures/types/ImportAttribute/output.js +++ /dev/null @@ -1 +0,0 @@ -import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/ast/spec.md b/packages/babel-parser/ast/spec.md index 5d23fa7ded35..9c0cf41ac306 100644 --- a/packages/babel-parser/ast/spec.md +++ b/packages/babel-parser/ast/spec.md @@ -1292,7 +1292,7 @@ interface ImportDeclaration <: ModuleDeclaration { importKind: null | "type" | "typeof" | "value"; specifiers: [ ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ]; source: StringLiteral; - attributes?: [ ImportAttribute ]; + assertions?: [ ImportAttribute ]; } ``` diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 3e48b00945e0..de48216fe962 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -829,13 +829,13 @@ export default class ExpressionParser extends LValParser { ): N.Expression { if (node.callee.type === "Import") { if (node.arguments.length === 2) { - this.expectPlugin("moduleAttributes"); + this.expectPlugin("importAssertions"); } if (node.arguments.length === 0 || node.arguments.length > 2) { this.raise( node.start, Errors.ImportCallArity, - this.hasPlugin("moduleAttributes") + this.hasPlugin("importAssertions") ? "one or two arguments" : "one argument", ); @@ -872,7 +872,7 @@ export default class ExpressionParser extends LValParser { } else { this.expect(tt.comma); if (this.match(close)) { - if (dynamicImport && !this.hasPlugin("moduleAttributes")) { + if (dynamicImport && !this.hasPlugin("importAssertions")) { 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 425bc7f0ec28..3f1f06415ced 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2109,11 +2109,12 @@ export default class StatementParser extends ExpressionParser { this.expectContextual("from"); } node.source = this.parseImportSource(); - // https://github.com/tc39/proposal-module-attributes - // parse module attributes if the next token is `with` or ignore and finish the ImportDeclaration node. - const attributes = this.maybeParseModuleAttributes(); - if (attributes) { - node.attributes = attributes; + // https://github.com/tc39/proposal-import-assertions + // parse module import assertions if the next token is `assert` or ignore + // and finish the ImportDeclaration node. + const assertions = this.maybeParseImportAssertions(); + if (assertions) { + node.assertions = assertions; } this.semicolon(); return this.finishNode(node, "ImportDeclaration"); @@ -2145,45 +2146,24 @@ export default class StatementParser extends ExpressionParser { node.specifiers.push(this.finishNode(specifier, type)); } - maybeParseModuleAttributes() { - if (this.match(tt._with) && !this.hasPrecedingLineBreak()) { - this.expectPlugin("moduleAttributes"); - this.next(); - } else { - if (this.hasPlugin("moduleAttributes")) return []; - return null; - } + parseAssertEntries() { + this.expectPlugin("importAssertions"); + const attrs = []; - const attributes = new Set(); + do { - // we are trying to parse a node which has the following syntax - // with type: "json" - // [with -> keyword], [type -> Identifier], [":" -> token for colon], ["json" -> StringLiteral] const node = this.startNode(); - node.key = this.parseIdentifier(true); - // for now we are only allowing `type` as the only allowed module attribute - if (node.key.name !== "type") { - this.raise( - node.key.start, - Errors.ModuleAttributeDifferentFromType, - node.key.name, - ); + // parse AssertionKey : IdentifierName, StringLiteral + let assertionKeyNode; + if (this.match(tt.string)) { + assertionKeyNode = this.parseLiteral(this.state.value, "StringLiteral"); + } else { + assertionKeyNode = this.parseIdentifier(); } + this.next(); + node.key = assertionKeyNode; - // check if we already have an entry for an attribute - // if a duplicate entry is found, throw an error - // for now this logic will come into play only when someone declares `type` twice - 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); - // check if the value set to the module attribute is a string as we only allow string literals if (!this.match(tt.string)) { throw this.unexpected( this.state.start, @@ -2198,6 +2178,26 @@ export default class StatementParser extends ExpressionParser { return attrs; } + maybeParseImportAssertions() { + if ( + this.match(tt.name) && + this.state.value === "assert" && + !this.hasPrecedingLineBreak() + ) { + this.expectPlugin("importAssertions"); + this.next(); + } else { + if (this.hasPlugin("importAssertions")) return []; + return null; + } + + this.eat(tt.braceL); + const attrs = this.parseAssertEntries(); + this.eat(tt.braceR); + + return attrs; + } + maybeParseDefaultImportSpecifier(node: N.ImportDeclaration): boolean { if (this.shouldParseDefaultImport(node)) { // import defaultObj, { x, y as z } from '...' diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index f87f37fe3449..1320659ef7c6 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -86,17 +86,17 @@ export function validatePlugins(plugins: PluginList) { ); } - if (hasPlugin(plugins, "moduleAttributes")) { - const moduleAttributesVerionPluginOption = getPluginOption( + if (hasPlugin(plugins, "importAssertions")) { + const importAssertionsVerionPluginOption = getPluginOption( plugins, - "moduleAttributes", + "importAssertions", "version", ); - if (moduleAttributesVerionPluginOption !== "may-2020") { + if (importAssertionsVerionPluginOption !== "september-2020") { throw new Error( - "The 'moduleAttributes' plugin requires a 'version' option," + + "The 'importAssertions' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + - " only supported value is 'may-2020'.", + " only supported value is 'september-2020'.", ); } } diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json index 6b7622b8eeb2..7dc0fb837dff 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json @@ -1,5 +1,5 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:24)", + "throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:24)", "sourceType": "module", "plugins": [] -} \ No newline at end of file +} 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 fb31508a0860..7b43091236b9 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" with type: "json"; +import foo from "foo.json" assert { 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 index 3f9b696c8de9..b9144a3ad2cc 100644 --- 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 @@ -1,5 +1,5 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", + "throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:27)", "sourceType": "module", "plugins": [] -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/input.js new file mode 100644 index 000000000000..481c52e1c75f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/input.js @@ -0,0 +1 @@ +import("foo.json", { assert: { type: "json" } }) diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/options.json similarity index 55% rename from packages/babel-generator/test/fixtures/types/ImportAttribute/options.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/options.json index 14244aa04de3..640775892178 100644 --- a/packages/babel-generator/test/fixtures/types/ImportAttribute/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/options.json @@ -1,9 +1,9 @@ { "plugins": [ [ - "moduleAttributes", + "importAssertions", { - "version": "may-2020" + "version": "september-2020" } ] ], 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/import-assertions/dynamic-import-with-valid-syntax/output.json similarity index 62% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/output.json index eef6b877df49..5762113a14b2 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/output.json @@ -1,18 +1,18 @@ { "type": "File", - "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, "program": { "type": "Program", - "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ExpressionStatement", - "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, "expression": { "type": "CallExpression", - "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, "callee": { "type": "Import", "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} @@ -29,37 +29,37 @@ }, { "type": "ObjectExpression", - "start":19,"end":45,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":45}}, + "start":19,"end":47,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":47}}, "properties": [ { "type": "ObjectProperty", - "start":21,"end":43,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":43}}, + "start":21,"end":45,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":45}}, "method": false, "key": { "type": "Identifier", - "start":21,"end":25,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":25},"identifierName":"with"}, - "name": "with" + "start":21,"end":27,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":27},"identifierName":"assert"}, + "name": "assert" }, "computed": false, "shorthand": false, "value": { "type": "ObjectExpression", - "start":27,"end":43,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":43}}, + "start":29,"end":45,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":45}}, "properties": [ { "type": "ObjectProperty", - "start":29,"end":41,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":41}}, + "start":31,"end":43,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":43}}, "method": false, "key": { "type": "Identifier", - "start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33},"identifierName":"type"}, + "start":31,"end":35,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":35},"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}}, + "start":37,"end":43,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":43}}, "extra": { "rawValue": "json", "raw": "\"json\"" diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/input.js new file mode 100644 index 000000000000..5b6c5fa3373a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/input.js @@ -0,0 +1,2 @@ +import "x" +assert ({}); diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/options.json similarity index 59% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/options.json index 54d338484c57..3876aba950da 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/options.json @@ -1,9 +1,9 @@ { "plugins": [ [ - "moduleAttributes", + "importAssertions", { - "version": "may-2020" + "version": "september-2020" } ], "estree" diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/output.json new file mode 100644 index 000000000000..f845790255b7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/output.json @@ -0,0 +1,45 @@ +{ + "type": "File", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "program": { + "type": "Program", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "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\"" + }, + "assertions": [] + }, + { + "type": "ExpressionStatement", + "start":11,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "expression": { + "type": "CallExpression", + "start":11,"end":22,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":11}}, + "callee": { + "type": "Identifier", + "start":11,"end":17,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6},"identifierName":"assert"}, + "name": "assert" + }, + "arguments": [ + { + "type": "ObjectExpression", + "start":19,"end":21,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":10}}, + "properties": [] + } + ], + "optional": false + } + } + ] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/input.js new file mode 100644 index 000000000000..0863c2ee4884 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/input.js @@ -0,0 +1,2 @@ +import(); +import("./foo.json", { assert: { type: "json"} }, "unsupported"); 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/import-assertions/incorrect-arity/options.json similarity index 55% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/options.json index 14244aa04de3..640775892178 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/options.json @@ -1,9 +1,9 @@ { "plugins": [ [ - "moduleAttributes", + "importAssertions", { - "version": "may-2020" + "version": "september-2020" } ] ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/output.json similarity index 69% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/output.json index fbc2d14ad598..de469d3cab1e 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/output.json @@ -1,13 +1,13 @@ { "type": "File", - "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":63}}, + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":65}}, "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}}, + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":65}}, "sourceType": "module", "interpreter": null, "body": [ @@ -26,10 +26,10 @@ }, { "type": "ExpressionStatement", - "start":10,"end":73,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":63}}, + "start":10,"end":75,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":65}}, "expression": { "type": "CallExpression", - "start":10,"end":72,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":62}}, + "start":10,"end":74,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":64}}, "callee": { "type": "Import", "start":10,"end":16,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}} @@ -46,37 +46,37 @@ }, { "type": "ObjectExpression", - "start":31,"end":56,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":46}}, + "start":31,"end":58,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":48}}, "properties": [ { "type": "ObjectProperty", - "start":33,"end":54,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":44}}, + "start":33,"end":56,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":46}}, "method": false, "key": { "type": "Identifier", - "start":33,"end":37,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":27},"identifierName":"with"}, - "name": "with" + "start":33,"end":39,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":29},"identifierName":"assert"}, + "name": "assert" }, "computed": false, "shorthand": false, "value": { "type": "ObjectExpression", - "start":39,"end":54,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":44}}, + "start":41,"end":56,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":46}}, "properties": [ { "type": "ObjectProperty", - "start":41,"end":53,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}}, + "start":43,"end":55,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":45}}, "method": false, "key": { "type": "Identifier", - "start":41,"end":45,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"identifierName":"type"}, + "start":43,"end":47,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":37},"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}}, + "start":49,"end":55,"loc":{"start":{"line":2,"column":39},"end":{"line":2,"column":45}}, "extra": { "rawValue": "json", "raw": "\"json\"" @@ -91,7 +91,7 @@ }, { "type": "StringLiteral", - "start":58,"end":71,"loc":{"start":{"line":2,"column":48},"end":{"line":2,"column":61}}, + "start":60,"end":73,"loc":{"start":{"line":2,"column":50},"end":{"line":2,"column":63}}, "extra": { "rawValue": "unsupported", "raw": "\"unsupported\"" 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/import-assertions/invalid-spread-element-import-call/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/input.js rename to packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/options.json similarity index 55% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/options.json index 14244aa04de3..640775892178 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/options.json @@ -1,9 +1,9 @@ { "plugins": [ [ - "moduleAttributes", + "importAssertions", { - "version": "may-2020" + "version": "september-2020" } ] ], 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/import-assertions/invalid-spread-element-import-call/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-spread-element-import-call/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/input.js new file mode 100644 index 000000000000..92c0d33eddd0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/input.js @@ -0,0 +1,2 @@ +import "x" assert +{ type: "json" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-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/import-assertions/invalid-syntax-with-attributes-multiple-lines/output.json similarity index 59% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/output.json index 9dcc39ab57b9..772136f80389 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/output.json @@ -1,15 +1,15 @@ { "type": "File", - "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}}, "program": { "type": "Program", - "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}}, "specifiers": [], "source": { "type": "StringLiteral", @@ -20,18 +20,18 @@ }, "value": "x" }, - "attributes": [ + "assertions": [ { "type": "ImportAttribute", - "start":16,"end":28,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "start":20,"end":32,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}}, "key": { "type": "Identifier", - "start":16,"end":20,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":4},"identifierName":"type"}, + "start":20,"end":24,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start":22,"end":28,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":12}}, + "start":26,"end":32,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":14}}, "extra": { "rawValue": "json", "raw": "\"json\"" diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/input.js new file mode 100644 index 000000000000..e2920266c8ff --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/input.js @@ -0,0 +1 @@ +import "x" assert; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json new file mode 100644 index 000000000000..3e9b35f35878 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json @@ -0,0 +1,12 @@ +{ + "throws": "Unexpected token (1:17)", + "plugins": [ + [ + "importAssertions", + { + "version": "september-2020" + } + ] + ], + "sourceType": "module" +} 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 new file mode 100644 index 000000000000..b448aa280c34 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js @@ -0,0 +1,2 @@ +import("foo.js",); +import("foo.json", { assert: { type: "json" } },); diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-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/import-assertions/trailing-comma/output.json similarity index 70% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/output.json index 599f4fe5488d..832615ebadd2 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/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":67,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":48}}, + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}}, "program": { "type": "Program", - "start":0,"end":67,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":48}}, + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}}, "sourceType": "module", "interpreter": null, "body": [ @@ -35,16 +35,16 @@ }, { "type": "ExpressionStatement", - "start":19,"end":67,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":48}}, + "start":19,"end":69,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":50}}, "expression": { "type": "CallExpression", - "start":19,"end":66,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":47}}, + "start":19,"end":68,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":49}}, "callee": { "type": "Import", "start":19,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}} }, "extra": { - "trailingComma": 64 + "trailingComma": 66 }, "arguments": [ { @@ -58,37 +58,37 @@ }, { "type": "ObjectExpression", - "start":38,"end":64,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":45}}, + "start":38,"end":66,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":47}}, "properties": [ { "type": "ObjectProperty", - "start":40,"end":62,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":43}}, + "start":40,"end":64,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":45}}, "method": false, "key": { "type": "Identifier", - "start":40,"end":44,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":25},"identifierName":"with"}, - "name": "with" + "start":40,"end":46,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":27},"identifierName":"assert"}, + "name": "assert" }, "computed": false, "shorthand": false, "value": { "type": "ObjectExpression", - "start":46,"end":62,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":43}}, + "start":48,"end":64,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":45}}, "properties": [ { "type": "ObjectProperty", - "start":48,"end":60,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":41}}, + "start":50,"end":62,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}}, "method": false, "key": { "type": "Identifier", - "start":48,"end":52,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":33},"identifierName":"type"}, + "start":50,"end":54,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"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}}, + "start":56,"end":62,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}}, "extra": { "rawValue": "json", "raw": "\"json\"" diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/input.js new file mode 100644 index 000000000000..4f1ee63fb3a6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/input.js @@ -0,0 +1,2 @@ +import "x" assert { type: "json" } +[0] diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-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/import-assertions/valid-syntax-with-attributes-and-value/output.json similarity index 64% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/output.json index e830da53a1c5..e83db8d7602a 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/output.json @@ -1,15 +1,15 @@ { "type": "File", - "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}}, + "start":0,"end":38,"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}}, + "start":0,"end":38,"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}}, + "start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":34}}, "specifiers": [], "source": { "type": "StringLiteral", @@ -20,18 +20,18 @@ }, "value": "x" }, - "attributes": [ + "assertions": [ { "type": "ImportAttribute", - "start":16,"end":28,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":28}}, + "start":20,"end":32,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":32}}, "key": { "type": "Identifier", - "start":16,"end":20,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":20},"identifierName":"type"}, + "start":20,"end":24,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":24},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start":22,"end":28,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":28}}, + "start":26,"end":32,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":32}}, "extra": { "rawValue": "json", "raw": "\"json\"" @@ -43,14 +43,14 @@ }, { "type": "ExpressionStatement", - "start":29,"end":32,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "start":35,"end":38,"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}}, + "start":35,"end":38,"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}}, + "start":36,"end":37,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}}, "extra": { "rawValue": 0, "raw": "0" diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/input.js new file mode 100644 index 000000000000..7b43091236b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-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/import-assertions/valid-syntax-with-attributes/output.json similarity index 67% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/output.json index dd150dd9dbb1..df6cbd6bcebc 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/output.json @@ -1,15 +1,15 @@ { "type": "File", - "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, "program": { "type": "Program", - "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, "specifiers": [ { "type": "ImportDefaultSpecifier", @@ -30,18 +30,18 @@ }, "value": "foo.json" }, - "attributes": [ + "assertions": [ { "type": "ImportAttribute", - "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, + "start":36,"end":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}}, "key": { "type": "Identifier", - "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, + "start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, + "start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}}, "extra": { "rawValue": "json", "raw": "\"json\"" diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/input.js new file mode 100644 index 000000000000..7b43091236b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { 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/import-assertions/valid-syntax-with-invalid-plugin-option/options.json similarity index 56% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/options.json index a8be45900fa0..0e0d45828851 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/options.json @@ -1,12 +1,12 @@ { "plugins": [ [ - "moduleAttributes", + "importAssertions", { "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 + "throws": "The 'importAssertions' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'september-2020'." +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/input.js new file mode 100644 index 000000000000..01425ab2e2e7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { 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/import-assertions/valid-syntax-with-invalid-value/options.json similarity index 63% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/options.json index f21e649b0f1d..1281bc065c37 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/options.json @@ -1,12 +1,12 @@ { "plugins": [ [ - "moduleAttributes", + "importAssertions", { - "version": "may-2020" + "version": "september-2020" } ] ], "sourceType": "module", - "throws": "Only string literals are allowed as module attribute values (1:52)" -} \ No newline at end of file + "throws": "Only string literals are allowed as module attribute values (1:56)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/input.js new file mode 100644 index 000000000000..bdcedd93bb26 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { lazy: "true" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-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/import-assertions/valid-syntax-with-no-type-attribute/output.json similarity index 64% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json index a3bbfd621562..8d5cffa682dc 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json @@ -1,18 +1,15 @@ { "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)" - ], + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, "program": { "type": "Program", - "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, "specifiers": [ { "type": "ImportDefaultSpecifier", @@ -33,18 +30,18 @@ }, "value": "foo.json" }, - "attributes": [ + "assertions": [ { "type": "ImportAttribute", - "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, + "start":36,"end":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}}, "key": { "type": "Identifier", - "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"lazy"}, + "start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"lazy"}, "name": "lazy" }, "value": { "type": "StringLiteral", - "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, + "start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}}, "extra": { "rawValue": "true", "raw": "\"true\"" diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/input.js new file mode 100644 index 000000000000..828d93472261 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { type: "json", hasOwnProperty: "true" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-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/import-assertions/valid-syntax-with-object-method-attribute/output.json similarity index 62% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json index 12af675ae8b5..5fa35065f83f 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json @@ -1,18 +1,15 @@ { "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)" - ], + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, "program": { "type": "Program", - "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, "specifiers": [ { "type": "ImportDefaultSpecifier", @@ -33,18 +30,18 @@ }, "value": "foo.json" }, - "attributes": [ + "assertions": [ { "type": "ImportAttribute", - "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, + "start":36,"end":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}}, "key": { "type": "Identifier", - "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, + "start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, + "start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}}, "extra": { "rawValue": "json", "raw": "\"json\"" @@ -54,15 +51,15 @@ }, { "type": "ImportAttribute", - "start":46,"end":68,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":68}}, + "start":50,"end":72,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":72}}, "key": { "type": "Identifier", - "start":46,"end":60,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":60},"identifierName":"hasOwnProperty"}, + "start":50,"end":64,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":64},"identifierName":"hasOwnProperty"}, "name": "hasOwnProperty" }, "value": { "type": "StringLiteral", - "start":62,"end":68,"loc":{"start":{"line":1,"column":62},"end":{"line":1,"column":68}}, + "start":66,"end":72,"loc":{"start":{"line":1,"column":66},"end":{"line":1,"column":72}}, "extra": { "rawValue": "true", "raw": "\"true\"" diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/input.js new file mode 100644 index 000000000000..7fcebc0d4fe6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { type: "json", type: "html" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-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/import-assertions/valid-syntax-with-repeated-type/output.json similarity index 61% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/output.json index ad53ece6e4dc..5a444810c109 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/output.json @@ -1,18 +1,15 @@ { "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)" - ], + "start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}}, "program": { "type": "Program", - "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}}, + "start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":59}}, + "start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}}, "specifiers": [ { "type": "ImportDefaultSpecifier", @@ -33,18 +30,18 @@ }, "value": "foo.json" }, - "attributes": [ + "assertions": [ { "type": "ImportAttribute", - "start":32,"end":44,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":44}}, + "start":36,"end":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}}, "key": { "type": "Identifier", - "start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36},"identifierName":"type"}, + "start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start":38,"end":44,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":44}}, + "start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}}, "extra": { "rawValue": "json", "raw": "\"json\"" @@ -54,15 +51,15 @@ }, { "type": "ImportAttribute", - "start":46,"end":58,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":58}}, + "start":50,"end":62,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":62}}, "key": { "type": "Identifier", - "start":46,"end":50,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":50},"identifierName":"type"}, + "start":50,"end":54,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":54},"identifierName":"type"}, "name": "type" }, "value": { "type": "StringLiteral", - "start":52,"end":58,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":58}}, + "start":56,"end":62,"loc":{"start":{"line":1,"column":56},"end":{"line":1,"column":62}}, "extra": { "rawValue": "html", "raw": "\"html\"" diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/input.js rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-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/import-assertions/valid-syntax-without-attributes/output.json similarity index 97% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/output.json index 9a295afe63f1..5a271ca76c70 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/output.json @@ -30,7 +30,7 @@ }, "value": "foo.json" }, - "attributes": [] + "assertions": [] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/input.js new file mode 100644 index 000000000000..7b43091236b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/options.json similarity index 66% rename from packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/options.json index 8645af2af8c7..12d041cba6b3 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/options.json @@ -1,4 +1,4 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", + "throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:27)", "plugins": [] -} \ No newline at end of file +} 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 deleted file mode 100644 index baa60fc43c5f..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/dynamic-import-with-valid-syntax/input.js +++ /dev/null @@ -1 +0,0 @@ -import("foo.json", { with: { type: "json" } }) 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 deleted file mode 100644 index bfa86f1acde6..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/input.js +++ /dev/null @@ -1,2 +0,0 @@ -import "x" -with ({}); 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 deleted file mode 100644 index cccb9e1b329d..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/import-with-statement/output.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "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 deleted file mode 100644 index 7f6df49300e8..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/incorrect-arity/input.js +++ /dev/null @@ -1,2 +0,0 @@ -import(); -import("./foo.json", { with: { type: "json"} }, "unsupported"); 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 deleted file mode 100644 index 4fb4f13bf6f9..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/input.js +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-with-attributes-multiple-lines/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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 deleted file mode 100644 index af54005dade7..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/input.js +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 49737184d7fd..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/invalid-syntax-without-attributes-identifier/options.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "throws": "Unexpected token (1:15)", - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js deleted file mode 100644 index 0be461789922..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/input.js +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/trailing-comma/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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 deleted file mode 100644 index ec62816617cd..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/input.js +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes-and-value/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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 deleted file mode 100644 index fb31508a0860..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/input.js +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-attributes/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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 deleted file mode 100644 index fb31508a0860..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-plugin-option/input.js +++ /dev/null @@ -1 +0,0 @@ -import foo from "foo.json" with type: "json"; 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 deleted file mode 100644 index cfb5372f6b12..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-invalid-value/input.js +++ /dev/null @@ -1 +0,0 @@ -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-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js deleted file mode 100644 index c2ae7496dcb7..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/input.js +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-no-type-attribute/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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 deleted file mode 100644 index d027b991db49..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/input.js +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-object-method-attribute/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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 deleted file mode 100644 index 856373edcf34..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/input.js +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-with-repeated-type/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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 deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/valid-syntax-without-attributes/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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 deleted file mode 100644 index fb31508a0860..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/without-plugin/input.js +++ /dev/null @@ -1 +0,0 @@ -import foo from "foo.json" with type: "json"; diff --git a/packages/babel-parser/typings/babel-parser.d.ts b/packages/babel-parser/typings/babel-parser.d.ts index ed63d17fc9f3..45a2fd149f16 100644 --- a/packages/babel-parser/typings/babel-parser.d.ts +++ b/packages/babel-parser/typings/babel-parser.d.ts @@ -115,7 +115,7 @@ export type ParserPlugin = 'importMeta' | 'jsx' | 'logicalAssignment' | - 'moduleAttributes' | + 'importAssertions' | 'moduleStringNames' | 'nullishCoalescingOperator' | 'numericSeparator' | diff --git a/packages/babel-plugin-syntax-module-attributes/.npmignore b/packages/babel-plugin-syntax-import-assertions/.npmignore similarity index 100% rename from packages/babel-plugin-syntax-module-attributes/.npmignore rename to packages/babel-plugin-syntax-import-assertions/.npmignore diff --git a/packages/babel-plugin-syntax-import-assertions/README.md b/packages/babel-plugin-syntax-import-assertions/README.md new file mode 100644 index 000000000000..f42e4a7f90f1 --- /dev/null +++ b/packages/babel-plugin-syntax-import-assertions/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-syntax-import-assertions + +> Allow parsing of the module assertion attributes in the import statements + +See our website [@babel/plugin-syntax-import-assertions](https://babeljs.io/docs/en/next/babel-plugin-syntax-import-assertions.html) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-syntax-import-assertions +``` + +or using yarn: + +```sh +yarn add @babel/plugin-syntax-import-assertions --dev +``` diff --git a/packages/babel-plugin-syntax-module-attributes/package.json b/packages/babel-plugin-syntax-import-assertions/package.json similarity index 68% rename from packages/babel-plugin-syntax-module-attributes/package.json rename to packages/babel-plugin-syntax-import-assertions/package.json index 1a384738ba92..f09333e34cf1 100644 --- a/packages/babel-plugin-syntax-module-attributes/package.json +++ b/packages/babel-plugin-syntax-import-assertions/package.json @@ -1,11 +1,11 @@ { - "name": "@babel/plugin-syntax-module-attributes", + "name": "@babel/plugin-syntax-import-assertions", "version": "7.10.4", - "description": "Allow parsing of the module attributes in the import statement", + "description": "Allow parsing of the module assertion attributes in the import statement", "repository": { "type": "git", "url": "https://github.com/babel/babel.git", - "directory": "packages/babel-plugin-syntax-module-attributes" + "directory": "packages/babel-plugin-syntax-import-assertions" }, "license": "MIT", "publishConfig": { diff --git a/packages/babel-plugin-syntax-module-attributes/src/index.js b/packages/babel-plugin-syntax-import-assertions/src/index.js similarity index 51% rename from packages/babel-plugin-syntax-module-attributes/src/index.js rename to packages/babel-plugin-syntax-import-assertions/src/index.js index b4709051492d..4796e1445d4e 100644 --- a/packages/babel-plugin-syntax-module-attributes/src/index.js +++ b/packages/babel-plugin-syntax-import-assertions/src/index.js @@ -3,19 +3,19 @@ import { declare } from "@babel/helper-plugin-utils"; export default declare((api, { version }) => { api.assertVersion(7); - if (typeof version !== "string" || version !== "may-2020") { + if (typeof version !== "string" || version !== "september-2020") { throw new Error( - "The 'moduleAttributes' plugin requires a 'version' option," + + "The 'importAssertions' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + - " only supported value is 'may-2020'.", + " only supported value is 'september-2020'.", ); } return { - name: "syntax-module-attributes", + name: "syntax-import-assertions", manipulateOptions(opts, parserOpts) { - parserOpts.plugins.push(["moduleAttributes", { version }]); + parserOpts.plugins.push(["importAssertions", { version }]); }, }; }); diff --git a/packages/babel-plugin-syntax-module-attributes/README.md b/packages/babel-plugin-syntax-module-attributes/README.md deleted file mode 100644 index 3ae0f5a00927..000000000000 --- a/packages/babel-plugin-syntax-module-attributes/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# @babel/plugin-syntax-module-attributes - -> Allow parsing of the module attributes in the import statements - -See our website [@babel/plugin-syntax-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-module-attributes.html) for more information. - -## Install - -Using npm: - -```sh -npm install --save-dev @babel/plugin-syntax-module-attributes -``` - -or using yarn: - -```sh -yarn add @babel/plugin-syntax-module-attributes --dev -``` diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index f20a5920586c..1f6605aea359 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -41,9 +41,8 @@ "@babel/plugin-syntax-flow": "workspace:^7.10.4", "@babel/plugin-syntax-function-bind": "workspace:^7.10.4", "@babel/plugin-syntax-function-sent": "workspace:^7.10.4", - "@babel/plugin-syntax-import-meta": "^7.10.4", + "@babel/plugin-syntax-import-assertions": "workspace:^7.10.4", "@babel/plugin-syntax-jsx": "workspace:^7.10.4", - "@babel/plugin-syntax-module-attributes": "workspace:^7.10.4", "@babel/plugin-syntax-object-rest-spread": "^7.8.0", "@babel/plugin-syntax-optional-catch-binding": "^7.8.0", "@babel/plugin-syntax-pipeline-operator": "workspace:^7.10.4", diff --git a/packages/babel-standalone/scripts/pluginConfig.json b/packages/babel-standalone/scripts/pluginConfig.json index 2b2c3f3a4219..94bc08dd0a20 100644 --- a/packages/babel-standalone/scripts/pluginConfig.json +++ b/packages/babel-standalone/scripts/pluginConfig.json @@ -11,7 +11,7 @@ "syntax-function-sent", "syntax-import-meta", "syntax-jsx", - "syntax-module-attributes", + "syntax-import-assertions", "syntax-object-rest-spread", "syntax-optional-catch-binding", "syntax-pipeline-operator", diff --git a/packages/babel-standalone/src/generated/plugins.js b/packages/babel-standalone/src/generated/plugins.js index c54939ad3450..d49c10aa68bb 100644 --- a/packages/babel-standalone/src/generated/plugins.js +++ b/packages/babel-standalone/src/generated/plugins.js @@ -14,7 +14,7 @@ import syntaxFunctionBind from "@babel/plugin-syntax-function-bind"; import syntaxFunctionSent from "@babel/plugin-syntax-function-sent"; import syntaxImportMeta from "@babel/plugin-syntax-import-meta"; import syntaxJsx from "@babel/plugin-syntax-jsx"; -import syntaxModuleAttributes from "@babel/plugin-syntax-module-attributes"; +import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions"; import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread"; import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding"; import syntaxPipelineOperator from "@babel/plugin-syntax-pipeline-operator"; @@ -107,7 +107,7 @@ export { syntaxFunctionSent, syntaxImportMeta, syntaxJsx, - syntaxModuleAttributes, + syntaxImportAssertions, syntaxObjectRestSpread, syntaxOptionalCatchBinding, syntaxPipelineOperator, @@ -201,7 +201,7 @@ export const all = { "syntax-function-sent": syntaxFunctionSent, "syntax-import-meta": syntaxImportMeta, "syntax-jsx": syntaxJsx, - "syntax-module-attributes": syntaxModuleAttributes, + "syntax-import-assertions": syntaxImportAssertions, "syntax-object-rest-spread": syntaxObjectRestSpread, "syntax-optional-catch-binding": syntaxOptionalCatchBinding, "syntax-pipeline-operator": syntaxPipelineOperator, diff --git a/packages/babel-standalone/src/preset-stage-0.js b/packages/babel-standalone/src/preset-stage-0.js index eaa28036907c..54643f575cdf 100644 --- a/packages/babel-standalone/src/preset-stage-0.js +++ b/packages/babel-standalone/src/preset-stage-0.js @@ -9,7 +9,7 @@ export default (_: any, opts: Object = {}) => { decoratorsLegacy = false, decoratorsBeforeExport, pipelineProposal = "minimal", - moduleAttributesVersion = "may-2020", + importAssertionsVersion = "september-2020", } = opts; return { @@ -22,7 +22,7 @@ export default (_: any, opts: Object = {}) => { decoratorsLegacy, decoratorsBeforeExport, pipelineProposal, - moduleAttributesVersion, + importAssertionsVersion, }, ], ], diff --git a/packages/babel-standalone/src/preset-stage-1.js b/packages/babel-standalone/src/preset-stage-1.js index 0e0cd18c138c..a516ee17e1d9 100644 --- a/packages/babel-standalone/src/preset-stage-1.js +++ b/packages/babel-standalone/src/preset-stage-1.js @@ -10,7 +10,7 @@ export default (_: any, opts: Object = {}) => { decoratorsBeforeExport, pipelineProposal = "minimal", recordAndTupleSyntax: recordAndTupleSyntax = "hash", - moduleAttributesVersion = "may-2020", + importAssertionsVersion = "september-2020", } = opts; return { @@ -23,8 +23,8 @@ export default (_: any, opts: Object = {}) => { plugins: [ babelPlugins.syntaxDecimal, [ - babelPlugins.syntaxModuleAttributes, - { version: moduleAttributesVersion }, + babelPlugins.syntaxImportAssertions, + { version: importAssertionsVersion }, ], [babelPlugins.syntaxRecordAndTuple, { syntaxType: recordAndTupleSyntax }], babelPlugins.proposalExportDefaultFrom, diff --git a/packages/babel-standalone/test/babel.js b/packages/babel-standalone/test/babel.js index c6a450328491..b17e9c50cea3 100644 --- a/packages/babel-standalone/test/babel.js +++ b/packages/babel-standalone/test/babel.js @@ -203,7 +203,7 @@ Babel.transform("/a*/u", { presets: ["es2015"] }), ).not.toThrow(); }); - it("#11628 - supports stage-0 passing moduleAttributesVersion to stage-1", () => { + it("#11628 - supports stage-0 passing importAssertionsVersion to stage-1", () => { expect(() => Babel.transform("const getMessage = () => 'Hello World'", { presets: [["stage-0", { decoratorsBeforeExport: false }]], diff --git a/packages/babel-types/src/definitions/experimental.js b/packages/babel-types/src/definitions/experimental.js index 984e1897da3d..b22d70e6091e 100644 --- a/packages/babel-types/src/definitions/experimental.js +++ b/packages/babel-types/src/definitions/experimental.js @@ -162,7 +162,7 @@ defineType("ImportAttribute", { visitor: ["key", "value"], fields: { key: { - validate: assertNodeType("Identifier"), + validate: assertNodeType("Identifier", "StringLiteral"), }, value: { validate: assertNodeType("StringLiteral"), diff --git a/yarn.lock b/yarn.lock index d0a30fa5f14d..43a439d618b3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1541,16 +1541,16 @@ __metadata: languageName: unknown linkType: soft -"@babel/plugin-syntax-import-meta@npm:^7.10.4": - version: 7.10.4 - resolution: "@babel/plugin-syntax-import-meta@npm:7.10.4" +"@babel/plugin-syntax-import-assertions@workspace:^7.10.4, @babel/plugin-syntax-import-assertions@workspace:packages/babel-plugin-syntax-import-assertions": + version: 0.0.0-use.local + resolution: "@babel/plugin-syntax-import-assertions@workspace:packages/babel-plugin-syntax-import-assertions" dependencies: - "@babel/helper-plugin-utils": ^7.10.4 + "@babel/core": "workspace:^7.10.4" + "@babel/helper-plugin-utils": "workspace:^7.10.4" peerDependencies: "@babel/core": ^7.0.0-0 - checksum: 685ee8f0b5b675952e02e1cabcde4d92638918a66ed515b2663e2e0b2246210a0768325423d5642f8687653a449357826675ccfcb712676be260a0ae13313828 - languageName: node - linkType: hard + languageName: unknown + linkType: soft "@babel/plugin-syntax-json-strings@npm:^7.8.0": version: 7.8.3 @@ -1585,17 +1585,6 @@ __metadata: languageName: node linkType: hard -"@babel/plugin-syntax-module-attributes@workspace:^7.10.4, @babel/plugin-syntax-module-attributes@workspace:packages/babel-plugin-syntax-module-attributes": - version: 0.0.0-use.local - resolution: "@babel/plugin-syntax-module-attributes@workspace:packages/babel-plugin-syntax-module-attributes" - dependencies: - "@babel/core": "workspace:^7.10.4" - "@babel/helper-plugin-utils": "workspace:^7.10.4" - peerDependencies: - "@babel/core": ^7.0.0-0 - languageName: unknown - linkType: soft - "@babel/plugin-syntax-module-string-names@workspace:packages/babel-plugin-syntax-module-string-names": version: 0.0.0-use.local resolution: "@babel/plugin-syntax-module-string-names@workspace:packages/babel-plugin-syntax-module-string-names" @@ -3194,9 +3183,8 @@ __metadata: "@babel/plugin-syntax-flow": "workspace:^7.10.4" "@babel/plugin-syntax-function-bind": "workspace:^7.10.4" "@babel/plugin-syntax-function-sent": "workspace:^7.10.4" - "@babel/plugin-syntax-import-meta": ^7.10.4 + "@babel/plugin-syntax-import-assertions": "workspace:^7.10.4" "@babel/plugin-syntax-jsx": "workspace:^7.10.4" - "@babel/plugin-syntax-module-attributes": "workspace:^7.10.4" "@babel/plugin-syntax-object-rest-spread": ^7.8.0 "@babel/plugin-syntax-optional-catch-binding": ^7.8.0 "@babel/plugin-syntax-pipeline-operator": "workspace:^7.10.4" From 93b5831db6a91de82287fb01d9ae3bc7bc7c2c14 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Sun, 4 Oct 2020 13:31:25 +0100 Subject: [PATCH 02/10] Update packages/babel-parser/src/parser/statement.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Huáng Jùnliàng --- packages/babel-parser/src/parser/statement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 3f1f06415ced..71d88fe7ae1f 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2159,7 +2159,7 @@ export default class StatementParser extends ExpressionParser { if (this.match(tt.string)) { assertionKeyNode = this.parseLiteral(this.state.value, "StringLiteral"); } else { - assertionKeyNode = this.parseIdentifier(); + assertionKeyNode = this.parseIdentifier(true); } this.next(); node.key = assertionKeyNode; From b08145760cb61f67f9d7768f96592ae65ccecaf3 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Mon, 31 Aug 2020 12:34:32 +0100 Subject: [PATCH 03/10] Rename module-attributes to import-assertions --- packages/babel-parser/src/parser/statement.js | 25 ++++ .../input.js | 0 .../options.json | 0 .../_no-plugin/import-assertions/input.js | 1 + .../options.json | 0 .../import-assertions/string-literal/input.js | 1 + .../string-literal/options.json | 11 ++ .../string-literal/output.json | 60 ++++++++ .../trailing-comma-dynamic/input.js | 2 + .../trailing-comma-dynamic/options.json | 11 ++ .../trailing-comma-dynamic/output.json | 110 +++++++++++++++ .../import-assertions/trailing-comma/input.js | 3 +- .../trailing-comma/output.json | 130 ++++++------------ .../output.json | 3 + .../output.json | 3 + .../output.json | 3 + .../.npmignore | 3 - 17 files changed, 271 insertions(+), 95 deletions(-) rename packages/babel-parser/test/fixtures/experimental/_no-plugin/{module-attributes-dynamic => import-assertions-dynamic}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/_no-plugin/{module-attributes-dynamic => import-assertions-dynamic}/options.json (100%) create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/input.js rename packages/babel-parser/test/fixtures/experimental/_no-plugin/{module-attributes => import-assertions}/options.json (100%) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/output.json delete mode 100644 packages/babel-plugin-syntax-import-assertions/.npmignore diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 71d88fe7ae1f..4de5415df52d 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2150,8 +2150,13 @@ export default class StatementParser extends ExpressionParser { this.expectPlugin("importAssertions"); const attrs = []; + const attrNames = new Set(); do { + if (this.match(tt.braceR)) { + break; + } + const node = this.startNode(); // parse AssertionKey : IdentifierName, StringLiteral @@ -2164,6 +2169,26 @@ export default class StatementParser extends ExpressionParser { this.next(); node.key = assertionKeyNode; + // for now we are only allowing `type` as the only allowed module attribute + if (node.key.name !== "type") { + this.raise( + node.key.start, + Errors.ModuleAttributeDifferentFromType, + node.key.name, + ); + } + // check if we already have an entry for an attribute + // if a duplicate entry is found, throw an error + // for now this logic will come into play only when someone declares `type` twice + if (attrNames.has(node.key.name)) { + this.raise( + node.key.start, + Errors.ModuleAttributesWithDuplicateKeys, + node.key.name, + ); + } + attrNames.add(node.key.name); + if (!this.match(tt.string)) { throw this.unexpected( this.state.start, diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/input.js rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes-dynamic/options.json rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/input.js new file mode 100644 index 000000000000..7b43091236b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { 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/import-assertions/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json 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 new file mode 100644 index 000000000000..647ce9367930 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/input.js @@ -0,0 +1 @@ +import foo from "foo.json" assert { for: "for" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-2020" + } + ] + ], + "sourceType": "module" +} 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 new file mode 100644 index 000000000000..f300725af587 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json @@ -0,0 +1,60 @@ +{ + "type": "File", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:36)" + ], + "program": { + "type": "Program", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, + "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" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start":36,"end":46,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":46}}, + "key": { + "type": "Identifier", + "start":36,"end":39,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":39},"identifierName":"for"}, + "name": "for" + }, + "value": { + "type": "StringLiteral", + "start":41,"end":46,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":46}}, + "extra": { + "rawValue": "for", + "raw": "\"for\"" + }, + "value": "for" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/input.js new file mode 100644 index 000000000000..b448aa280c34 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/input.js @@ -0,0 +1,2 @@ +import("foo.js",); +import("foo.json", { assert: { type: "json" } },); diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/options.json new file mode 100644 index 000000000000..640775892178 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "importAssertions", + { + "version": "september-2020" + } + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/output.json new file mode 100644 index 000000000000..832615ebadd2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/output.json @@ -0,0 +1,110 @@ +{ + "type": "File", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}}, + "program": { + "type": "Program", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}}, + "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":69,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":50}}, + "expression": { + "type": "CallExpression", + "start":19,"end":68,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":49}}, + "callee": { + "type": "Import", + "start":19,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}} + }, + "extra": { + "trailingComma": 66 + }, + "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":66,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":47}}, + "properties": [ + { + "type": "ObjectProperty", + "start":40,"end":64,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":45}}, + "method": false, + "key": { + "type": "Identifier", + "start":40,"end":46,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":27},"identifierName":"assert"}, + "name": "assert" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":48,"end":64,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":50,"end":62,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":50,"end":54,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":56,"end":62,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}}, + "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/trailing-comma/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js index b448aa280c34..df65c37d1598 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,2 +1 @@ -import("foo.js",); -import("foo.json", { assert: { type: "json" } },); +import foo from "foo" assert { foo: "foo", } 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 832615ebadd2..93368b651b07 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,108 +1,58 @@ { "type": "File", - "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}}, + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:31)" + ], "program": { "type": "Program", - "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}}, + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, "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": "ImportDeclaration", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "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" } - ] - } - }, - { - "type": "ExpressionStatement", - "start":19,"end":69,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":50}}, - "expression": { - "type": "CallExpression", - "start":19,"end":68,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":49}}, - "callee": { - "type": "Import", - "start":19,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}} - }, + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":21,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":21}}, "extra": { - "trailingComma": 66 + "rawValue": "foo", + "raw": "\"foo\"" }, - "arguments": [ - { + "value": "foo" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start":31,"end":41,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":41}}, + "key": { + "type": "Identifier", + "start":31,"end":34,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":34},"identifierName":"foo"}, + "name": "foo" + }, + "value": { "type": "StringLiteral", - "start":26,"end":36,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":17}}, + "start":36,"end":41,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":41}}, "extra": { - "rawValue": "foo.json", - "raw": "\"foo.json\"" + "rawValue": "foo", + "raw": "\"foo\"" }, - "value": "foo.json" - }, - { - "type": "ObjectExpression", - "start":38,"end":66,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":47}}, - "properties": [ - { - "type": "ObjectProperty", - "start":40,"end":64,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":45}}, - "method": false, - "key": { - "type": "Identifier", - "start":40,"end":46,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":27},"identifierName":"assert"}, - "name": "assert" - }, - "computed": false, - "shorthand": false, - "value": { - "type": "ObjectExpression", - "start":48,"end":64,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":45}}, - "properties": [ - { - "type": "ObjectProperty", - "start":50,"end":62,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}}, - "method": false, - "key": { - "type": "Identifier", - "start":50,"end":54,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"identifierName":"type"}, - "name": "type" - }, - "computed": false, - "shorthand": false, - "value": { - "type": "StringLiteral", - "start":56,"end":62,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}}, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } - } - ] - } - } - ] + "value": "foo" } - ] - } + } + ] } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json index 8d5cffa682dc..69a02fc5865f 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json @@ -1,6 +1,9 @@ { "type": "File", "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:36)" + ], "program": { "type": "Program", "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json index 5fa35065f83f..4259ecc0ee05 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json @@ -1,6 +1,9 @@ { "type": "File", "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, + "errors": [ + "SyntaxError: The only accepted module attribute is `type` (1:50)" + ], "program": { "type": "Program", "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/output.json index 5a444810c109..5d8f1cee0e88 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/output.json @@ -1,6 +1,9 @@ { "type": "File", "start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}}, + "errors": [ + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:50)" + ], "program": { "type": "Program", "start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}}, diff --git a/packages/babel-plugin-syntax-import-assertions/.npmignore b/packages/babel-plugin-syntax-import-assertions/.npmignore deleted file mode 100644 index 2b1fceba679b..000000000000 --- a/packages/babel-plugin-syntax-import-assertions/.npmignore +++ /dev/null @@ -1,3 +0,0 @@ -*.log -src -test From a093f33bd286db7f4ac0b645d7d7a8c23bb29bad Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Mon, 5 Oct 2020 15:36:56 +0100 Subject: [PATCH 04/10] Update packages/babel-core/src/parser/util/missing-plugin-helper.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- packages/babel-core/src/parser/util/missing-plugin-helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-core/src/parser/util/missing-plugin-helper.js b/packages/babel-core/src/parser/util/missing-plugin-helper.js index 2a8b7ba68dfc..165aeb4b3c26 100644 --- a/packages/babel-core/src/parser/util/missing-plugin-helper.js +++ b/packages/babel-core/src/parser/util/missing-plugin-helper.js @@ -132,7 +132,7 @@ const pluginNameMap = { importAssertions: { syntax: { name: "@babel/plugin-syntax-import-assertions", - url: "TODO", + url: "https://git.io/JUbkv", }, }, moduleStringNames: { From eb8f4184e9f6e6d8256a214729e75e277490c36c 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 05/10] 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 de48216fe962..a5e07661dee0 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 4de5415df52d..079e3ae117f4 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2116,6 +2116,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"); } @@ -2203,6 +2211,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 From 7662634aebbd8f3185c6d6fec92b0c5bc3b605cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 9 Oct 2020 10:15:26 -0400 Subject: [PATCH 06/10] Update packages/babel-generator/src/generators/modules.js --- packages/babel-generator/src/generators/modules.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/babel-generator/src/generators/modules.js b/packages/babel-generator/src/generators/modules.js index 09733a310e3e..a7418b2cff5c 100644 --- a/packages/babel-generator/src/generators/modules.js +++ b/packages/babel-generator/src/generators/modules.js @@ -196,7 +196,6 @@ export function ImportDeclaration(node: Object) { this.space(); this.word("with"); this.space(); - this.space(); this.printList(node.attributes, node); } From ba35624be13be303b09e654288f05405923c03ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 12 Oct 2020 15:36:26 -0400 Subject: [PATCH 07/10] remove `version` of `import-assertions` --- .../types/ImportAssertion/options.json | 5 +--- packages/babel-parser/src/plugin-utils.js | 24 ++++--------------- .../import-assertions-dynamic/options.json | 2 +- .../_no-plugin/import-assertions/options.json | 2 +- .../options.json | 11 --------- .../input.js | 0 .../output.json | 15 +++++++----- .../import-with-statement/options.json | 12 ---------- .../incorrect-arity/options.json | 11 --------- .../options.json | 11 --------- .../options.json | 11 --------- .../options.json | 11 ++++----- .../import-assertions/options.json | 8 +++++++ .../string-literal/options.json | 11 --------- .../trailing-comma-dynamic/options.json | 11 --------- .../trailing-comma/options.json | 11 --------- .../options.json | 11 --------- .../valid-syntax-with-attributes/options.json | 11 --------- .../input.js | 1 - .../options.json | 12 ---------- .../options.json | 7 ++---- .../options.json | 11 --------- .../options.json | 11 --------- .../options.json | 11 --------- .../options.json | 11 --------- 25 files changed, 31 insertions(+), 211 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/options.json rename packages/babel-parser/test/fixtures/experimental/import-assertions/{import-with-statement => import-assert-call-expression}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/import-assertions/{import-with-statement => import-assert-call-expression}/output.json (88%) delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/input.js delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/options.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/options.json diff --git a/packages/babel-generator/test/fixtures/types/ImportAssertion/options.json b/packages/babel-generator/test/fixtures/types/ImportAssertion/options.json index 640775892178..94e3f3742509 100644 --- a/packages/babel-generator/test/fixtures/types/ImportAssertion/options.json +++ b/packages/babel-generator/test/fixtures/types/ImportAssertion/options.json @@ -1,10 +1,7 @@ { "plugins": [ [ - "importAssertions", - { - "version": "september-2020" - } + "importAssertions" ] ], "sourceType": "module" diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index 9c7198d81076..777ee42b20bf 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -87,6 +87,11 @@ export function validatePlugins(plugins: PluginList) { } if (hasPlugin(plugins, "moduleAttributes")) { + if (hasPlugin(plugins, "importAssertions")) { + throw new Error( + "Cannot combine importAssertions and moduleAttributes plugins.", + ); + } const moduleAttributesVerionPluginOption = getPluginOption( plugins, "moduleAttributes", @@ -101,25 +106,6 @@ export function validatePlugins(plugins: PluginList) { } } - if (hasPlugin(plugins, "importAssertions")) { - const importAssertionsVerionPluginOption = getPluginOption( - plugins, - "importAssertions", - "version", - ); - if (importAssertionsVerionPluginOption !== "september-2020") { - throw new Error( - "The 'importAssertions' plugin requires a 'version' option," + - " representing the last proposal update. Currently, the" + - " only supported value is 'september-2020'.", - ); - } - if (hasPlugin(plugins, "moduleAttributes")) { - throw new Error( - "Cannot combine importAssertions and moduleAttributes plugins.", - ); - } - } if ( hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes( diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json index 7dc0fb837dff..eab055ee2fb6 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json @@ -2,4 +2,4 @@ "throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:24)", "sourceType": "module", "plugins": [] -} +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json index b9144a3ad2cc..25a75a1cd87e 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json @@ -2,4 +2,4 @@ "throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:27)", "sourceType": "module", "plugins": [] -} +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/input.js rename to packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/output.json similarity index 88% rename from packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/output.json index f845790255b7..e59fa66d98fa 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/output.json @@ -12,10 +12,13 @@ "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, "specifiers": [], "source": { - "type": "Literal", + "type": "StringLiteral", "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, - "value": "x", - "raw": "\"x\"" + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" }, "assertions": [] }, @@ -36,10 +39,10 @@ "start":19,"end":21,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":10}}, "properties": [] } - ], - "optional": false + ] } } - ] + ], + "directives": [] } } \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/options.json deleted file mode 100644 index 3876aba950da..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/import-with-statement/options.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ], - "estree" - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json index 3e9b35f35878..7f991d8162b7 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json @@ -1,12 +1,9 @@ { - "throws": "Unexpected token (1:17)", "plugins": [ [ - "importAssertions", - { - "version": "september-2020" - } + "importAssertions" ] ], - "sourceType": "module" -} + "sourceType": "module", + "throws": "Unexpected token (1:17)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/options.json new file mode 100644 index 000000000000..94e3f3742509 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "importAssertions" + ] + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma-dynamic/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/input.js deleted file mode 100644 index 7b43091236b9..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/input.js +++ /dev/null @@ -1 +0,0 @@ -import foo from "foo.json" assert { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/options.json deleted file mode 100644 index 0e0d45828851..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-plugin-option/options.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "jan-2020" - } - ] - ], - "sourceType": "module", - "throws": "The 'importAssertions' plugin requires a 'version' option, representing the last proposal update. Currently, the only supported value is 'september-2020'." -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/options.json index 1281bc065c37..a1951fb94624 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/options.json @@ -1,12 +1,9 @@ { "plugins": [ [ - "importAssertions", - { - "version": "september-2020" - } + "importAssertions" ] ], "sourceType": "module", "throws": "Only string literals are allowed as module attribute values (1:56)" -} +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/options.json deleted file mode 100644 index 640775892178..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "importAssertions", - { - "version": "september-2020" - } - ] - ], - "sourceType": "module" -} From 8bebffce7449517240fe8db7d12a216f57f020ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 12 Oct 2020 16:00:30 -0400 Subject: [PATCH 08/10] move import-assertions to stage 3 --- packages/babel-standalone/package.json | 1 + packages/babel-standalone/src/preset-stage-1.js | 5 ----- packages/babel-standalone/src/preset-stage-3.js | 1 + yarn.lock | 12 ++++++++++++ 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index 1f6605aea359..48f400dd05cc 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -42,6 +42,7 @@ "@babel/plugin-syntax-function-bind": "workspace:^7.10.4", "@babel/plugin-syntax-function-sent": "workspace:^7.10.4", "@babel/plugin-syntax-import-assertions": "workspace:^7.10.4", + "@babel/plugin-syntax-import-meta": "^7.10.4", "@babel/plugin-syntax-jsx": "workspace:^7.10.4", "@babel/plugin-syntax-object-rest-spread": "^7.8.0", "@babel/plugin-syntax-optional-catch-binding": "^7.8.0", diff --git a/packages/babel-standalone/src/preset-stage-1.js b/packages/babel-standalone/src/preset-stage-1.js index a516ee17e1d9..d04643b8a690 100644 --- a/packages/babel-standalone/src/preset-stage-1.js +++ b/packages/babel-standalone/src/preset-stage-1.js @@ -10,7 +10,6 @@ export default (_: any, opts: Object = {}) => { decoratorsBeforeExport, pipelineProposal = "minimal", recordAndTupleSyntax: recordAndTupleSyntax = "hash", - importAssertionsVersion = "september-2020", } = opts; return { @@ -22,10 +21,6 @@ export default (_: any, opts: Object = {}) => { ], plugins: [ babelPlugins.syntaxDecimal, - [ - babelPlugins.syntaxImportAssertions, - { version: importAssertionsVersion }, - ], [babelPlugins.syntaxRecordAndTuple, { syntaxType: recordAndTupleSyntax }], babelPlugins.proposalExportDefaultFrom, [babelPlugins.proposalPipelineOperator, { proposal: pipelineProposal }], diff --git a/packages/babel-standalone/src/preset-stage-3.js b/packages/babel-standalone/src/preset-stage-3.js index 2b42d24f4e83..78701c876792 100644 --- a/packages/babel-standalone/src/preset-stage-3.js +++ b/packages/babel-standalone/src/preset-stage-3.js @@ -10,6 +10,7 @@ export default (_: any, opts: Object) => { return { plugins: [ + babelPlugins.syntaxImportAssertions, babelPlugins.syntaxImportMeta, babelPlugins.syntaxTopLevelAwait, babelPlugins.proposalExportNamespaceFrom, diff --git a/yarn.lock b/yarn.lock index 43a439d618b3..2f8e25f3c9a8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1552,6 +1552,17 @@ __metadata: languageName: unknown linkType: soft +"@babel/plugin-syntax-import-meta@npm:^7.10.4": + version: 7.10.4 + resolution: "@babel/plugin-syntax-import-meta@npm:7.10.4" + dependencies: + "@babel/helper-plugin-utils": ^7.10.4 + peerDependencies: + "@babel/core": ^7.0.0-0 + checksum: 685ee8f0b5b675952e02e1cabcde4d92638918a66ed515b2663e2e0b2246210a0768325423d5642f8687653a449357826675ccfcb712676be260a0ae13313828 + languageName: node + linkType: hard + "@babel/plugin-syntax-json-strings@npm:^7.8.0": version: 7.8.3 resolution: "@babel/plugin-syntax-json-strings@npm:7.8.3" @@ -3184,6 +3195,7 @@ __metadata: "@babel/plugin-syntax-function-bind": "workspace:^7.10.4" "@babel/plugin-syntax-function-sent": "workspace:^7.10.4" "@babel/plugin-syntax-import-assertions": "workspace:^7.10.4" + "@babel/plugin-syntax-import-meta": ^7.10.4 "@babel/plugin-syntax-jsx": "workspace:^7.10.4" "@babel/plugin-syntax-object-rest-spread": ^7.8.0 "@babel/plugin-syntax-optional-catch-binding": ^7.8.0 From b0601b1ad590f5b719f93fcb1bbe5a7dbdbd2323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 12 Oct 2020 16:27:24 -0400 Subject: [PATCH 09/10] remove "version" options from syntax-import-assertions --- .../src/index.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/babel-plugin-syntax-import-assertions/src/index.js b/packages/babel-plugin-syntax-import-assertions/src/index.js index 4796e1445d4e..e99359e0372d 100644 --- a/packages/babel-plugin-syntax-import-assertions/src/index.js +++ b/packages/babel-plugin-syntax-import-assertions/src/index.js @@ -1,21 +1,13 @@ import { declare } from "@babel/helper-plugin-utils"; -export default declare((api, { version }) => { +export default declare(api => { api.assertVersion(7); - if (typeof version !== "string" || version !== "september-2020") { - throw new Error( - "The 'importAssertions' plugin requires a 'version' option," + - " representing the last proposal update. Currently, the" + - " only supported value is 'september-2020'.", - ); - } - return { name: "syntax-import-assertions", manipulateOptions(opts, parserOpts) { - parserOpts.plugins.push(["importAssertions", { version }]); + parserOpts.plugins.push(["importAssertions"]); }, }; }); From 39838f0a9fa5713c61bddbd86d42e7fd3cc1ae07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 13 Oct 2020 15:46:43 -0400 Subject: [PATCH 10/10] update test fixtures --- .../import-assertions/trailing-comma/input.js | 2 +- .../trailing-comma/output.json | 23 ++++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) 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 df65c37d1598..7a70cbae8d9b 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 @@ -import foo from "foo" assert { foo: "foo", } +import 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 93368b651b07..30bc21d725d9 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,18 +1,15 @@ { "type": "File", - "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, - "errors": [ - "SyntaxError: The only accepted module attribute is `type` (1:31)" - ], + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, "program": { "type": "Program", - "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ImportDeclaration", - "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, "specifiers": [ { "type": "ImportDefaultSpecifier", @@ -36,20 +33,20 @@ "assertions": [ { "type": "ImportAttribute", - "start":31,"end":41,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":41}}, + "start":31,"end":43,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":43}}, "key": { "type": "Identifier", - "start":31,"end":34,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":34},"identifierName":"foo"}, - "name": "foo" + "start":31,"end":35,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":35},"identifierName":"type"}, + "name": "type" }, "value": { "type": "StringLiteral", - "start":36,"end":41,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":41}}, + "start":37,"end":43,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":43}}, "extra": { - "rawValue": "foo", - "raw": "\"foo\"" + "rawValue": "json", + "raw": "\"json\"" }, - "value": "foo" + "value": "json" } } ]