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

Handle nested call functions #683

Merged
merged 2 commits into from
Apr 25, 2019
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
9 changes: 9 additions & 0 deletions src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,20 @@ var expression = {
// foo[bar()]
else if(token === "(") {
top = stack.top();
lastToken = stack.topLastChild();
if(top.type === "Lookup") {
stack.replaceTopAndPush({
type: "Call",
method: convertToAtLookup(top)
});

// Nested Call
// foo()()
} else if (lastToken && lastToken.type === "Call") {
stack.replaceTopAndPush({
type: "Call",
method: lastToken
});
} else {
throw new Error("Unable to understand expression "+tokens.join(''));
}
Expand Down
82 changes: 82 additions & 0 deletions test/expression-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ test("expression.ast - root hash expressions work", function(){

});

test("expression.ast - nested call expressions", function(){
var ast = expression.ast("foo()()");

deepEqual(ast, {
type: "Call",
method: {
type: "Call",
method: {type: "Lookup", key: "@foo"},
}
});

});

test("expression.ast - everything", function(){
var ast = expression.ast("helperA helperB(1, valueA, propA=~valueB propC=2, 1).zed() 'def' nested@prop outerPropA=helperC(2,valueB)");
Expand Down Expand Up @@ -223,6 +234,77 @@ test("expression.parse(str, {lookupRule: 'method', methodRule: 'call'})",
deepEqual(exprData.argExprs[0], hashArg, "correct hashes");
});

test("expression.parse nested Call expressions", function(){
QUnit.expect(7);

deepEqual(expression.parse("foo()()"),
new expression.Call(
new expression.Call(
new expression.Lookup('@foo'),
[],
{}
),
[],
{}
),
"Returned the correct expression"
);

var expr = new expression.Call(
new expression.Call(
new expression.Lookup('@bar'),
[ new expression.Literal(1) ],
{}
),
[ new expression.Literal(2) ],
{}
);
var compute = expr.value(
new Scope(
new SimpleMap({
bar: function(outter) {
equal(outter, 1, "Outter called with correct value");

return function (inner) {
equal(inner, 2, "Inner called with correct value");

return 'Inner!';
};
}
})
)
);
equal(compute.get(), "Inner!", "Got the inner value");

expr = new expression.Call(
new expression.Call(
new expression.Lookup('@foobar'),
[ new expression.Lookup('fname') ],
{}
),
[ new expression.Lookup('lname') ],
{}
);
compute = expr.value(
new Scope(
new SimpleMap({
foobar: function(outter) {
equal(outter, 'Matt', "Outter called with correct value");

return function (inner) {
equal(inner, 'Chaffe', "Inner called with correct value");

return 'Inner!';
};
},
fname: 'Matt',
lname: 'Chaffe'
})
)
);
equal(compute.get(), "Inner!", "Got the inner value");
});

test("numeric expression.Literal", function(){
var exprData = expression.parse("3");

Expand Down
22 changes: 22 additions & 0 deletions test/stache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6596,6 +6596,28 @@ function makeTest(name, doc, mutation) {
map.bar = true;
QUnit.equal(innerHTML(div), "and or");
});

QUnit.test("{{foo()()}} nested call expressions", function(){
QUnit.expect(3);

var div = doc.createElement('div');
var data = {
foo: function(outter) {
QUnit.equal(outter, 1, 'foo should be called with 1');
return function (inner) {
QUnit.equal(inner, 2, 'inner should be called with 2');
return inner;
};
}
};
var template = stache("{{foo(1)(2)}}");
var frag = template(data);

div.appendChild(frag);

QUnit.equal(innerHTML(div), 2, 'should return inner value');
});

// PUT NEW TESTS RIGHT BEFORE THIS!

}