Skip to content

Commit

Permalink
Merge pull request #146 from canjs/bracket-call-fix
Browse files Browse the repository at this point in the history
Fix for Call expressions inside of Bracket expressions
  • Loading branch information
justinbmeyer committed Jan 4, 2017
2 parents 3dc1ce5 + 083e2f4 commit cd6ad68
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 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
75 changes: 66 additions & 9 deletions test/expression-test.js
Expand Up @@ -334,30 +334,36 @@ test("expression.ast - [] operator", function(){
deepEqual(expression.ast("['propName']"), {
type: "Bracket",
children: [{type: "Literal", value: "propName"}]
});
}, "['propName'] valid");

deepEqual(expression.ast("[propName]"), {
type: "Bracket",
children: [{type: "Lookup", key: "propName"}]
});
}, "[propName] valid");

deepEqual(expression.ast("foo['bar']"), {
type: "Bracket",
root: {type: "Lookup", key: "foo"},
children: [{type: "Literal", value: "bar"}]
});
}, "foo['bar'] valid");

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

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

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

deepEqual(expression.ast("foo [bar]"), {
type: "Helper",
Expand All @@ -369,7 +375,7 @@ test("expression.ast - [] operator", function(){
type: "Bracket",
children: [{type: "Lookup", key: "bar"}]
}]
});
}, "foo [bar] valid");

deepEqual(expression.ast("eq foo['bar'] 'foo'"), {
type: "Helper",
Expand All @@ -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 @@ -403,7 +411,7 @@ test("expression.ast - [] operator", function(){
type: "Lookup",
key: "foo"
}]
});
}, "eq foo[bar] foo valid");

deepEqual(expression.ast("foo[bar][baz]"), {
type: "Bracket",
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 Expand Up @@ -458,6 +468,34 @@ test("expression.parse - [] operator", function(){
)
)
);

exprData = expression.parse("foo[bar()]");
deepEqual(exprData,
new expression.Bracket(
new expression.Call(
new expression.Lookup('@bar'),
[],
{}
),
new expression.Lookup('foo')
)
);

exprData = expression.parse("foo()[bar()]");
deepEqual(exprData,
new expression.Bracket(
new expression.Call(
new expression.Lookup("@bar"),
[],
{}
),
new expression.Call(
new expression.Lookup("@foo"),
[],
{}
)
)
);
});

test("Bracket expression", function(){
Expand Down Expand Up @@ -526,6 +564,25 @@ test("Bracket expression", function(){
);
equal(compute(), "Kevin");

// foo[bar()]
expr = new expression.Bracket(
new expression.Call(
new expression.Lookup('@bar'),
[],
{}
),
new expression.Lookup('foo')
);
compute = expr.value(
new Scope(
new CanMap({
foo: {name: "Kevin"},
bar: function () { return "name"; }
})
)
);
equal(compute(), "Kevin");

// foo()[bar()]
expr = new expression.Bracket(
new expression.Call(
Expand Down

0 comments on commit cd6ad68

Please sign in to comment.