Skip to content

Commit

Permalink
[helpers TS conversion] New decorators (#16540)
Browse files Browse the repository at this point in the history
* New decorators Helpers

* review
  • Loading branch information
liuxingbaoyu authored Jun 21, 2024
1 parent ec7626f commit 8a87859
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 49 deletions.
56 changes: 29 additions & 27 deletions packages/babel-helpers/src/helpers/applyDecs2311.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* @minVersion 7.24.0 */
/* @mangleFns */

/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion -- `typescript-eslint` complains when using `!` */

import checkInRHS from "./checkInRHS.ts";
import setFunctionName from "./setFunctionName.ts";
import toPropertyKey from "./toPropertyKey.ts";
Expand Down Expand Up @@ -266,14 +268,14 @@ export default /* @no-mangle */ function applyDecs2311(
hasPrivateBrand?: Function,
) {
function assertInstanceIfPrivate(target: any) {
if (!hasPrivateBrand(target)) {
if (!hasPrivateBrand!(target)) {
throw new TypeError(
"Attempted to access private element on non-instance",
);
}
}

var decs = [].concat(decInfo[0]),
var decs = ([] as Function[]).concat(decInfo[0]),
decVal = decInfo[3],
isClass = !ret;

Expand Down Expand Up @@ -314,13 +316,13 @@ export default /* @no-mangle */ function applyDecs2311(
desc = {
get: setFunctionName(
function (this: any) {
return decVal(this);
return decVal!(this);
},
name,
"get",
),
set: function (this: any, value: any) {
decInfo[4](this, value);
decInfo[4]!(this, value);
},
};
} else {
Expand All @@ -331,11 +333,11 @@ export default /* @no-mangle */ function applyDecs2311(
setFunctionName(desc[key], name, isMethod ? "" : key);
}
} else if (!isField) {
desc = Object.getOwnPropertyDescriptor(Class, name);
desc = Object.getOwnPropertyDescriptor(Class, name)!;
}

if (!isField && !isPrivate) {
_ = existingNonFields[+isStatic][name];
_ = existingNonFields[+isStatic!][name];
// flag is 1, 3, or 4; kind is 0, 1, 2, 3, or 4
// flag ^ kind is 7 if and only if one of them is 3 and the other one is 4.
if (_ && (_ ^ kind) !== 7) {
Expand All @@ -347,7 +349,7 @@ export default /* @no-mangle */ function applyDecs2311(
}
// We use PROP_KIND.ACCESSOR to mark a name as "fully used":
// either a get/set pair, or a non-getter/setter.
existingNonFields[+isStatic][name] =
existingNonFields[+isStatic!][name] =
kind < PROP_KIND.GETTER
? PROP_KIND.ACCESSOR
: (kind as PROP_KIND.GETTER | PROP_KIND.SETTER);
Expand Down Expand Up @@ -426,24 +428,24 @@ export default /* @no-mangle */ function applyDecs2311(
decThis,
isAccessor
? {
get: desc.get,
set: desc.set,
get: desc!.get,
set: desc!.set,
}
: desc[key],
: desc![key!],
ctx,
);
decoratorFinishedRef.v = 1;

if (isAccessor) {
if (typeof newValue === "object" && newValue) {
if ((_ = assertCallable(newValue.get, "accessor.get"))) {
desc.get = _;
desc!.get = _;
}
if ((_ = assertCallable(newValue.set, "accessor.set"))) {
desc.set = _;
desc!.set = _;
}
if ((_ = assertCallable(newValue.init, "accessor.init"))) {
init.unshift(_);
init!.unshift(_);
}
} else if (newValue !== void 0) {
throw new TypeError(
Expand All @@ -458,19 +460,19 @@ export default /* @no-mangle */ function applyDecs2311(
)
) {
if (isField) {
init.unshift(newValue);
init!.unshift(newValue);
} else {
desc[key] = newValue;
desc![key!] = newValue;
}
}
}
}

// isField || isAccessor
if (kind < PROP_KIND.METHOD) {
ret.push(
ret!.push(
// init
createRunInitializers(init, isStatic, 1),
createRunInitializers(init!, isStatic, 1),
// init_extra
createRunInitializers(initializers, isStatic, 0),
);
Expand All @@ -480,22 +482,22 @@ export default /* @no-mangle */ function applyDecs2311(
if (isPrivate) {
if (isAccessor) {
// get and set should be returned before init_extra
ret.splice(
ret!.splice(
-1,
0,
_bindPropCall("get", isStatic),
_bindPropCall("set", isStatic),
_bindPropCall("get", isStatic!),
_bindPropCall("set", isStatic!),
);
} else {
ret.push(
ret!.push(
isMethod
? desc[key]
? desc![key!]
: // Equivalent to `Function.call`, just to reduce code size
assertCallable.call.bind(desc[key]),
assertCallable.call.bind(desc![key!]),
);
}
} else {
defineProperty(Class, name, desc);
defineProperty(Class, name, desc!);
}
}
return newValue;
Expand All @@ -520,7 +522,7 @@ export default /* @no-mangle */ function applyDecs2311(
for (var i = 0; i < memberDecs.length; i++) {
var decInfo = memberDecs[i];

var kind = decInfo[1];
var kind = decInfo[1]!;
var kindOnly: PROP_KIND = kind & PROP_KIND.KIND_MASK;
if (
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison, eqeqeq
Expand Down Expand Up @@ -565,8 +567,8 @@ export default /* @no-mangle */ function applyDecs2311(
applyMemberDecsOfKind(PROP_KIND.STATIC, 1);
applyMemberDecsOfKind(0, 1);

pushInitializers(protoInitializers);
pushInitializers(staticInitializers);
pushInitializers(protoInitializers!);
pushInitializers(staticInitializers!);
return ret;
}

Expand Down
8 changes: 0 additions & 8 deletions packages/babel-helpers/src/helpers/defineAccessor.js

This file was deleted.

12 changes: 12 additions & 0 deletions packages/babel-helpers/src/helpers/defineAccessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* @minVersion 7.20.7 */

export default function _defineAccessor<Type extends "get" | "set">(
type: Type,
obj: any,
key: string | symbol,
fn: PropertyDescriptor[Type],
) {
var desc: PropertyDescriptor = { configurable: true, enumerable: true };
desc[type] = fn;
return Object.defineProperty(obj, key, desc);
}
5 changes: 0 additions & 5 deletions packages/babel-helpers/src/helpers/identity.js

This file was deleted.

5 changes: 5 additions & 0 deletions packages/babel-helpers/src/helpers/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* @minVersion 7.17.0 */

export default function _identity<T>(x: T) {
return x;
}
3 changes: 2 additions & 1 deletion packages/babel-helpers/src/helpers/setFunctionName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default function setFunctionName<T extends Function>(
prefix?: string,
): T {
if (typeof name === "symbol") {
name = name.description;
// Here `undefined` is possible, we check for it in the next line.
name = name.description!;
name = name ? "[" + name + "]" : "";
}
// In some older browsers .name was non-configurable, here we catch any
Expand Down
9 changes: 2 additions & 7 deletions packages/babel-helpers/src/helpers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"rootDir": ".",
"target": "ESNext",
"module": "preserve",
"module": "Preserve",
"lib": ["ESNext"],
"declaration": false,
"noEmit": true,
Expand All @@ -13,10 +13,5 @@
"strict": true
},
"include": ["./*.ts"],
"exclude": [
"./applyDecs2305.ts",
"./applyDecs2311.ts",
"./construct.ts",
"./setFunctionName.ts"
]
"exclude": ["./applyDecs2305.ts", "./construct.ts", "./usingCtx.ts"]
}
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"composite": true,
"rootDir": ".",
"target": "ESNext",
"module": "preserve",
"module": "Preserve",
"lib": ["ESNext"],
"declaration": true,
"declarationMap": true,
Expand Down

0 comments on commit 8a87859

Please sign in to comment.