Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decorator initProto usage in derived classes #16170

Merged
merged 5 commits into from Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -949,7 +949,7 @@ function transformClass(
value.replaceWith(t.sequenceExpression(body));
} else if (constructorPath) {
if (path.node.superClass) {
path.traverse({
constructorPath.traverse({
CallExpression: {
exit(path) {
if (!path.get("callee").isSuper()) return;
Expand All @@ -961,6 +961,11 @@ function transformClass(
path.skip();
},
},
ClassMethod(path) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we skip ClassExpression?

if (path.node.kind === "constructor") {
path.skip();
}
},
});
} else {
constructorPath.node.body.body.unshift(
Expand Down Expand Up @@ -1336,7 +1341,7 @@ function NamedEvaluationVisitoryFactory(
t.assignmentExpression(
"=",
ref,
t.callExpression(state.addHelper("toPropertyKey"), [key]),
createToPropertyKeyCall(state, key),
),
);
return t.cloneNode(ref);
Expand Down
@@ -1,28 +1,173 @@
let self, a, initCalled;
{
let self, a, initCalled;

function deco(_, context) {
context.addInitializer(() => {
initCalled = true;
})
}
function deco(_, context) {
context.addInitializer(() => {
initCalled = true;
});
}

class B {
constructor(s) {
a = s;
class B {
constructor(s) {
a = s;
}
}
}

class A extends B {
constructor() {
let a = 2;
self = super(a);
class A extends B {
constructor() {
let a = 2;
self = super(a);
}

@deco
method() {}
}

@deco
method() {}
let instance = new A();
expect(self).toBe(instance);
expect(a).toBe(2);
expect(initCalled).toBe(true);
}

let instance = new A();
expect(self).toBe(instance);
expect(a).toBe(2);
expect(initCalled).toBe(true);
{
const dec = (fn) => {
return function () {
return fn.call(this) + 100;
};
};

class B {
constructor(v) {
this.a = v;
}
method() {
return this.a;
}
}

{
("super() nested within another constructor should not be transformed");
let log = [];
class A extends B {
constructor() {
log.push(super(3).method());
new (class Dummy extends B {
constructor() {
log.push(super(4).method());
}
})();
}

@dec
method() {
return this.a;
}
}

new A();

expect(log + "").toBe("103,4");
}

{
("super() not in decorated derived constructor should not be transformed: computed key");
let log = [];
new (class Dummy extends B {
constructor() {
let key;
class A extends B {
constructor() {
log.push(super(6).method());
}

@dec
method() {
return this.a;
}

[((key = super(5).method()), log.push(key), key)];
}

new A();
}
})();

expect(log + "").toBe("5,106");
}

{
("super() in decorator expression within decorated derived constructor should be transformed: decorator expression");
let log = [];
const noop = () => (fn) => fn;
new (class extends B {
constructor() {
class A extends B {
constructor() {
log.push(super(8).method());
}

@dec
method() {
return this.a;
}

@noop(log.push(super(7).method())) noop() {}
}

new A();
}
})();

expect(log + "").toBe("7,108");
}

{
("super() within decorated derived constructor should be transformed: computed key");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will fail if we skip the whole ClassExpression, ditto for ClassDeclaration.

let log = [];
class A extends B {
constructor() {
let key;
new (class Dummy extends B {
constructor() {
log.push(super(10).method());
}
[((key = super(9).method()), log.push(key), key)];
})();
}

@dec
method() {
return this.a;
}
}

new A();

expect(log + "").toBe("109,10");
}

{
("super() within decorated derived constructor should be transformed: decorator expression");
let log = [];
const noop = () => (fn) => fn;
class A extends B {
constructor() {
new (class Dummy extends B {
constructor() {
log.push(super(12).method());
}
@noop(log.push(super(11).method())) noop() {}
})();
}

@dec
method() {
return this.a;
}
}

new A();

expect(log + "").toBe("111,12");
}
}
@@ -0,0 +1,173 @@
{
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);
}

{
const dec = (fn) => {
return function () {
return fn.call(this) + 100;
};
};

class B {
constructor(v) {
this.a = v;
}
method() {
return this.a;
}
}

{
("super() nested within another constructor should not be transformed");
let log = [];
class A extends B {
constructor() {
log.push(super(3).method());
new (class Dummy extends B {
constructor() {
log.push(super(4).method());
}
})();
}

@dec
method() {
return this.a;
}
}

new A();

expect(log + "").toBe("103,4");
}

{
("super() not in decorated derived constructor should not be transformed: computed key");
let log = [];
new (class Dummy extends B {
constructor() {
let key;
class A extends B {
constructor() {
log.push(super(6).method());
}

@dec
method() {
return this.a;
}

[((key = super(5).method()), log.push(key), key)];
}

new A();
}
})();

expect(log + "").toBe("5,106");
}

{
("super() in decorator expression within decorated derived constructor should be transformed: decorator expression");
let log = [];
const noop = () => (fn) => fn;
new (class extends B {
constructor() {
class A extends B {
constructor() {
log.push(super(8).method());
}

@dec
method() {
return this.a;
}

@noop(log.push(super(7).method())) noop() {}
}

new A();
}
})();

expect(log + "").toBe("7,108");
}

{
("super() within decorated derived constructor should be transformed: computed key");
let log = [];
class A extends B {
constructor() {
let key;
new (class Dummy extends B {
constructor() {
log.push(super(10).method());
}
[((key = super(9).method()), log.push(key), key)];
})();
}

@dec
method() {
return this.a;
}
}

new A();

expect(log + "").toBe("109,10");
}

{
("super() within decorated derived constructor should be transformed: decorator expression");
let log = [];
const noop = () => (fn) => fn;
class A extends B {
constructor() {
new (class Dummy extends B {
constructor() {
log.push(super(12).method());
}
@noop(log.push(super(11).method())) noop() {}
})();
}

@dec
method() {
return this.a;
}
}

new A();

expect(log + "").toBe("111,12");
}
}