Skip to content

Commit 427ceb6

Browse files
[helpers TS conversion] legacy decorators (#16556)
Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
1 parent 02da3da commit 427ceb6

5 files changed

Lines changed: 39 additions & 20 deletions

File tree

packages/babel-helpers/src/helpers-generated.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ const helpers: Record<string, Helper> = {
4747
dependencies: {},
4848
},
4949
),
50-
// size: 450, gzip size: 270
50+
// size: 447, gzip size: 270
5151
applyDecoratedDescriptor: helper(
5252
"7.0.0-beta.0",
53-
'function _applyDecoratedDescriptor(i,e,r,n,l){var a={};return Object.keys(n).forEach((function(i){a[i]=n[i]})),a.enumerable=!!a.enumerable,a.configurable=!!a.configurable,("value"in a||a.initializer)&&(a.writable=!0),a=r.slice().reverse().reduce((function(r,n){return n(i,e,r)||r}),a),l&&void 0!==a.initializer&&(a.value=a.initializer?a.initializer.call(l):void 0,a.initializer=void 0),void 0===a.initializer&&(Object.defineProperty(i,e,a),a=null),a}',
53+
'function _applyDecoratedDescriptor(i,e,r,n,l){var a={};return Object.keys(n).forEach((function(i){a[i]=n[i]})),a.enumerable=!!a.enumerable,a.configurable=!!a.configurable,("value"in a||a.initializer)&&(a.writable=!0),a=r.slice().reverse().reduce((function(r,n){return n(i,e,r)||r}),a),l&&void 0!==a.initializer&&(a.value=a.initializer?a.initializer.call(l):void 0,a.initializer=void 0),void 0===a.initializer?(Object.defineProperty(i,e,a),null):a}',
5454
{
5555
globals: ["Object"],
5656
locals: { _applyDecoratedDescriptor: ["body.0.id"] },

packages/babel-helpers/src/helpers/applyDecoratedDescriptor.js renamed to packages/babel-helpers/src/helpers/applyDecoratedDescriptor.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
/* @minVersion 7.0.0-beta.0 */
22

3-
export default function _applyDecoratedDescriptor(
4-
target,
5-
property,
6-
decorators,
7-
descriptor,
8-
context,
3+
interface DescriptorWithInitializer extends PropertyDescriptor {
4+
initializer?: () => any;
5+
}
6+
7+
declare const Object: Omit<typeof globalThis.Object, "keys"> & {
8+
keys<T>(o: T): Array<keyof T>;
9+
};
10+
11+
export default function _applyDecoratedDescriptor<T>(
12+
target: T,
13+
property: PropertyKey,
14+
decorators: ((
15+
t: T,
16+
p: PropertyKey,
17+
desc: DescriptorWithInitializer,
18+
) => any)[],
19+
descriptor: DescriptorWithInitializer,
20+
context: DecoratorContext,
921
) {
10-
var desc = {};
22+
var desc: DescriptorWithInitializer = {};
1123
Object.keys(descriptor).forEach(function (key) {
1224
desc[key] = descriptor[key];
1325
});
@@ -26,12 +38,12 @@ export default function _applyDecoratedDescriptor(
2638

2739
if (context && desc.initializer !== void 0) {
2840
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
29-
desc.initializer = undefined;
41+
desc.initializer = void 0;
3042
}
3143

3244
if (desc.initializer === void 0) {
3345
Object.defineProperty(target, property, desc);
34-
desc = null;
46+
return null;
3547
}
3648

3749
return desc;

packages/babel-helpers/src/helpers/initializerDefineProperty.js renamed to packages/babel-helpers/src/helpers/initializerDefineProperty.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
/* @minVersion 7.0.0-beta.0 */
22

3-
export default function _initializerDefineProperty(
4-
target,
5-
property,
6-
descriptor,
7-
context,
8-
) {
3+
interface DescriptorWithInitializer extends PropertyDescriptor {
4+
initializer?: () => any;
5+
}
6+
7+
export default function _initializerDefineProperty<T>(
8+
target: T,
9+
property: PropertyKey,
10+
descriptor: DescriptorWithInitializer | undefined,
11+
context: DecoratorContext,
12+
): void {
913
if (!descriptor) return;
1014

1115
Object.defineProperty(target, property, {

packages/babel-helpers/src/helpers/initializerWarningHelper.js renamed to packages/babel-helpers/src/helpers/initializerWarningHelper.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/* @minVersion 7.0.0-beta.0 */
22

3-
// eslint-disable-next-line no-unused-vars
4-
export default function _initializerWarningHelper(descriptor, context) {
3+
/* eslint-disable @typescript-eslint/no-unused-vars */
4+
export default function _initializerWarningHelper(
5+
descriptor: PropertyDescriptor,
6+
context: DecoratorContext,
7+
): never {
58
throw new Error(
69
"Decorating class property failed. Please ensure that " +
710
"transform-class-properties is enabled and runs after the decorators transform.",

packages/babel-runtime-corejs3/helpers/esm/applyDecoratedDescriptor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ function _applyDecoratedDescriptor(i, e, r, n, l) {
1111
a[i] = n[i];
1212
}), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = !0), a = _reduceInstanceProperty(_context2 = _reverseInstanceProperty(_context3 = _sliceInstanceProperty(r).call(r)).call(_context3)).call(_context2, function (r, n) {
1313
return n(i, e, r) || r;
14-
}, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer && (_Object$defineProperty(i, e, a), a = null), a;
14+
}, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (_Object$defineProperty(i, e, a), null) : a;
1515
}
1616
export { _applyDecoratedDescriptor as default };

0 commit comments

Comments
 (0)