From af8e0facc1b96bf83e1ac8a641706c10d42a2d2e Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Wed, 14 Oct 2020 20:18:16 +0100 Subject: [PATCH] Parse import-assertions (#12139) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Huáng Jùnliàng Co-authored-by: Nicolò Ribaudo --- .../src/parser/util/missing-plugin-helper.js | 6 +- .../babel-generator/src/generators/modules.js | 14 ++- .../fixtures/types/ImportAssertion/input.js | 1 + .../types/ImportAssertion/options.json | 8 ++ .../fixtures/types/ImportAssertion/output.js | 1 + .../fixtures/types/ImportAttribute/input.js | 1 - .../types/ImportAttribute/options.json | 11 -- .../fixtures/types/ImportAttribute/output.js | 1 - .../{Import2 => ModuleAttributes}/input.js | 2 + .../options.json | 0 .../{Import2 => ModuleAttributes}/output.js | 1 + packages/babel-parser/ast/spec.md | 2 +- .../babel-parser/src/parser/expression.js | 15 ++- packages/babel-parser/src/parser/statement.js | 110 +++++++++++++++--- packages/babel-parser/src/plugin-utils.js | 6 + .../input.js | 0 .../options.json | 2 +- .../_no-plugin/import-assertions/input.js | 1 + .../_no-plugin/import-assertions/options.json | 5 + .../_no-plugin/module-attributes/options.json | 6 +- .../dynamic-import-with-valid-syntax/input.js | 1 + .../output.json | 81 +++++++++++++ .../import-assert-call-expression/input.js | 2 + .../import-assert-call-expression/output.json | 48 ++++++++ .../incorrect-arity/input.js | 2 + .../incorrect-arity/output.json | 107 +++++++++++++++++ .../input.js | 1 + .../output.json | 48 ++++++++ .../input.js | 2 + .../output.json | 47 ++++++++ .../input.js | 1 + .../options.json | 9 ++ .../import-assertions/options.json | 8 ++ .../import-assertions/string-literal/input.js | 1 + .../string-literal/output.json | 60 ++++++++++ .../trailing-comma-dynamic/input.js | 2 + .../trailing-comma-dynamic/output.json | 110 ++++++++++++++++++ .../import-assertions/trailing-comma/input.js | 1 + .../trailing-comma/output.json | 57 +++++++++ .../input.js | 2 + .../output.json | 66 +++++++++++ .../valid-syntax-with-attributes/input.js | 1 + .../valid-syntax-with-attributes/output.json | 57 +++++++++ .../valid-syntax-with-invalid-value/input.js | 1 + .../options.json | 9 ++ .../input.js | 1 + .../output.json | 60 ++++++++++ .../input.js | 1 + .../output.json | 78 +++++++++++++ .../valid-syntax-with-repeated-type/input.js | 1 + .../output.json | 78 +++++++++++++ .../valid-syntax-without-attributes/input.js | 1 + .../output.json | 38 ++++++ .../import-assertions/without-plugin/input.js | 1 + .../without-plugin/options.json | 4 + .../babel-parser/typings/babel-parser.d.ts | 2 +- .../README.md | 19 +++ .../package.json | 6 +- .../src/index.js | 13 +++ .../.npmignore | 3 - .../README.md | 19 --- .../src/index.js | 21 ---- packages/babel-standalone/package.json | 2 +- .../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 | 5 - .../babel-standalone/src/preset-stage-3.js | 1 + packages/babel-standalone/test/babel.js | 2 +- .../src/definitions/experimental.js | 2 +- yarn.lock | 24 ++-- 71 files changed, 1199 insertions(+), 112 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/types/ImportAssertion/input.js create mode 100644 packages/babel-generator/test/fixtures/types/ImportAssertion/options.json 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/options.json delete mode 100644 packages/babel-generator/test/fixtures/types/ImportAttribute/output.js rename packages/babel-generator/test/fixtures/types/{Import2 => ModuleAttributes}/input.js (56%) rename packages/babel-generator/test/fixtures/types/{Import2 => ModuleAttributes}/options.json (100%) rename packages/babel-generator/test/fixtures/types/{Import2 => ModuleAttributes}/output.js (60%) 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 (72%) create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/output.json 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/output.json 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/options.json 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/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/output.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/output.json 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/output.json 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/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/options.json 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/output.json 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/output.json 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/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/options.json 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%) create mode 100644 packages/babel-plugin-syntax-import-assertions/src/index.js delete mode 100644 packages/babel-plugin-syntax-module-attributes/.npmignore delete mode 100644 packages/babel-plugin-syntax-module-attributes/README.md delete mode 100644 packages/babel-plugin-syntax-module-attributes/src/index.js 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 f2b77703a7d7..b1a949746d51 100644 --- a/packages/babel-core/src/parser/util/missing-plugin-helper.js +++ b/packages/babel-core/src/parser/util/missing-plugin-helper.js @@ -139,10 +139,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: "https://git.io/JUbkv", }, }, moduleStringNames: { diff --git a/packages/babel-generator/src/generators/modules.js b/packages/babel-generator/src/generators/modules.js index 0d398f801c53..a7418b2cff5c 100644 --- a/packages/babel-generator/src/generators/modules.js +++ b/packages/babel-generator/src/generators/modules.js @@ -180,7 +180,19 @@ export function ImportDeclaration(node: Object) { this.print(node.source, node); - if (node.attributes?.length) { + if (node.assertions?.length) { + this.space(); + this.word("assert"); + this.space(); + this.token("{"); + this.space(); + this.printList(node.assertions, node); + 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(); 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-generator/test/fixtures/types/ImportAssertion/options.json b/packages/babel-generator/test/fixtures/types/ImportAssertion/options.json new file mode 100644 index 000000000000..94e3f3742509 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAssertion/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "importAssertions" + ] + ], + "sourceType": "module" +} 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/options.json b/packages/babel-generator/test/fixtures/types/ImportAttribute/options.json deleted file mode 100644 index 14244aa04de3..000000000000 --- a/packages/babel-generator/test/fixtures/types/ImportAttribute/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "moduleAttributes", - { - "version": "may-2020" - } - ] - ], - "sourceType": "module" -} 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-generator/test/fixtures/types/Import2/input.js b/packages/babel-generator/test/fixtures/types/ModuleAttributes/input.js similarity index 56% rename from packages/babel-generator/test/fixtures/types/Import2/input.js rename to packages/babel-generator/test/fixtures/types/ModuleAttributes/input.js index f01b3a7ec608..d415f18fe52c 100644 --- a/packages/babel-generator/test/fixtures/types/Import2/input.js +++ b/packages/babel-generator/test/fixtures/types/ModuleAttributes/input.js @@ -1 +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 100% rename from packages/babel-generator/test/fixtures/types/Import2/options.json rename to packages/babel-generator/test/fixtures/types/ModuleAttributes/options.json 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/ast/spec.md b/packages/babel-parser/ast/spec.md index 9ccd9a960d60..85bbbf20cfa6 100644 --- a/packages/babel-parser/ast/spec.md +++ b/packages/babel-parser/ast/spec.md @@ -1303,7 +1303,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 b54b4a8442b8..ad8ad9ccb225 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("moduleAttributes"); + // 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("moduleAttributes") + 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("moduleAttributes")) { + 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 7fbb3d838b96..2156734b74a5 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2144,12 +2144,21 @@ 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; + } + // todo(Babel 8): remove module attributes support + else { + const attributes = this.maybeParseModuleAttributes(); + if (attributes) { + node.attributes = attributes; + } } + this.semicolon(); return this.finishNode(node, "ImportDeclaration"); } @@ -2180,6 +2189,69 @@ export default class StatementParser extends ExpressionParser { node.specifiers.push(this.finishNode(specifier, type)); } + parseAssertEntries() { + this.expectPlugin("importAssertions"); + + const attrs = []; + const attrNames = new Set(); + + do { + if (this.match(tt.braceR)) { + break; + } + + const node = this.startNode(); + + // parse AssertionKey : IdentifierName, StringLiteral + let assertionKeyNode; + if (this.match(tt.string)) { + assertionKeyNode = this.parseLiteral(this.state.value, "StringLiteral"); + } else { + assertionKeyNode = this.parseIdentifier(true); + } + 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, + Errors.ModuleAttributeInvalidValue, + ); + } + node.value = this.parseLiteral(this.state.value, "StringLiteral"); + this.finishNode(node, "ImportAttribute"); + attrs.push(node); + } while (this.eat(tt.comma)); + + 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"); @@ -2191,13 +2263,9 @@ export default class StatementParser extends ExpressionParser { 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, @@ -2206,9 +2274,6 @@ export default class StatementParser extends ExpressionParser { ); } - // 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, @@ -2218,7 +2283,6 @@ export default class StatementParser extends ExpressionParser { } 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, @@ -2233,6 +2297,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..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", @@ -100,6 +105,7 @@ export function validatePlugins(plugins: PluginList) { ); } } + if ( hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes( 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 72% 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 index 6b7622b8eeb2..eab055ee2fb6 100644 --- 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 @@ -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/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/import-assertions/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json new file mode 100644 index 000000000000..25a75a1cd87e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json @@ -0,0 +1,5 @@ +{ + "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/_no-plugin/module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/module-attributes/options.json index 3f9b696c8de9..299e44f15cf8 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,3 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", - "sourceType": "module", - "plugins": [] -} \ No newline at end of file + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)" +} 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-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/output.json new file mode 100644 index 000000000000..5762113a14b2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax/output.json @@ -0,0 +1,81 @@ +{ + "type": "File", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, + "program": { + "type": "Program", + "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":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}}, + "expression": { + "type": "CallExpression", + "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}} + }, + "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":47,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":47}}, + "properties": [ + { + "type": "ObjectProperty", + "start":21,"end":45,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":45}}, + "method": false, + "key": { + "type": "Identifier", + "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":29,"end":45,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":31,"end":43,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "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":37,"end":43,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"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/import-assert-call-expression/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/input.js new file mode 100644 index 000000000000..5b6c5fa3373a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/input.js @@ -0,0 +1,2 @@ +import "x" +assert ({}); diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/output.json new file mode 100644 index 000000000000..e59fa66d98fa --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/import-assert-call-expression/output.json @@ -0,0 +1,48 @@ +{ + "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": "StringLiteral", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "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": [] + } + ] + } + } + ], + "directives": [] + } +} \ 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/import-assertions/incorrect-arity/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/output.json new file mode 100644 index 000000000000..de469d3cab1e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/output.json @@ -0,0 +1,107 @@ +{ + "type": "File", + "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":75,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":65}}, + "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":75,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":65}}, + "expression": { + "type": "CallExpression", + "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}} + }, + "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":58,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":48}}, + "properties": [ + { + "type": "ObjectProperty", + "start":33,"end":56,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":46}}, + "method": false, + "key": { + "type": "Identifier", + "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":41,"end":56,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":46}}, + "properties": [ + { + "type": "ObjectProperty", + "start":43,"end":55,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":45}}, + "method": false, + "key": { + "type": "Identifier", + "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":49,"end":55,"loc":{"start":{"line":2,"column":39},"end":{"line":2,"column":45}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + }, + { + "type": "StringLiteral", + "start":60,"end":73,"loc":{"start":{"line":2,"column":50},"end":{"line":2,"column":63}}, + "extra": { + "rawValue": "unsupported", + "raw": "\"unsupported\"" + }, + "value": "unsupported" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/input.js new file mode 100644 index 000000000000..0a9420c378f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/input.js @@ -0,0 +1 @@ +import("./foo.json", ...[]); diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-spread-element-import-call/output.json new file mode 100644 index 000000000000..f016f97336e9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/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/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/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/output.json new file mode 100644 index 000000000000..772136f80389 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/output.json @@ -0,0 +1,47 @@ +{ + "type": "File", + "start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}}, + "program": { + "type": "Program", + "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":34,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}}, + "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" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start":20,"end":32,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}}, + "key": { + "type": "Identifier", + "start":20,"end":24,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":26,"end":32,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":14}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-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..7f991d8162b7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-without-attributes-identifier/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + [ + "importAssertions" + ] + ], + "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/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/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/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 new file mode 100644 index 000000000000..7a70cbae8d9b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js @@ -0,0 +1 @@ +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 new file mode 100644 index 000000000000..30bc21d725d9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/output.json @@ -0,0 +1,57 @@ +{ + "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": "ImportDeclaration", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "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":21,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":21}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start":31,"end":43,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":43}}, + "key": { + "type": "Identifier", + "start":31,"end":35,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":35},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":37,"end":43,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"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/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/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/output.json new file mode 100644 index 000000000000..e83db8d7602a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-and-value/output.json @@ -0,0 +1,66 @@ +{ + "type": "File", + "start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}}, + "program": { + "type": "Program", + "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":34,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":34}}, + "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" + }, + "assertions": [ + { + "type": "ImportAttribute", + "start":20,"end":32,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":32}}, + "key": { + "type": "Identifier", + "start":20,"end":24,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":24},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":26,"end":32,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":32}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + }, + { + "type": "ExpressionStatement", + "start":35,"end":38,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "expression": { + "type": "ArrayExpression", + "start":35,"end":38,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "elements": [ + { + "type": "NumericLiteral", + "start":36,"end":37,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-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/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/output.json new file mode 100644 index 000000000000..df6cbd6bcebc --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes/output.json @@ -0,0 +1,57 @@ +{ + "type": "File", + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, + "program": { + "type": "Program", + "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":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, + "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":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}}, + "key": { + "type": "Identifier", + "start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-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/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 new file mode 100644 index 000000000000..a1951fb94624 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-invalid-value/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + [ + "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/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/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json new file mode 100644 index 000000000000..69a02fc5865f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json @@ -0,0 +1,60 @@ +{ + "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}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, + "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":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}}, + "key": { + "type": "Identifier", + "start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"lazy"}, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-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/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json new file mode 100644 index 000000000000..4259ecc0ee05 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json @@ -0,0 +1,78 @@ +{ + "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}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, + "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":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}}, + "key": { + "type": "Identifier", + "start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":50,"end":72,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":72}}, + "key": { + "type": "Identifier", + "start":50,"end":64,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":64},"identifierName":"hasOwnProperty"}, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start":66,"end":72,"loc":{"start":{"line":1,"column":66},"end":{"line":1,"column":72}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-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/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/output.json new file mode 100644 index 000000000000..5d8f1cee0e88 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-repeated-type/output.json @@ -0,0 +1,78 @@ +{ + "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}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}}, + "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":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}}, + "key": { + "type": "Identifier", + "start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":50,"end":62,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":62}}, + "key": { + "type": "Identifier", + "start":50,"end":54,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":54},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":56,"end":62,"loc":{"start":{"line":1,"column":56},"end":{"line":1,"column":62}}, + "extra": { + "rawValue": "html", + "raw": "\"html\"" + }, + "value": "html" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/input.js new file mode 100644 index 000000000000..92901ed56b38 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-without-attributes/output.json new file mode 100644 index 000000000000..5a271ca76c70 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/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" + }, + "assertions": [] + } + ], + "directives": [] + } +} \ No newline at end of file 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/import-assertions/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/options.json new file mode 100644 index 000000000000..12d041cba6b3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/options.json @@ -0,0 +1,4 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:27)", + "plugins": [] +} diff --git a/packages/babel-parser/typings/babel-parser.d.ts b/packages/babel-parser/typings/babel-parser.d.ts index 1d70960bed87..7a9d0ebab10e 100644 --- a/packages/babel-parser/typings/babel-parser.d.ts +++ b/packages/babel-parser/typings/babel-parser.d.ts @@ -116,7 +116,7 @@ export type ParserPlugin = 'importMeta' | 'jsx' | 'logicalAssignment' | - 'moduleAttributes' | + 'importAssertions' | 'moduleStringNames' | 'nullishCoalescingOperator' | 'numericSeparator' | 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-import-assertions/src/index.js b/packages/babel-plugin-syntax-import-assertions/src/index.js new file mode 100644 index 000000000000..e99359e0372d --- /dev/null +++ b/packages/babel-plugin-syntax-import-assertions/src/index.js @@ -0,0 +1,13 @@ +import { declare } from "@babel/helper-plugin-utils"; + +export default declare(api => { + api.assertVersion(7); + + return { + name: "syntax-import-assertions", + + manipulateOptions(opts, parserOpts) { + parserOpts.plugins.push(["importAssertions"]); + }, + }; +}); diff --git a/packages/babel-plugin-syntax-module-attributes/.npmignore b/packages/babel-plugin-syntax-module-attributes/.npmignore deleted file mode 100644 index 2b1fceba679b..000000000000 --- a/packages/babel-plugin-syntax-module-attributes/.npmignore +++ /dev/null @@ -1,3 +0,0 @@ -*.log -src -test 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-plugin-syntax-module-attributes/src/index.js b/packages/babel-plugin-syntax-module-attributes/src/index.js deleted file mode 100644 index b4709051492d..000000000000 --- a/packages/babel-plugin-syntax-module-attributes/src/index.js +++ /dev/null @@ -1,21 +0,0 @@ -import { declare } from "@babel/helper-plugin-utils"; - -export default declare((api, { version }) => { - api.assertVersion(7); - - if (typeof version !== "string" || version !== "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'.", - ); - } - - return { - name: "syntax-module-attributes", - - manipulateOptions(opts, parserOpts) { - parserOpts.plugins.push(["moduleAttributes", { version }]); - }, - }; -}); diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index f6a653d78b84..d6abb26eb49e 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -43,9 +43,9 @@ "@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-assertions": "workspace:^7.10.4", "@babel/plugin-syntax-import-meta": "^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 326d74c0c335..11b25e193cfd 100644 --- a/packages/babel-standalone/scripts/pluginConfig.json +++ b/packages/babel-standalone/scripts/pluginConfig.json @@ -12,7 +12,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 af19753aee7e..220c1f5dc02c 100644 --- a/packages/babel-standalone/src/generated/plugins.js +++ b/packages/babel-standalone/src/generated/plugins.js @@ -15,7 +15,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"; @@ -110,7 +110,7 @@ export { syntaxFunctionSent, syntaxImportMeta, syntaxJsx, - syntaxModuleAttributes, + syntaxImportAssertions, syntaxObjectRestSpread, syntaxOptionalCatchBinding, syntaxPipelineOperator, @@ -206,7 +206,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 4b40aaf96f79..eaf640f6cc6d 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", - moduleAttributesVersion = "may-2020", } = opts; return { @@ -22,10 +21,6 @@ export default (_: any, opts: Object = {}) => { ], plugins: [ babelPlugins.syntaxDecimal, - [ - babelPlugins.syntaxModuleAttributes, - { version: moduleAttributesVersion }, - ], [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/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 e11581704963..b7f3360f98c8 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 8ad497330dfa..81c31d7db9b2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1564,6 +1564,17 @@ __metadata: languageName: unknown linkType: soft +"@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/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-import-meta@npm:^7.10.4": version: 7.10.4 resolution: "@babel/plugin-syntax-import-meta@npm:7.10.4" @@ -1608,17 +1619,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" @@ -3219,9 +3219,9 @@ __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-assertions": "workspace:^7.10.4" "@babel/plugin-syntax-import-meta": ^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"