[Babel 8]: remove module attributes parser/generator support (#13308)
* breaking: remove support of moduleAttributes * Update packages/babel-parser/src/plugin-utils.js Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com> * chore: remove todo comments * make prettier happy Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
parent
b670c11070
commit
b3d35cd412
@ -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();
|
||||
|
||||
@ -7,5 +7,6 @@
|
||||
}
|
||||
]
|
||||
],
|
||||
"sourceType": "module"
|
||||
"sourceType": "module",
|
||||
"BABEL_8_BREAKING": false
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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'.",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
3
packages/babel-parser/test/fixtures/experimental/module-attributes/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/experimental/module-attributes/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false
|
||||
}
|
||||
@ -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\`."`,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user