Simplify _initProto injection for existing super()

This commit is contained in:
Nicolò Ribaudo 2022-01-09 00:46:37 +01:00
parent 0f7463e77d
commit 82612add5c
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++) {
initializers[i].call(instance, instance);
}
return instance;
});
} else {
ret.push(function () {});
ret.push(function (instance) {
return instance;
});
}
}

View File

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

View File

@ -7,9 +7,9 @@ class A extends B {
constructor() {
if (Math.random() > 0.5) {
super(true), _initProto(this), this;
_initProto(super(true));
} else {
super(false), _initProto(this), this;
_initProto(super(false));
}
}
@ -24,7 +24,7 @@ class C extends B {
constructor() {
try {
super((super(), _initProto2(this), this), null.x), _initProto2(this), this;
_initProto2(super(_initProto2(super()), null.x));
} 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() {
let a = 2;
super(a), _initProto(this), this;
_initProto(super(a));
foo();
}