Skip to content

Commit

Permalink
fix(es/helpers): Add addInitializer to field decorators (#8619)
Browse files Browse the repository at this point in the history
**Description:**

At some point the spec text of ES decorators was changed to also have
addInitializer for field decorators. This is already used e.g. in mobx.
This simple change removes the special case that was present for field
decorators and seems to fix the issue for me.

This change is backwards compatible since it only affects code that was
broken before.

**Related issue:**

 - Closes #8525
  • Loading branch information
phiresky authored Feb 14, 2024
1 parent cb95f04 commit cd38ef3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 35 deletions.
22 changes: 8 additions & 14 deletions crates/swc_ecma_transforms_base/src/helpers/_apply_decs_2203_r.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ function applyDecs2203RFactory() {

var decoratorFinishedRef = { v: false };

if (kind !== 0 /* FIELD */) {
ctx.addInitializer = createAddInitializerMethod(
initializers,
decoratorFinishedRef
);
}
ctx.addInitializer = createAddInitializerMethod(
initializers,
decoratorFinishedRef
);

var get, set;
if (kind === 0 /* FIELD */) {
Expand Down Expand Up @@ -376,17 +374,13 @@ function applyDecs2203RFactory() {
base = Class;
kind = kind - 5 /* STATIC */;
// initialize staticInitializers when we see a non-field static member
if (kind !== 0 /* FIELD */) {
staticInitializers = staticInitializers || [];
initializers = staticInitializers;
}
staticInitializers = staticInitializers || [];
initializers = staticInitializers;
} else {
base = Class.prototype;
// initialize protoInitializers when we see a non-field member
if (kind !== 0 /* FIELD */) {
protoInitializers = protoInitializers || [];
initializers = protoInitializers;
}
protoInitializers = protoInitializers || [];
initializers = protoInitializers;
}

if (kind !== 0 /* FIELD */ && !isPrivate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ expect(aContext.name).toBe('#a');
expect(aContext.kind).toBe('field');
expect(aContext.static).toBe(false);
expect(aContext.private).toBe(true);
expect(typeof aContext.addInitializer).toBe('undefined');
expect(typeof aContext.addInitializer).toBe('function');

expect(bContext.access.get.call(foo)).toBe(124);
bContext.access.set.call(foo, 123);
Expand All @@ -34,4 +34,4 @@ expect(bContext.name).toBe('#b');
expect(bContext.kind).toBe('field');
expect(bContext.static).toBe(false);
expect(bContext.private).toBe(true);
expect(typeof aContext.addInitializer).toBe('undefined');
expect(typeof aContext.addInitializer).toBe('function');
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('field');
expect(aContext.static).toBe(false);
expect(aContext.private).toBe(false);
expect(typeof aContext.addInitializer).toBe('undefined');
expect(typeof aContext.addInitializer).toBe('function');
expect(foo.hasOwnProperty('a')).toBe(true);
expect(Foo.prototype.hasOwnProperty('a')).toBe(false);

Expand All @@ -45,7 +45,7 @@ expect(bContext.name).toBe('b');
expect(bContext.kind).toBe('field');
expect(bContext.static).toBe(false);
expect(bContext.private).toBe(false);
expect(typeof bContext.addInitializer).toBe('undefined');
expect(typeof bContext.addInitializer).toBe('function');
expect(foo.hasOwnProperty('b')).toBe(true);
expect(Foo.prototype.hasOwnProperty('b')).toBe(false);

Expand All @@ -56,6 +56,6 @@ expect(cContext.name).toBe('c');
expect(cContext.kind).toBe('field');
expect(cContext.static).toBe(false);
expect(cContext.private).toBe(false);
expect(typeof cContext.addInitializer).toBe('undefined');
expect(typeof cContext.addInitializer).toBe('function');
expect(foo.hasOwnProperty('c')).toBe(true);
expect(Foo.prototype.hasOwnProperty('c')).toBe(false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ expect(aContext.name).toBe('#a');
expect(aContext.kind).toBe('field');
expect(aContext.static).toBe(true);
expect(aContext.private).toBe(true);
expect(typeof aContext.addInitializer).toBe('undefined');
expect(typeof aContext.addInitializer).toBe('function');

expect(bContext.access.get.call(Foo)).toBe(124);
bContext.access.set.call(Foo, 123);
Expand All @@ -32,4 +32,4 @@ expect(bContext.name).toBe('#b');
expect(bContext.kind).toBe('field');
expect(bContext.static).toBe(true);
expect(bContext.private).toBe(true);
expect(typeof aContext.addInitializer).toBe('undefined');
expect(typeof aContext.addInitializer).toBe('function');
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('field');
expect(aContext.static).toBe(true);
expect(aContext.private).toBe(false);
expect(typeof aContext.addInitializer).toBe('undefined');
expect(typeof aContext.addInitializer).toBe('function');
expect(Foo.hasOwnProperty('a')).toBe(true);

expect(Foo.b).toBe(124);
Expand All @@ -42,7 +42,7 @@ expect(bContext.name).toBe('b');
expect(bContext.kind).toBe('field');
expect(bContext.static).toBe(true);
expect(bContext.private).toBe(false);
expect(typeof bContext.addInitializer).toBe('undefined');
expect(typeof bContext.addInitializer).toBe('function');
expect(Foo.hasOwnProperty('b')).toBe(true);

expect(Foo.c).toBe(457);
Expand All @@ -52,5 +52,5 @@ expect(cContext.name).toBe('c');
expect(cContext.kind).toBe('field');
expect(cContext.static).toBe(true);
expect(cContext.private).toBe(false);
expect(typeof cContext.addInitializer).toBe('undefined');
expect(typeof cContext.addInitializer).toBe('function');
expect(Foo.hasOwnProperty('c')).toBe(true);
16 changes: 5 additions & 11 deletions packages/helpers/esm/_apply_decs_2203_r.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ export function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentCla

var decoratorFinishedRef = { v: false };

if (kind !== 0 /* FIELD */) {
ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
}
ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);

var get, set;
if (kind === 0 /* FIELD */) {
Expand Down Expand Up @@ -306,17 +304,13 @@ export function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentCla
base = Class;
kind = kind - 5 /* STATIC */;
// initialize staticInitializers when we see a non-field static member
if (kind !== 0 /* FIELD */) {
staticInitializers = staticInitializers || [];
initializers = staticInitializers;
}
staticInitializers = staticInitializers || [];
initializers = staticInitializers;
} else {
base = Class.prototype;
// initialize protoInitializers when we see a non-field member
if (kind !== 0 /* FIELD */) {
protoInitializers = protoInitializers || [];
initializers = protoInitializers;
}
protoInitializers = protoInitializers || [];
initializers = protoInitializers;
}

if (kind !== 0 /* FIELD */ && !isPrivate) {
Expand Down

0 comments on commit cd38ef3

Please sign in to comment.