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

[BUGFIX] Fix {{get}} when used on non-objects #1269

Merged
merged 1 commit into from
Feb 15, 2021
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
2 changes: 1 addition & 1 deletion packages/@glimmer/integration-tests/lib/modes/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ setGlobalContext({
let current: unknown = obj;

for (let part of parts) {
if (typeof current === 'function' || (typeof current === 'object' && current !== null)) {
if (current !== null && current !== undefined) {
current = (current as Record<string, unknown>)[part];
}
}
Expand Down
33 changes: 33 additions & 0 deletions packages/@glimmer/integration-tests/test/helpers/get-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,39 @@ class GetTest extends RenderTest {
this.rerender({ age: 30 });
this.assertHTML('miguelandrade30');
}

@test
'should be able to get string length with a static key'() {
this.render(`[{{get this.name 'length'}}] [{{if true (get this.name 'length')}}]`, {
name: 'Tomster',
});

this.assertHTML('[7] [7]');
this.assertStableRerender();

this.rerender({ name: 'Zoey' });
this.assertHTML('[4] [4]');

this.rerender({ name: 'Tomster' });
this.assertHTML('[7] [7]');
}

@test
'should be able to get string length with a bound/dynamic key'() {
this.render(`[{{get this.name this.key}}] [{{if true (get this.name this.key)}}]`, {
name: 'Tomster',
key: 'length',
});

this.assertHTML('[7] [7]');
this.assertStableRerender();

this.rerender({ key: 'foo' });
this.assertHTML('[] []');

this.rerender({ name: 'Zoey', key: 'length' });
this.assertHTML('[4] [4]');
}
}

jitSuite(GetTest);
9 changes: 3 additions & 6 deletions packages/@glimmer/runtime/lib/helpers/get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getPath, setPath } from '@glimmer/global-context';
import { VMArguments } from '@glimmer/interfaces';
import { createComputeRef, valueForRef } from '@glimmer/reference';
import { isDict } from '@glimmer/util';
import { internalHelper } from './internal-helper';

/**
Expand Down Expand Up @@ -88,21 +89,17 @@ export default internalHelper((args: VMArguments) => {
() => {
let source = valueForRef(sourceRef);

if (isObject(source)) {
if (isDict(source)) {
Copy link
Member

Choose a reason for hiding this comment

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

For the lazy, this is doing source !== null && source !== undefined

export function isDict<T>(u: T): u is Dict & T {
return u !== null && u !== undefined;
}

return getPath(source, String(valueForRef(pathRef)));
}
},
(value) => {
let source = valueForRef(sourceRef);

if (isObject(source)) {
if (isDict(source)) {
return setPath(source, String(valueForRef(pathRef)), value);
}
},
'get'
);
});

function isObject(obj: unknown): obj is object {
return typeof obj === 'function' || (typeof obj === 'object' && obj !== null);
}