Skip to content
Open
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
6 changes: 6 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export function preparePath(data, sexpr, parts, loc) {
let tail = [];
let depth = 0;

// Check if the first path segment is a number, which should throw an error
if (parts.length > 0 && !data && !sexpr &&
!isNaN(parseFloat(parts[0].part)) && isFinite(parts[0].part)) {
throw new Exception('Invalid path: ' + parts.map(p => (p.separator ?? '') + p.part).join(''), { loc });
}

for (let i = 0, l = parts.length; i < l; i++) {
let part = parts[i].part;
// If we have [] syntax then we do not treat path references as operators,
Expand Down
10 changes: 10 additions & 0 deletions spec/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ describe('parser', function () {
equalsAst('{{this/foo}}', '{{ p%foo }}');
equalsAst('{{this.foo}}', '{{ p%this.foo }}');
equalsAst('{{this.#foo}}', '{{ p%this.#foo }}');
equalsAst('{{this.0.foo}}', '{{ p%this.0/foo }}');
equalsAst('{{this.foo.0}}', '{{ p%this.foo/0 }}');
equalsAst('{{this.0}}', '{{ p%this.0 }}');
shouldThrow(
function () {
astFor('{{0.foo}}');
},
Error,
/Invalid path: 0.foo/
);
});

it('parses mustaches with - in a path', function () {
Expand Down
2 changes: 1 addition & 1 deletion src/handlebars.yy
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,6 @@ path
;

pathSegments
: pathSegments sep ID { $1.push({part: yy.id($3), original: $3, separator: $2}); $$ = $1; }
: pathSegments sep (ID | NUMBER) { $1.push({part: yy.id($3), original: $3, separator: $2}); $$ = $1; }
| ID -> [{part: yy.id($1), original: $1}]
;