DEV: Allow parent method to be called using `super.` in `modifyClass` (#29389)
This commit is contained in:
parent
4c80beeec9
commit
f84f7436c5
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue