diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index 40dd668043..e52e48a1fa 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -204,15 +204,15 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) { this.print(node.source, node); this.printAssertions(node); - // todo(Babel 8): remove this if branch - // `module-attributes` support is discontinued, use `import-assertions` instead. - // @ts-expect-error - if (node.attributes?.length) { - this.space(); - this.word("with"); - this.space(); + if (!process.env.BABEL_8_BREAKING) { // @ts-expect-error - this.printList(node.attributes, node); + if (node.attributes?.length) { + this.space(); + this.word("with"); + this.space(); + // @ts-expect-error + this.printList(node.attributes, node); + } } this.semicolon(); diff --git a/packages/babel-generator/test/fixtures/types/ModuleAttributes/options.json b/packages/babel-generator/test/fixtures/types/ModuleAttributes/options.json index 14244aa04d..82129adc81 100644 --- a/packages/babel-generator/test/fixtures/types/ModuleAttributes/options.json +++ b/packages/babel-generator/test/fixtures/types/ModuleAttributes/options.json @@ -7,5 +7,6 @@ } ] ], - "sourceType": "module" + "sourceType": "module", + "BABEL_8_BREAKING": false } diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 8dc5ca0107..43616202ee 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -857,10 +857,12 @@ export default class ExpressionParser extends LValParser { ): N.Expression { if (node.callee.type === "Import") { if (node.arguments.length === 2) { - // todo(Babel 8): remove the if condition, - // moduleAttributes is renamed to importAssertions - if (!this.hasPlugin("moduleAttributes")) { + if (process.env.BABEL_8_BREAKING) { this.expectPlugin("importAssertions"); + } else { + if (!this.hasPlugin("moduleAttributes")) { + this.expectPlugin("importAssertions"); + } } } if (node.arguments.length === 0 || node.arguments.length > 2) { diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 856ccdeda4..9bc8851827 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2197,9 +2197,7 @@ export default class StatementParser extends ExpressionParser { const assertions = this.maybeParseImportAssertions(); if (assertions) { node.assertions = assertions; - } - // todo(Babel 8): remove module attributes support - else { + } else if (!process.env.BABEL_8_BREAKING) { const attributes = this.maybeParseModuleAttributes(); if (attributes) { node.attributes = attributes; diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index a9c8687f52..69286d5f4d 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -87,22 +87,28 @@ export function validatePlugins(plugins: PluginList) { } if (hasPlugin(plugins, "moduleAttributes")) { - if (hasPlugin(plugins, "importAssertions")) { + if (process.env.BABEL_8_BREAKING) { throw new Error( - "Cannot combine importAssertions and moduleAttributes plugins.", + "`moduleAttributes` has been removed in Babel 8, please use `importAssertions` parser plugin, or `@babel/plugin-syntax-import-assertions`.", ); - } - 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'.", + } else { + if (hasPlugin(plugins, "importAssertions")) { + throw new Error( + "Cannot combine importAssertions and moduleAttributes plugins.", + ); + } + 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'.", + ); + } } } 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 299e44f15c..6fb1dbfc24 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,3 +1,4 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)" + "throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)", + "BABEL_8_BREAKING": false } diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/options.json new file mode 100644 index 0000000000..29a3f0e841 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/options.json @@ -0,0 +1,3 @@ +{ + "BABEL_8_BREAKING": false +} diff --git a/packages/babel-parser/test/plugin-options.js b/packages/babel-parser/test/plugin-options.js index ff70783a82..da6e321db3 100644 --- a/packages/babel-parser/test/plugin-options.js +++ b/packages/babel-parser/test/plugin-options.js @@ -65,4 +65,13 @@ describe("plugin options", function () { expect(getParser(SYNTAX_2, [OPT_1, OPT_2])).toThrow(); }); }); + describe("'moduleAttributes' plugin", () => { + (process.env.BABEL_8_BREAKING ? it : it.skip)("removed in Babel 8", () => { + expect( + getParser("", ["moduleAttributes"]), + ).toThrowErrorMatchingInlineSnapshot( + `"\`moduleAttributes\` has been removed in Babel 8, please use \`importAssertions\` parser plugin, or \`@babel/plugin-syntax-import-assertions\`."`, + ); + }); + }); });