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

fix: add check for undefined/null before calling Object.keys #185

Merged
merged 1 commit into from
Jul 14, 2022
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## Unreleased
- bugfix for edge case affecting `get`, `getObject`, and `option`

## 5.0.5
- setup to replace `get` & `getObject` helpers ([#183](https://github.com/bigcommerce/paper-handlebars/pull/183))
## 5.0.4
Expand Down
3 changes: 3 additions & 0 deletions helpers/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function getValue(globals, object, path) {
let result = object;
let prefix = '';
for (let key of parts) {
if (result === undefined || result === null) {
return undefined;
}
// preserve handling of trailing backslashes for backwards compatibility
if (key.slice(-1) === '\\') {
prefix = prefix + key.slice(0, -1) + '.';
Expand Down
41 changes: 26 additions & 15 deletions spec/helpers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ describe('get helper', function () {
], done);
});

// uncomment when 3rd-party version is replaced
// it('does not access prototype properties', (done) => {
// context.__proto__ = {x: 'yz'};
// runTestCases([
// {
// input: `{{get "x" this}}`,
// output: ``,
// }
// ], done);
// });
it('does not access prototype properties', (done) => {
context.__proto__ = {x: 'yz'};
runTestCases([
{
input: `{{get "x" this}}`,
output: ``,
}
], done);
});

it('accepts SafeString paths', (done) => {
runTestCases([
Expand All @@ -44,11 +43,10 @@ describe('get helper', function () {
input: `{{get (concat 'a' 'b') this}}`,
output: `b`,
},
// uncomment when 3rd-party version is replaced
// {
// input: `{{get (concat 'options.a' '.b.c') this}}`,
// output: `d`,
// }
{
input: `{{get (concat 'options.a' '.b.c') this}}`,
output: `d`,
}
], done);
});

Expand Down Expand Up @@ -90,4 +88,17 @@ describe('get helper', function () {
}
], done);
});

it('returns undefined if prop path does not exist', (done) => {
runTestCases([
{ // a key does not exist
input: `{{get 'z.z' options}}`,
output: ``,
},
{ // first key exists, but its value is `undefined`
input:`{{get '0.zyx' (pluck (arrayify options) 'b')}}`, // array: [ undefined ]
output: ``,
}
], done);
});
});
2 changes: 2 additions & 0 deletions spec/helpers/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ describe('common utils', function () {
expect(getValue(globals, obj, 'a.c.23')).to.equal(undefined);
expect(getValue(globals, obj, 'ab')).to.equal(undefined);
expect(getValue(globals, obj, 'nonexistent')).to.equal(undefined);
expect(getValue(globals, [ undefined ], '0.x')).to.equal(undefined);
expect(getValue(globals, [ null ], '0.x')).to.equal(undefined);
done();
});

Expand Down