Skip to content

Commit

Permalink
[FEATURE beta] Assert key in Ember.get is not empty if '' not `in
Browse files Browse the repository at this point in the history
obj`

This adds an assertion to let the user know when they are using an empty
string with an object that has no `''` property.

I'm keeping `Ember.get({'': 42}, '')` as a valid expression given it is
a tested behaviour.

Addresses [this
comment](emberjs#14572 (comment)) in emberjs#14572
  • Loading branch information
Serabe committed Nov 4, 2016
1 parent d5acb2d commit f26c32e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/ember-metal/lib/property_get.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function get(obj, keyName) {
assert(`Cannot call get with '${keyName}' on an undefined object.`, obj !== undefined && obj !== null);
assert(`The key provided to get must be a string, you passed ${keyName}`, typeof keyName === 'string');
assert(`'this' in paths is not supported`, !hasThis(keyName));
assert('Cannot call `Ember.get` with an empty string', keyName !== '' || '' in obj);

let value = obj[keyName];
let desc = (value !== null && typeof value === 'object' && value.isDescriptor) ? value : undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/ember-metal/tests/accessors/get_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ QUnit.test('warn on attempts to use get with an unsupported property path', func
expectAssertion(() => get(obj, undefined), /The key provided to get must be a string, you passed undefined/);
expectAssertion(() => get(obj, false), /The key provided to get must be a string, you passed false/);
expectAssertion(() => get(obj, 42), /The key provided to get must be a string, you passed 42/);
expectAssertion(() => get(obj, ''), /Cannot call `Ember.get` with an empty string/);
});

// ..........................................................
Expand Down

0 comments on commit f26c32e

Please sign in to comment.