Skip to content

Commit

Permalink
Remove decoratorsBeforeExport from the syntax plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jun 25, 2022
1 parent 220e638 commit e7290d9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 45 deletions.
101 changes: 60 additions & 41 deletions packages/babel-plugin-syntax-decorators/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,83 @@
import { declare } from "@babel/helper-plugin-utils";

export interface Options {
// TODO(Babel 8): Remove
legacy?: boolean;
// TODO(Babel 8): Remove "2018-09"
version?: "legacy" | "2018-09" | "2021-12";
// TODO(Babel 8): Remove
decoratorsBeforeExport?: boolean;
}

const VALID_VERSIONS = process.env.BABEL_8_BREAKING
? new Set(["2021-12", "legacy"])
: new Set(["2021-12", "2018-09", "legacy"]);

export default declare((api, options: Options) => {
api.assertVersion(7);

if (!process.env.BABEL_8_BREAKING) {
let { version } = options;

if (process.env.BABEL_8_BREAKING) {
if (version === undefined) {
throw new Error(
"The decorators plugin requires a 'version' option, whose value must be one of: " +
"'2021-12', '2018-09', or 'legacy'.",
);
}
if (version !== "2021-12" && version !== "legacy") {
throw new Error("Unsupported decorators version: " + version);
}
if (options.legacy !== undefined) {
throw new Error(
`The .legacy option has been removed in Babel 8. Use .version: "legacy" instead.`,
);
}
if (options.decoratorsBeforeExport !== undefined) {
throw new Error(
`The .decoratorsBeforeExport option has been removed in Babel 8.`,
);
}
} else {
const { legacy } = options;
// eslint-disable-next-line no-var
var { version = legacy ? "legacy" : "2018-09" } = options;

if (legacy !== undefined) {
if (typeof legacy !== "boolean") {
throw new Error(".legacy must be a boolean.");
}
if (options.version !== undefined) {
if (version !== undefined) {
throw new Error(
"You can either use the .legacy or the .version option, not both.",
);
}
}
} else {
// eslint-disable-next-line no-var
var { version } = options;
}
if (version === undefined) {
throw new Error(
"The decorators plugin requires a 'version' option, whose value must be one of: " +
Array.from(VALID_VERSIONS, v => `'${v}'`).join(", "),
);
}
if (!VALID_VERSIONS.has(version)) {
throw new Error("Unsupported decorators version: " + version);
}

let { decoratorsBeforeExport } = options;
if (decoratorsBeforeExport === undefined) {
if (version === "2021-12") {
decoratorsBeforeExport = false;
if (version === undefined) {
version = legacy ? "legacy" : "2018-09";
} else if (
version !== "2021-12" &&
version !== "2018-09" &&
version !== "legacy"
) {
throw new Error("Unsupported decorators version: " + version);
}
if (!process.env.BABEL_8_BREAKING) {
if (version === "2018-09") {

// eslint-disable-next-line no-var
var { decoratorsBeforeExport } = options;
if (decoratorsBeforeExport === undefined) {
if (version === "2021-12") {
decoratorsBeforeExport = false;
} else if (version === "2018-09") {
throw new Error(
"The decorators plugin, when .version is '2018-09' or not specified," +
" requires a 'decoratorsBeforeExport' option, whose value must be a boolean.",
);
}
}
} else {
if (version === "legacy") {
throw new Error(
"'decoratorsBeforeExport' can't be used with legacy decorators.",
);
}
if (typeof decoratorsBeforeExport !== "boolean") {
throw new Error("'decoratorsBeforeExport' must be a boolean.");
} else {
if (version === "legacy") {
throw new Error(
"'decoratorsBeforeExport' can't be used with legacy decorators.",
);
}
if (typeof decoratorsBeforeExport !== "boolean") {
throw new Error("'decoratorsBeforeExport' must be a boolean.");
}
}
}

Expand All @@ -73,14 +87,19 @@ export default declare((api, options: Options) => {
manipulateOptions({ generatorOpts }, parserOpts) {
if (version === "legacy") {
parserOpts.plugins.push("decorators-legacy");
} else if (version === "2021-12") {
} else if (process.env.BABEL_8_BREAKING) {
parserOpts.plugins.push(
["decorators", { decoratorsBeforeExport }],
["decorators", { decoratorsBeforeExport: false }],
"decoratorAutoAccessors",
);
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
} else if (!process.env.BABEL_8_BREAKING) {
if (version === "2018-09") {
} else {
if (version === "2021-12") {
parserOpts.plugins.push(
["decorators", { decoratorsBeforeExport }],
"decoratorAutoAccessors",
);
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
} else if (version === "2018-09") {
parserOpts.plugins.push(["decorators", { decoratorsBeforeExport }]);
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/babel-plugin-syntax-decorators/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function makeParser(code, options) {
});
}

const babel7 = process.env.BABEL_8_BREAKING ? test.skip : test;
const babel8 = process.env.BABEL_8_BREAKING ? test : test.skip;
const babel7describe = process.env.BABEL_8_BREAKING ? describe.skip : describe;

Expand All @@ -32,14 +31,14 @@ babel7describe("'legacy' option", function () {
});
});

describe("'decoratorsBeforeExport' option", function () {
babel7describe("'decoratorsBeforeExport' option", function () {
test("must be boolean", function () {
expect(
makeParser("", { version: "2021-12", decoratorsBeforeExport: "before" }),
).toThrow();
});

babel7("is required with 2018-09 decorators", function () {
test("is required with 2018-09 decorators", function () {
expect(makeParser("", { legacy: false })).toThrow(/decoratorsBeforeExport/);
expect(makeParser("", { version: "2018-09" })).toThrow(
/decoratorsBeforeExport/,
Expand All @@ -52,7 +51,7 @@ describe("'decoratorsBeforeExport' option", function () {
).toThrow();
});

babel7("is incompatible with legacy when using the 'legacy' option", () => {
test("is incompatible with legacy when using the 'legacy' option", () => {
expect(
makeParser("", { decoratorsBeforeExport: false, legacy: true }),
).toThrow();
Expand Down

0 comments on commit e7290d9

Please sign in to comment.