Simplify _initProto injection for existing super()

This commit is contained in:
Nicolò Ribaudo 2022-01-09 00:46:37 +01:00
parent 5ba1365f9a
commit 9a876a2a64
6 changed files with 40 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@ -518,9 +518,12 @@ function pushInitializers(ret, initializers) {
for (var i = 0; i < initializers.length; i++) { for (var i = 0; i < initializers.length; i++) {
initializers[i].call(instance, instance); initializers[i].call(instance, instance);
} }
return instance;
}); });
} else { } else {
ret.push(function () {}); ret.push(function (instance) {
return instance;
});
} }
} }

View File

@ -796,11 +796,7 @@ function transformClass(
if (!path.get("callee").isSuper()) return; if (!path.get("callee").isSuper()) return;
path.replaceWith( path.replaceWith(
t.sequenceExpression([ t.callExpression(t.cloneNode(protoInitLocal), [path.node]),
path.node,
t.cloneNode(protoInitCall),
t.thisExpression(),
]),
); );
path.skip(); path.skip();

View File

@ -7,9 +7,9 @@ class A extends B {
constructor() { constructor() {
if (Math.random() > 0.5) { if (Math.random() > 0.5) {
super(true), _initProto(this), this; _initProto(super(true));
} else { } else {
super(false), _initProto(this), this; _initProto(super(false));
} }
} }
@ -24,7 +24,7 @@ class C extends B {
constructor() { constructor() {
try { try {
super((super(), _initProto2(this), this), null.x), _initProto2(this), this; _initProto2(super(_initProto2(super()), null.x));
} catch {} } catch {}
} }

View File

@ -0,0 +1,28 @@
let self, a, initCalled;
function deco(_, context) {
context.addInitializer(() => {
initCalled = true;
})
}
class B {
constructor(s) {
a = s;
}
}
class A extends B {
constructor() {
let a = 2;
self = super(a);
}
@deco
method() {}
}
let instance = new A();
expect(self).toBe(instance);
expect(a).toBe(2);
expect(initCalled).toBe(true);

View File

@ -7,7 +7,9 @@ class A extends B {
constructor() { constructor() {
let a = 2; let a = 2;
super(a), _initProto(this), this;
_initProto(super(a));
foo(); foo();
} }