Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add an assertion when watched props are are written to without Em.set. #9395

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/ember-metal/lib/property_get.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ var get = function get(obj, keyName) {
if (Ember.FEATURES.isEnabled('mandatory-setter')) {
if (hasPropertyAccessors && meta && meta.watching[keyName] > 0) {
ret = meta.values[keyName];

if (ret === undefined && keyName in obj && !(keyName in meta.values)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be fine, but we should confirm no perf issue is caused by this in.

If there is we can always be clever and use one the sentinel UNDEFINED technique

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'll look into that.

Also, this whole section is completely removed from prod builds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok

Ember.assert('You must use Ember.set to set `' + keyName + '` (on `' + obj + '`).');
}
} else {
ret = obj[keyName];
}
Expand Down
30 changes: 30 additions & 0 deletions packages/ember-metal/tests/accessors/mandatory_setters_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ if (Ember.FEATURES.isEnabled('mandatory-setter')) {
}, 'You must use Ember.set() to set the `someProp` property (of custom-object) to `foo-bar`.');
});

test('should assert if get a watched property (not on the prototype) that is not set by Ember.set', function() {
var obj = {
toString: function() {
return 'custom-object';
}
};

watch(obj, 'someProp');

obj.someProp = 'foo-bar';

expectAssertion(function() {
get(obj, 'someProp');
}, 'You must use Ember.set to set `someProp` (on `custom-object`).');
});

test('should not assert if set with Ember.set when property is being watched', function() {
var obj = {
someProp: null,
Expand All @@ -45,6 +61,20 @@ if (Ember.FEATURES.isEnabled('mandatory-setter')) {

equal(get(obj, 'someProp'), 'foo-bar');
});

test('prints a helpful assertion if a watched property (not present at watch time) was not set with Ember.set', function() {
var obj = {
toString: function() {
return 'custom-object';
}
};

watch(obj, 'someProp');
obj.someProp = 'blastix';

equal(get(obj, 'someProp'), 'blastix');
});

} else {
test('does not assert', function() {
var obj = {
Expand Down