[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:
Huáng Jùnliàng 2021-05-14 09:55:40 -04:00 committed by GitHub
parent b670c11070
commit b3d35cd412
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 29 deletions

View File

@ -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();

View File

@ -7,5 +7,6 @@
}
]
],
"sourceType": "module"
"sourceType": "module",
"BABEL_8_BREAKING": false
}

View File

@ -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) {

View File

@ -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;

View File

@ -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'.",
);
}
}
}

View File

@ -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
}

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}

View File

@ -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\`."`,
);
});
});
});