Skip to content

Commit aa5e4ed

Browse files
wycatskategengler
authored andcommitted
[BUGFIX LTS] Don't run getters while applying mixins
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)
1 parent 7c84305 commit aa5e4ed

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import EmberObject from '@ember/object';
2+
import { moduleFor, RenderingTestCase } from 'internal-test-helpers';
3+
4+
moduleFor(
5+
'runtime: Mixin Accessors',
6+
class extends RenderingTestCase {
7+
['@test works with getters'](assert) {
8+
let value = 'building';
9+
10+
let Base = EmberObject.extend({
11+
get foo() {
12+
if (value === 'building') {
13+
throw Error('base should not be called yet');
14+
}
15+
16+
return "base's foo";
17+
},
18+
});
19+
20+
// force Base to be finalized so its properties will contain `foo`
21+
Base.proto();
22+
23+
class Child extends Base {
24+
get foo() {
25+
if (value === 'building') {
26+
throw Error('child should not be called yet');
27+
}
28+
29+
return "child's foo";
30+
}
31+
}
32+
33+
Child.proto();
34+
35+
let Grandchild = Child.extend({
36+
get foo() {
37+
if (value === 'building') {
38+
throw Error('grandchild should not be called yet');
39+
}
40+
41+
return value;
42+
},
43+
});
44+
45+
let instance = Grandchild.create();
46+
47+
value = 'done building';
48+
49+
assert.equal(instance.foo, 'done building', 'getter defined correctly');
50+
51+
value = 'changed value';
52+
53+
assert.equal(instance.foo, 'changed value', 'the value is a real getter, not a snapshot');
54+
}
55+
}
56+
);

packages/@ember/object/mixin.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import { guidFor, observerListenerMetaFor, ROOT, wrap } from '@ember/-internals/
88
import { assert } from '@ember/debug';
99
import { DEBUG } from '@glimmer/env';
1010
import { _WeakSet } from '@glimmer/util';
11-
import type {
12-
ComputedDecorator,
13-
ComputedPropertyGetter,
14-
ComputedPropertyObj,
15-
ComputedPropertySetter,
16-
ComputedDescriptor,
11+
import {
12+
type ComputedDecorator,
13+
type ComputedPropertyGetter,
14+
type ComputedPropertyObj,
15+
type ComputedPropertySetter,
16+
type ComputedDescriptor,
17+
isAccessor,
18+
isClassicDecorator,
1719
} from '@ember/-internals/metal';
1820
import {
1921
ComputedProperty,
@@ -235,7 +237,7 @@ function mergeMixins(
235237
keys: string[],
236238
keysWithSuper: string[]
237239
): void {
238-
let currentMixin;
240+
let currentMixin: MixinLike | undefined;
239241

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

311313
if (desc === undefined) {
312-
// The superclass did not have a CP, which means it may have
313-
// observers or listeners on that property.
314-
let prev = (values[key] = base[key]);
315-
316-
if (typeof prev === 'function') {
317-
updateObserversAndListeners(base, key, prev, false);
314+
// If the value is a classic decorator, we don't want to actually
315+
// access it, because that will execute the decorator while we're
316+
// building the class.
317+
if (!isClassicDecorator(value)) {
318+
// The superclass did not have a CP, which means it may have
319+
// observers or listeners on that property.
320+
let prev = (values[key] = base[key]);
321+
322+
if (typeof prev === 'function') {
323+
updateObserversAndListeners(base, key, prev, false);
324+
}
318325
}
319326
} else {
320327
descs[key] = desc;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"outDir": "dist",
55
"noEmit": true
66
},
7-
"include": ["packages/**/*.ts"],
7+
"include": ["packages/**/*.ts", "packages/**/*.js"],
88
"exclude": ["dist", "node_modules", "tmp", "types"]
99
}

0 commit comments

Comments
 (0)