DEV: Allow parent method to be called using `super.` in `modifyClass` (#29389)

This commit is contained in:
David Taylor 2024-10-24 12:11:02 +01:00 committed by GitHub
parent 4c80beeec9
commit f84f7436c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -42,7 +42,9 @@ export default function classPrepend(klass, callback) {
// Make a fake class which is a copy of the klass at this point in time. This provides the 'super' // Make a fake class which is a copy of the klass at this point in time. This provides the 'super'
// implementation. // implementation.
const FakeSuperclass = class {}; const klassProto = Object.getPrototypeOf(klass);
const FakeSuperclass =
klassProto !== Function.prototype ? class extends klassProto {} : class {};
Object.defineProperties(FakeSuperclass, originalKlassDescs); Object.defineProperties(FakeSuperclass, originalKlassDescs);
Object.defineProperties(FakeSuperclass.prototype, originalProtoDescs); Object.defineProperties(FakeSuperclass.prototype, originalProtoDescs);

View File

@ -275,4 +275,26 @@ module("Unit | class-prepend", function () {
assert.strictEqual(new Topic().someFunction(), 1, "change is rolled back"); assert.strictEqual(new Topic().someFunction(), 1, "change is rolled back");
}); });
test("can override method from parent, with super support", function (assert) {
class Parent {
someFunction() {
return 1;
}
}
class Child extends Parent {}
classPrepend(
Child,
(Superclass) =>
class extends Superclass {
someFunction() {
return super.someFunction() + 1;
}
}
);
assert.strictEqual(new Child().someFunction(), 2, "it works");
});
}); });