refactor: centralize option validation in syntax-decorators
This commit is contained in:
parent
e52f1dc4b2
commit
be798df427
@ -12,36 +12,7 @@ import transformer2021_12 from "./transformer-2021-12";
|
|||||||
export default declare((api, options) => {
|
export default declare((api, options) => {
|
||||||
api.assertVersion(7);
|
api.assertVersion(7);
|
||||||
|
|
||||||
let { legacy = false } = options;
|
const { legacy, decoratorsBeforeExport, version } = options;
|
||||||
if (typeof legacy !== "boolean") {
|
|
||||||
throw new Error("'legacy' must be a boolean.");
|
|
||||||
}
|
|
||||||
const { decoratorsBeforeExport, version } = options;
|
|
||||||
if (version !== undefined && options.legacy !== undefined) {
|
|
||||||
throw new Error(
|
|
||||||
'You can either specify `legacy: true` or `version: "legacy"` with decorators, not both.',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
legacy ||= version === "legacy";
|
|
||||||
|
|
||||||
if (decoratorsBeforeExport === undefined) {
|
|
||||||
if (!legacy) {
|
|
||||||
throw new Error(
|
|
||||||
"The decorators plugin requires a 'decoratorsBeforeExport' option," +
|
|
||||||
" whose value must be a boolean. If you want to use the legacy" +
|
|
||||||
" decorators semantics, you can set the `version: 'legacy'` option.",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (legacy) {
|
|
||||||
throw new Error(
|
|
||||||
"'decoratorsBeforeExport' can't be used with legacy decorators.",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (typeof decoratorsBeforeExport !== "boolean") {
|
|
||||||
throw new Error("'decoratorsBeforeExport' must be a boolean.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (legacy) {
|
if (legacy) {
|
||||||
return {
|
return {
|
||||||
@ -52,24 +23,21 @@ export default declare((api, options) => {
|
|||||||
},
|
},
|
||||||
visitor: legacyVisitor,
|
visitor: legacyVisitor,
|
||||||
};
|
};
|
||||||
}
|
} else if (version === "2021-12") {
|
||||||
|
|
||||||
if (version === "2021-12") {
|
|
||||||
return transformer2021_12(api, options);
|
return transformer2021_12(api, options);
|
||||||
} else if (!(version === "2018-09" || version === undefined)) {
|
} else {
|
||||||
throw new Error("Unsupported decorators version: " + version);
|
const plugin = createClassFeaturePlugin({
|
||||||
|
name: "proposal-decorators",
|
||||||
|
|
||||||
|
api,
|
||||||
|
feature: FEATURES.decorators,
|
||||||
|
// loose: options.loose, Not supported
|
||||||
|
|
||||||
|
manipulateOptions({ generatorOpts }) {
|
||||||
|
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
plugin.inherits = syntaxDecorators;
|
||||||
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
return createClassFeaturePlugin({
|
|
||||||
name: "proposal-decorators",
|
|
||||||
|
|
||||||
api,
|
|
||||||
feature: FEATURES.decorators,
|
|
||||||
// loose: options.loose, Not supported
|
|
||||||
|
|
||||||
manipulateOptions({ generatorOpts, parserOpts }) {
|
|
||||||
parserOpts.plugins.push(["decorators", { decoratorsBeforeExport }]);
|
|
||||||
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,19 +3,29 @@ import { declare } from "@babel/helper-plugin-utils";
|
|||||||
export default declare((api, options) => {
|
export default declare((api, options) => {
|
||||||
api.assertVersion(7);
|
api.assertVersion(7);
|
||||||
|
|
||||||
const { legacy = false, version } = options;
|
let { legacy = false } = options;
|
||||||
if (typeof legacy !== "boolean") {
|
if (typeof legacy !== "boolean") {
|
||||||
throw new Error("'legacy' must be a boolean.");
|
throw new Error("'legacy' must be a boolean.");
|
||||||
}
|
}
|
||||||
|
const { decoratorsBeforeExport, version = "2018-09" } = options;
|
||||||
|
if (
|
||||||
|
!(version === "2021-12" || version === "2018-09" || version === "legacy")
|
||||||
|
) {
|
||||||
|
throw new Error("Unsupported decorators version: " + version);
|
||||||
|
}
|
||||||
|
if (version !== undefined && options.legacy !== undefined) {
|
||||||
|
throw new Error(
|
||||||
|
'You can either specify `legacy: true` or `version: "legacy"` with decorators, not both.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
legacy ||= version === "legacy";
|
||||||
|
|
||||||
const { decoratorsBeforeExport } = options;
|
|
||||||
if (decoratorsBeforeExport === undefined) {
|
if (decoratorsBeforeExport === undefined) {
|
||||||
if (!legacy) {
|
if (!legacy) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"The '@babel/plugin-syntax-decorators' plugin requires a" +
|
"The decorators plugin requires a 'decoratorsBeforeExport' option," +
|
||||||
" 'decoratorsBeforeExport' option, whose value must be a boolean." +
|
" whose value must be a boolean. If you want to use the legacy" +
|
||||||
" If you want to use the legacy decorators semantics, you can set" +
|
" decorators semantics, you can set the `version: 'legacy'` option.",
|
||||||
" the 'legacy: true' option.",
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -29,12 +39,6 @@ export default declare((api, options) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
|
||||||
!(version === "2021-12" || version === "2018-09" || version === undefined)
|
|
||||||
) {
|
|
||||||
throw new Error("Unsupported decorators version: " + version);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: "syntax-decorators",
|
name: "syntax-decorators",
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user