Skip to content

Commit

Permalink
[BUGFIX lts] Restores the shadowed property set behavior
Browse files Browse the repository at this point in the history
Restores the previous behavior that would happen when setting a shadowed
property.
  • Loading branch information
Chris Garrett committed Oct 13, 2020
1 parent ec49c70 commit 4cc6078
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
9 changes: 8 additions & 1 deletion packages/@ember/-internals/metal/lib/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meta, meta as metaFor, peekMeta } from '@ember/-internals/meta';
import { assert } from '@ember/debug';
import { _WeakSet } from '@ember/polyfills';

export type DecoratorPropertyDescriptor = PropertyDescriptor & { initializer?: any } | undefined;

Expand Down Expand Up @@ -79,11 +80,17 @@ function DESCRIPTOR_SETTER_FUNCTION(
name: string,
descriptor: ComputedDescriptor
): (value: any) => void {
return function CPSETTER_FUNCTION(this: object, value: any): void {
let set = function CPSETTER_FUNCTION(this: object, value: any): void {
return descriptor.set(this, name, value);
};

COMPUTED_SETTERS.add(set);

return set;
}

export const COMPUTED_SETTERS = new _WeakSet();

export function makeComputedDecorator(
desc: ComputedDescriptor,
DecoratorClass: { prototype: object }
Expand Down
10 changes: 5 additions & 5 deletions packages/@ember/-internals/metal/lib/property_set.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HAS_NATIVE_PROXY, setWithMandatorySetter, toString } from '@ember/-internals/utils';
import { HAS_NATIVE_PROXY, lookupDescriptor, setWithMandatorySetter, toString } from '@ember/-internals/utils';
import { assert } from '@ember/debug';
import EmberError from '@ember/error';
import { DEBUG } from '@glimmer/env';
import { descriptorForProperty } from './decorator';
import { COMPUTED_SETTERS } from './decorator';
import { isPath } from './path_cache';
import { notifyPropertyChange } from './property_events';
import { _getPath as getPath, getPossibleMandatoryProxyValue } from './property_get';
Expand Down Expand Up @@ -67,10 +67,10 @@ export function set(obj: object, keyName: string, value: any, tolerant?: boolean
return setPath(obj, keyName, value, tolerant);
}

let descriptor = descriptorForProperty(obj, keyName);
let descriptor = lookupDescriptor(obj, keyName);

if (descriptor !== undefined) {
descriptor.set(obj, keyName, value);
if (descriptor !== null && COMPUTED_SETTERS.has(descriptor.set!)) {
obj[keyName] = value;
return value;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/@ember/-internals/metal/lib/tracked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DEBUG } from '@glimmer/env';
import { consumeTag, dirtyTagFor, tagFor, trackedData } from '@glimmer/validator';
import { CHAIN_PASS_THROUGH } from './chain-tags';
import {
COMPUTED_SETTERS,
Decorator,
DecoratorPropertyDescriptor,
isElementDescriptor,
Expand Down Expand Up @@ -182,6 +183,8 @@ function descriptorForField([target, key, desc]: [
set,
};

COMPUTED_SETTERS.add(set);

metaFor(target).writeDescriptors(key, new TrackedDescriptor(get, set));

return newDesc;
Expand Down
16 changes: 16 additions & 0 deletions packages/@ember/-internals/metal/tests/tracked/set_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,21 @@ moduleFor(
assert.equal(get(newObj, key), obj[key], 'should set value');
}
}
['@test set should not throw an error when setting on shadowed properties'](assert) {
class Obj {
@tracked value = 'emberjs';
constructor() {
Object.defineProperty(this, 'value', { writable: true, value: 'emberjs' });
}
}
let newObj = new Obj();
set(newObj, 'value', 123);
assert.equal(newObj.value, 123, 'it worked');
}
}
);

0 comments on commit 4cc6078

Please sign in to comment.