Skip to content

Commit

Permalink
[BUGFIX LTS] Don't run getters while applying mixins
Browse files Browse the repository at this point in the history
This change ensures that getters are never evaluated while applying
mixins.

It relies on the fact that all getters (including undecorated ones) get
converted into classic decorators when the mixin is originally created.

(cherry picked from commit 87394c4)
  • Loading branch information
wycats authored and kategengler committed Mar 13, 2023
1 parent 7c84305 commit aa5e4ed
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
56 changes: 56 additions & 0 deletions packages/@ember/-internals/runtime/tests/mixins/accessor_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import EmberObject from '@ember/object';
import { moduleFor, RenderingTestCase } from 'internal-test-helpers';

moduleFor(
'runtime: Mixin Accessors',
class extends RenderingTestCase {
['@test works with getters'](assert) {
let value = 'building';

let Base = EmberObject.extend({
get foo() {
if (value === 'building') {
throw Error('base should not be called yet');
}

return "base's foo";
},
});

// force Base to be finalized so its properties will contain `foo`
Base.proto();

class Child extends Base {
get foo() {
if (value === 'building') {
throw Error('child should not be called yet');
}

return "child's foo";
}
}

Child.proto();

let Grandchild = Child.extend({
get foo() {
if (value === 'building') {
throw Error('grandchild should not be called yet');
}

return value;
},
});

let instance = Grandchild.create();

value = 'done building';

assert.equal(instance.foo, 'done building', 'getter defined correctly');

value = 'changed value';

assert.equal(instance.foo, 'changed value', 'the value is a real getter, not a snapshot');
}
}
);
33 changes: 20 additions & 13 deletions packages/@ember/object/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { guidFor, observerListenerMetaFor, ROOT, wrap } from '@ember/-internals/
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { _WeakSet } from '@glimmer/util';
import type {
ComputedDecorator,
ComputedPropertyGetter,
ComputedPropertyObj,
ComputedPropertySetter,
ComputedDescriptor,
import {
type ComputedDecorator,
type ComputedPropertyGetter,
type ComputedPropertyObj,
type ComputedPropertySetter,
type ComputedDescriptor,
isAccessor,
isClassicDecorator,
} from '@ember/-internals/metal';
import {
ComputedProperty,
Expand Down Expand Up @@ -235,7 +237,7 @@ function mergeMixins(
keys: string[],
keysWithSuper: string[]
): void {
let currentMixin;
let currentMixin: MixinLike | undefined;

for (let i = 0; i < mixins.length; i++) {
currentMixin = mixins[i];
Expand Down Expand Up @@ -309,12 +311,17 @@ function mergeProps(
let desc = meta.peekDescriptors(key);

if (desc === undefined) {
// The superclass did not have a CP, which means it may have
// observers or listeners on that property.
let prev = (values[key] = base[key]);

if (typeof prev === 'function') {
updateObserversAndListeners(base, key, prev, false);
// If the value is a classic decorator, we don't want to actually
// access it, because that will execute the decorator while we're
// building the class.
if (!isClassicDecorator(value)) {
// The superclass did not have a CP, which means it may have
// observers or listeners on that property.
let prev = (values[key] = base[key]);

if (typeof prev === 'function') {
updateObserversAndListeners(base, key, prev, false);
}
}
} else {
descs[key] = desc;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"outDir": "dist",
"noEmit": true
},
"include": ["packages/**/*.ts"],
"include": ["packages/**/*.ts", "packages/**/*.js"],
"exclude": ["dist", "node_modules", "tmp", "types"]
}

0 comments on commit aa5e4ed

Please sign in to comment.