Skip to content

Commit

Permalink
Merge pull request #17691 from emberjs/bugfix/ensure-tag-for-property…
Browse files Browse the repository at this point in the history
…-works-on-classes

[BUGFIX] Ensure tagForProperty works on class constructors
  • Loading branch information
rwjblue committed Mar 3, 2019
2 parents fd98f46 + 479a5d4 commit c707dc7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/@ember/-internals/metal/lib/decorator.ts
Expand Up @@ -22,8 +22,9 @@ export function isElementDescriptor(
return (
// Ensure we have the right number of args
args.length === 3 &&
// Make sure the target is an object
(typeof maybeTarget === 'object' && maybeTarget !== null) &&
// Make sure the target is a class or object (prototype)
(typeof maybeTarget === 'function' ||
(typeof maybeTarget === 'object' && maybeTarget !== null)) &&
// Make sure the key is a string
typeof maybeKey === 'string' &&
// Make sure the descriptor is the right shape
Expand Down
3 changes: 2 additions & 1 deletion packages/@ember/-internals/metal/lib/tags.ts
Expand Up @@ -22,7 +22,8 @@ function makeTag(): TagWrapper<DirtyableTag> {
}

export function tagForProperty(object: any, propertyKey: string | symbol, _meta?: Meta): Tag {
if (typeof object !== 'object' || object === null) {
let objectType = typeof object;
if (objectType !== 'function' && (objectType !== 'object' || object === null)) {
return CONSTANT_TAG;
}
let meta = _meta === undefined ? metaFor(object) : _meta;
Expand Down
19 changes: 19 additions & 0 deletions packages/@ember/-internals/metal/tests/computed_decorator_test.js
Expand Up @@ -70,6 +70,25 @@ if (EMBER_NATIVE_DECORATOR_SUPPORT) {
get(obj, 'fullName');
}

['@test computed property can be defined and accessed on a class constructor'](assert) {
let count = 0;

class Obj {
static bar = 123;

@computed
static get foo() {
count++;
return this.bar;
}
}

assert.equal(Obj.foo, 123, 'should return value');
Obj.foo;

assert.equal(count, 1, 'should only call getter once');
}

['@test it works with computed desc'](assert) {
assert.expect(4);

Expand Down
19 changes: 19 additions & 0 deletions packages/@ember/-internals/metal/tests/computed_test.js
Expand Up @@ -82,6 +82,25 @@ moduleFor(
assert.equal(count, 1, 'should have invoked computed property');
}

['@test computed property can be defined and accessed on a class constructor'](assert) {
let count = 0;

let Obj = EmberObject.extend();
Obj.reopenClass({
bar: 123,

foo: computed(function() {
count++;
return this.bar;
}),
});

assert.equal(Obj.foo, 123, 'should return value');
Obj.foo;

assert.equal(count, 1, 'should only call getter once');
}

['@test can override volatile computed property'](assert) {
let obj = {};

Expand Down

0 comments on commit c707dc7

Please sign in to comment.