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

cleanup _getPath #15692

Merged
merged 2 commits into from
Jun 8, 2018
Merged
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
16 changes: 1 addition & 15 deletions packages/ember-metal/lib/property_get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import { isPath } from './path_cache';
import { tagForProperty } from './tags';
import { getCurrentTracker } from './tracked';

const ALLOWABLE_TYPES = {
object: true,
function: true,
string: true,
};

export const PROXY_CONTENT = symbol('PROXY_CONTENT');

export let getPossibleMandatoryProxyValue: (obj: object, keyName: string) => any;
Expand Down Expand Up @@ -178,24 +172,16 @@ export function _getPath<T extends object>(root: T, path: string): any {
let parts = path.split('.');

for (let i = 0; i < parts.length; i++) {
if (!isGettable(obj)) {
if (obj === undefined || obj === null || (obj as MaybeHasIsDestroyed).isDestroyed) {
Copy link
Contributor Author

@bekzod bekzod Jun 8, 2018

Choose a reason for hiding this comment

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

maybe move (obj as MaybeHasIsDestroyed).isDestroyed to get itself ?

return undefined;
}

obj = get(obj, parts[i]);

if (obj && (obj as MaybeHasIsDestroyed).isDestroyed) {
return undefined;
}
}

return obj;
}

function isGettable(obj: any): boolean {
return obj !== undefined && obj !== null && ALLOWABLE_TYPES[typeof obj];
}

/**
Retrieves the value of a property from an Object, or a default value in the
case that the property returns `undefined`.
Expand Down
19 changes: 19 additions & 0 deletions packages/ember-metal/tests/accessors/get_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ENV } from 'ember-environment';
import { Object as EmberObject } from 'ember-runtime';
import { get, getWithDefault, Mixin, observer, computed } from '../..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { run } from '@ember/runloop';

function aget(x, y) {
return x[y];
Expand Down Expand Up @@ -98,6 +100,23 @@ moduleFor(
}
}

['@test get works with paths correctly'](assert) {
let func = function() {};
func.bar = 'awesome';

let destroyedObj = EmberObject.create({ bar: 'great' });
run(() => destroyedObj.destroy());

assert.equal(get({ foo: null }, 'foo.bar'), undefined);
assert.equal(get({ foo: { bar: 'hello' } }, 'foo.bar.length'), 5);
assert.equal(get({ foo: func }, 'foo.bar'), 'awesome');
assert.equal(get({ foo: func }, 'foo.bar.length'), 7);
assert.equal(get({}, 'foo.bar.length'), undefined);
assert.equal(get(function() {}, 'foo.bar.length'), undefined);
assert.equal(get('', 'foo.bar.length'), undefined);
assert.equal(get({ foo: destroyedObj }, 'foo.bar'), undefined);
}

['@test warn on attempts to call get with no arguments']() {
expectAssertion(function() {
get('aProperty');
Expand Down