Skip to content

Commit

Permalink
allow 2023-11 decorator version
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jan 27, 2024
1 parent 0024e8d commit cb47c63
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ type ClassElement =
| t.TSIndexSignature
| t.StaticBlock;

type DecoratorVersionKind = "2023-05" | "2023-01" | "2022-03" | "2021-12";
// TODO(Babel 8): Only keep 2023-11
export type DecoratorVersionKind =
| "2023-11"
| "2023-05"
| "2023-01"
| "2022-03"
| "2021-12";

function incrementId(id: number[], idx = id.length - 1): void {
// If index is -1, id needs an additional character, unshift A
Expand Down Expand Up @@ -181,7 +187,9 @@ function addProxyAccessorsFor(
const { static: isStatic } = element.node;

const thisArg =
version === "2023-05" && isStatic ? className : t.thisExpression();
(version === "2023-11" || version === "2023-05") && isStatic
? className
: t.thisExpression();

const getterBody = t.blockStatement([
t.returnStatement(
Expand Down Expand Up @@ -244,7 +252,7 @@ function extractProxyAccessorsFor(
targetKey: t.PrivateName,
version: DecoratorVersionKind,
): (t.FunctionExpression | t.ArrowFunctionExpression)[] {
if (version !== "2023-05" && version !== "2023-01") {
if (version !== "2023-11" && version !== "2023-05" && version !== "2023-01") {
return [
template.expression.ast`
function () {
Expand Down Expand Up @@ -514,7 +522,7 @@ function generateDecorationList(
const hasOneThis = decoratorsThis.some(Boolean);
const decs: t.Expression[] = [];
for (let i = 0; i < decsCount; i++) {
if (version === "2023-05" && hasOneThis) {
if ((version === "2023-11" || version === "2023-05") && hasOneThis) {
decs.push(
decoratorsThis[i] || t.unaryExpression("void", t.numericLiteral(0)),
);
Expand All @@ -539,7 +547,10 @@ function generateDecorationExprs(

let flag = el.kind;
if (el.isStatic) {
flag += version === "2023-05" ? STATIC : STATIC_OLD_VERSION;
flag +=
version === "2023-11" || version === "2023-05"
? STATIC
: STATIC_OLD_VERSION;
}
if (hasThis) flag += DECORATORS_HAVE_THIS;

Expand Down Expand Up @@ -860,7 +871,10 @@ function transformClass(
let needMemoise = false;
for (const decorator of decorators) {
const { expression } = decorator;
if (version === "2023-05" && t.isMemberExpression(expression)) {
if (
(version === "2023-11" || version === "2023-05") &&
t.isMemberExpression(expression)
) {
let object;
if (
t.isSuper(expression.object) ||
Expand Down Expand Up @@ -1296,7 +1310,12 @@ function transformClass(
}

let { superClass } = originalClass;
if (superClass && (process.env.BABEL_8_BREAKING || version === "2023-05")) {
if (
superClass &&
(process.env.BABEL_8_BREAKING ||
version === "2023-11" ||
version === "2023-05")
) {
const id = path.scope.maybeGenerateMemoised(superClass);
if (id) {
originalClass.superClass = t.assignmentExpression("=", id, superClass);
Expand Down Expand Up @@ -1389,7 +1408,11 @@ function createLocalsAssignment(
}
}

if (process.env.BABEL_8_BREAKING || version === "2023-05") {
if (
process.env.BABEL_8_BREAKING ||
version === "2023-11" ||
version === "2023-05"
) {
if (
maybePrivateBranName ||
superClass ||
Expand All @@ -1407,7 +1430,11 @@ function createLocalsAssignment(
args.push(t.unaryExpression("void", t.numericLiteral(0)));
}
if (superClass) args.push(superClass);
rhs = t.callExpression(state.addHelper("applyDecs2305"), args);
if (version === "2023-11") {
rhs = t.callExpression(state.addHelper("applyDecs2311"), args);
} else {
rhs = t.callExpression(state.addHelper("applyDecs2305"), args);
}
} else if (version === "2023-01") {
if (maybePrivateBranName) {
args.push(
Expand Down Expand Up @@ -1658,14 +1685,17 @@ function isDecoratedAnonymousClassExpression(path: NodePath) {
export default function (
{ assertVersion, assumption }: PluginAPI,
{ loose }: Options,
// TODO(Babel 8): Only keep 2023-05
version: "2023-05" | "2023-01" | "2022-03" | "2021-12",
version: DecoratorVersionKind,
inherits: PluginObject["inherits"],
): PluginObject {
if (process.env.BABEL_8_BREAKING) {
assertVersion(process.env.IS_PUBLISH ? PACKAGE_JSON.version : "^7.21.0");
} else {
if (version === "2023-05" || version === "2023-01") {
if (
version === "2023-11" ||
version === "2023-05" ||
version === "2023-01"
) {
assertVersion("^7.21.0");
} else if (version === "2021-12") {
assertVersion("^7.16.0");
Expand Down
12 changes: 7 additions & 5 deletions packages/babel-helper-create-class-features-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { NodePath } from "@babel/traverse";
import nameFunction from "@babel/helper-function-name";
import splitExportDeclaration from "@babel/helper-split-export-declaration";
import createDecoratorTransform from "./decorators.ts";
import type { DecoratorVersionKind } from "./decorators.ts";

import semver from "semver";

Expand Down Expand Up @@ -36,7 +37,7 @@ interface Options {
inherits?: PluginObject["inherits"];
manipulateOptions?: PluginObject["manipulateOptions"];
api?: PluginAPI;
decoratorVersion?: "2023-05" | "2023-01" | "2022-03" | "2021-12" | "2018-09";
decoratorVersion?: DecoratorVersionKind | "2018-09";
}

export function createClassFeaturePlugin({
Expand All @@ -50,13 +51,14 @@ export function createClassFeaturePlugin({
}: Options): PluginObject {
if (feature & FEATURES.decorators) {
if (process.env.BABEL_8_BREAKING) {
return createDecoratorTransform(api, { loose }, "2023-05", inherits);
return createDecoratorTransform(api, { loose }, "2023-11", inherits);
} else {
if (
decoratorVersion === "2021-12" ||
decoratorVersion === "2022-03" ||
decoratorVersion === "2023-11" ||
decoratorVersion === "2023-05" ||
decoratorVersion === "2023-01" ||
decoratorVersion === "2023-05"
decoratorVersion === "2022-03" ||
decoratorVersion === "2021-12"
) {
return createDecoratorTransform(
api,
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-helpers/src/helpers/applyDecs2311.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @minVersion 7.21.0 */
/* @minVersion 7.23.0 */
/* @mangleFns */

import checkInRHS from "./checkInRHS.ts";
Expand Down Expand Up @@ -189,7 +189,7 @@ type DecoratorInfo =
initializeClass(Class);
*/

export default /* @no-mangle */ function applyDecs2305(
export default /* @no-mangle */ function applyDecs2311(
targetClass: any,
memberDecs: DecoratorInfo[],
classDecs: Function[],
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-plugin-proposal-decorators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export default declare((api, options: Options) => {
version === "2021-12" ||
version === "2022-03" ||
version === "2023-01" ||
version === "2023-05"
version === "2023-05" ||
version === "2023-11"
) {
api.assertVersion(
process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH
Expand All @@ -65,7 +66,7 @@ export default declare((api, options: Options) => {
});
} else {
throw new Error(
"The '.version' option must be one of 'legacy', '2023-05', '2023-01', '2022-03', or '2021-12'.",
"The '.version' option must be one of 'legacy', '2023-11', '2023-05', '2023-01', '2022-03', or '2021-12'.",
);
}
});
15 changes: 11 additions & 4 deletions packages/babel-plugin-syntax-decorators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { declare } from "@babel/helper-plugin-utils";
export interface Options {
// TODO(Babel 8): Remove
legacy?: boolean;
// TODO(Babel 8): Remove "2018-09", "2021-12", '2022-03', and '2023-01'
// TODO(Babel 8): Remove "2018-09", "2021-12", '2022-03', '2023-01' and '2023-05'
version?:
| "legacy"
| "2018-09"
| "2021-12"
| "2022-03"
| "2023-01"
| "2023-05";
| "2023-05"
| "2023-11";
// TODO(Babel 8): Remove
decoratorsBeforeExport?: boolean;
}
Expand All @@ -28,10 +29,11 @@ export default declare((api, options: Options) => {
if (version === undefined) {
throw new Error(
"The decorators plugin requires a 'version' option, whose value must be one of: " +
"'2023-05', '2023-01', '2022-03', '2021-12', '2018-09', or 'legacy'.",
"'2023-11', '2023-05', '2023-01', '2022-03', '2021-12', '2018-09', or 'legacy'.",
);
}
if (
version !== "2023-11" &&
version !== "2023-05" &&
version !== "2023-01" &&
version !== "2022-03" &&
Expand Down Expand Up @@ -67,6 +69,7 @@ export default declare((api, options: Options) => {
if (version === undefined) {
version = legacy ? "legacy" : "2018-09";
} else if (
version !== "2023-11" &&
version !== "2023-05" &&
version !== "2023-01" &&
version !== "2022-03" &&
Expand Down Expand Up @@ -118,7 +121,11 @@ export default declare((api, options: Options) => {
"decoratorAutoAccessors",
);
} else {
if (version === "2023-01" || version === "2023-05") {
if (
version === "2023-01" ||
version === "2023-05" ||
version === "2023-11"
) {
parserOpts.plugins.push(
["decorators", { allowCallParenthesized: false }],
"decoratorAutoAccessors",
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-runtime-corejs2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@
"./helpers/applyDecs2305.js"
],
"./helpers/esm/applyDecs2305": "./helpers/esm/applyDecs2305.js",
"./helpers/applyDecs2311": [
{
"node": "./helpers/applyDecs2311.js",
"import": "./helpers/esm/applyDecs2311.js",
"default": "./helpers/applyDecs2311.js"
},
"./helpers/applyDecs2311.js"
],
"./helpers/esm/applyDecs2311": "./helpers/esm/applyDecs2311.js",
"./helpers/asyncGeneratorDelegate": [
{
"node": "./helpers/asyncGeneratorDelegate.js",
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-runtime-corejs3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@
"./helpers/applyDecs2305.js"
],
"./helpers/esm/applyDecs2305": "./helpers/esm/applyDecs2305.js",
"./helpers/applyDecs2311": [
{
"node": "./helpers/applyDecs2311.js",
"import": "./helpers/esm/applyDecs2311.js",
"default": "./helpers/applyDecs2311.js"
},
"./helpers/applyDecs2311.js"
],
"./helpers/esm/applyDecs2311": "./helpers/esm/applyDecs2311.js",
"./helpers/asyncGeneratorDelegate": [
{
"node": "./helpers/asyncGeneratorDelegate.js",
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@
"./helpers/applyDecs2305.js"
],
"./helpers/esm/applyDecs2305": "./helpers/esm/applyDecs2305.js",
"./helpers/applyDecs2311": [
{
"node": "./helpers/applyDecs2311.js",
"import": "./helpers/esm/applyDecs2311.js",
"default": "./helpers/applyDecs2311.js"
},
"./helpers/applyDecs2311.js"
],
"./helpers/esm/applyDecs2311": "./helpers/esm/applyDecs2311.js",
"./helpers/asyncGeneratorDelegate": [
{
"node": "./helpers/asyncGeneratorDelegate.js",
Expand Down

0 comments on commit cb47c63

Please sign in to comment.