Skip to content

Commit

Permalink
Fix AST parser for call expressions inside bracket expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
bmomberger-bitovi committed Jan 4, 2017
1 parent 3dc1ce5 commit 254a896
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/expression.js
Expand Up @@ -468,6 +468,9 @@ assign(Stack.prototype,{
},
popTo: function(types){
this.popUntil(types);
this.pop();
},
pop: function() {
if(!this.isRootTop()) {
this.stack.pop();
}
Expand Down Expand Up @@ -782,7 +785,7 @@ var expression = {
});
}
else if(firstParent.type === 'Bracket' && !(firstParent.children && firstParent.children.length > 0)) {
stack.addTo(["Bracket"], {type: "Lookup", key: token});
stack.addToAndPush(["Bracket"], {type: "Lookup", key: token});
}
// This is to make sure in a helper like `helper foo[bar] car` that
// car would not be added to the Bracket expression.
Expand All @@ -803,6 +806,7 @@ var expression = {
stack.addToAndPush(["Helper", "Call", "Hash"], {type: "Arg", key: token});
}
// Call
// foo[bar()]
else if(token === "(") {
top = stack.top();
if(top.type === "Lookup") {
Expand Down Expand Up @@ -841,6 +845,10 @@ var expression = {
stack.replaceTopAndPush({type: "Bracket"});
}
}
// End Bracket
else if(token === "]") {
stack.pop();
}
else if(token === " ") {
stack.push(token);
}
Expand Down
14 changes: 12 additions & 2 deletions test/expression-test.js
Expand Up @@ -353,6 +353,12 @@ test("expression.ast - [] operator", function(){
children: [{type: "Lookup", key: "bar"}]
});

deepEqual(expression.ast("foo[bar()]"), {
type: "Bracket",
root: {type: "Lookup", key: "foo"},
children: [{type: "Call", method: {key: "@bar", type: "Lookup" }}]
});

deepEqual(expression.ast("foo()[bar]"), {
type: "Bracket",
root: {type: "Call", method: {key: "@foo", type: "Lookup" } },
Expand Down Expand Up @@ -386,7 +392,9 @@ test("expression.ast - [] operator", function(){
type: "Literal",
value: "foo"
}]
});
},
"eq foo['bar'] 'foo' valid"
);

deepEqual(expression.ast("eq foo[bar] foo"), {
type: "Helper",
Expand All @@ -413,7 +421,9 @@ test("expression.ast - [] operator", function(){
children: [{type: "Lookup", key: "bar"}]
},
children: [{type: "Lookup", key: "baz"}]
});
},
"foo[bar][baz] valid"
);
});

test("expression.parse - [] operator", function(){
Expand Down

0 comments on commit 254a896

Please sign in to comment.