Skip to content

Commit

Permalink
[FEATURE beta] Assert key in Ember.get is not empty
Browse files Browse the repository at this point in the history
This adds an assertion to let the user know when they are using an empty
string as the key in `Ember.get`.

Addresses [this
comment](emberjs#14572 (comment)) in emberjs#14572
  • Loading branch information
Serabe committed Nov 4, 2016
1 parent d5acb2d commit d03ff1f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 8 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 !== '');

let value = obj[keyName];
let desc = (value !== null && typeof value === 'object' && value.isDescriptor) ? value : undefined;
Expand Down
9 changes: 1 addition & 8 deletions packages/ember-metal/tests/accessors/get_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ QUnit.test('should not access a property more than once', function() {
equal(count, 1);
});

QUnit.test('should be able to use an empty string as a property', function(assert) {
let obj = { '': 'empty string' };

let result = get(obj, '');

assert.equal(result, obj['']);
});

testBoth('should call unknownProperty on watched values if the value is undefined', function(get, set) {
let obj = {
count: 0,
Expand Down Expand Up @@ -114,6 +106,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 d03ff1f

Please sign in to comment.