Handle all potential literal values
Adds support for literal helper names in a few missing cases such as block expressions and subexpressions.
This commit is contained in:
@@ -110,6 +110,8 @@ Compiler.prototype = {
|
||||
},
|
||||
|
||||
BlockStatement: function(block) {
|
||||
transformLiteralToPath(block);
|
||||
|
||||
var program = block.program,
|
||||
inverse = block.inverse;
|
||||
|
||||
@@ -172,7 +174,6 @@ Compiler.prototype = {
|
||||
},
|
||||
|
||||
MustacheStatement: function(mustache) {
|
||||
transformLiteralToPath(mustache);
|
||||
this.SubExpression(mustache);
|
||||
|
||||
if(mustache.escaped && !this.options.noEscape) {
|
||||
@@ -191,6 +192,7 @@ Compiler.prototype = {
|
||||
CommentStatement: function() {},
|
||||
|
||||
SubExpression: function(sexpr) {
|
||||
transformLiteralToPath(sexpr);
|
||||
var type = this.classifySexpr(sexpr);
|
||||
|
||||
if (type === 'simple') {
|
||||
@@ -487,9 +489,11 @@ function argEquals(a, b) {
|
||||
}
|
||||
}
|
||||
|
||||
function transformLiteralToPath(mustache) {
|
||||
if (mustache.path.type.match(/Literal$/)) {
|
||||
var literal = mustache.path;
|
||||
mustache.path = { type: 'PathExpression', original: String(literal.original), parts: [String(literal.original)], depth: 0, data: false };
|
||||
function transformLiteralToPath(sexpr) {
|
||||
if (!sexpr.path.parts) {
|
||||
var literal = sexpr.path;
|
||||
// Casting to string here to make false and 0 literal values play nicely with the rest
|
||||
// of the system.
|
||||
sexpr.path = new AST.PathExpression(false, 0, [literal.original+''], literal.original+'', literal.log);
|
||||
}
|
||||
}
|
||||
|
||||
+23
-12
@@ -230,21 +230,32 @@ describe("basic context", function() {
|
||||
}, Error);
|
||||
});
|
||||
|
||||
it("pass string literals", function() {
|
||||
shouldCompileTo('{{"foo"}}', {}, "");
|
||||
shouldCompileTo('{{"foo"}}', { foo: "bar" }, "bar");
|
||||
it('pass string literals', function() {
|
||||
shouldCompileTo('{{"foo"}}', {}, '');
|
||||
shouldCompileTo('{{"foo"}}', { foo: 'bar' }, 'bar');
|
||||
shouldCompileTo('{{#"foo"}}{{.}}{{/"foo"}}', { foo: ['bar', 'baz'] }, 'barbaz');
|
||||
});
|
||||
|
||||
it("pass number literals", function() {
|
||||
shouldCompileTo("{{12}}", {}, "");
|
||||
shouldCompileTo("{{12}}", { "12": "bar" }, "bar");
|
||||
shouldCompileTo("{{12.34}}", {}, "");
|
||||
shouldCompileTo("{{12.34}}", { "12.34": "bar" }, "bar");
|
||||
it('pass number literals', function() {
|
||||
shouldCompileTo('{{12}}', {}, '');
|
||||
shouldCompileTo('{{12}}', { '12': 'bar' }, 'bar');
|
||||
shouldCompileTo('{{12.34}}', {}, '');
|
||||
shouldCompileTo('{{12.34}}', { '12.34': 'bar' }, 'bar');
|
||||
shouldCompileTo('{{12.34 1}}', { '12.34': function(arg) { return 'bar' + arg; } }, 'bar1');
|
||||
});
|
||||
|
||||
it("pass boolean literals", function() {
|
||||
shouldCompileTo("{{true}}", {}, "");
|
||||
shouldCompileTo("{{true}}", { "": "foo" }, "");
|
||||
shouldCompileTo("{{false}}", { "false": "foo" }, "foo");
|
||||
it('pass boolean literals', function() {
|
||||
shouldCompileTo('{{true}}', {}, '');
|
||||
shouldCompileTo('{{true}}', { '': 'foo' }, '');
|
||||
shouldCompileTo('{{false}}', { 'false': 'foo' }, 'foo');
|
||||
});
|
||||
|
||||
it('should handle literals in subexpression', function() {
|
||||
var helpers = {
|
||||
foo: function(arg) {
|
||||
return arg;
|
||||
}
|
||||
};
|
||||
shouldCompileTo('{{foo (false)}}', [{ 'false': function() { return 'bar'; } }, helpers], 'bar');
|
||||
});
|
||||
});
|
||||
|
||||
+3
-7
@@ -66,7 +66,7 @@ inverseChain
|
||||
;
|
||||
|
||||
closeBlock
|
||||
: OPEN_ENDBLOCK path CLOSE -> {path: $2, strip: yy.stripFlags($1, $3)}
|
||||
: OPEN_ENDBLOCK helperName CLOSE -> {path: $2, strip: yy.stripFlags($1, $3)}
|
||||
;
|
||||
|
||||
mustache
|
||||
@@ -81,11 +81,7 @@ partial
|
||||
;
|
||||
|
||||
param
|
||||
: path -> $1
|
||||
| STRING -> new yy.StringLiteral($1, yy.locInfo(@$))
|
||||
| NUMBER -> new yy.NumberLiteral($1, yy.locInfo(@$))
|
||||
| BOOLEAN -> new yy.BooleanLiteral($1, yy.locInfo(@$))
|
||||
| dataName -> $1
|
||||
: helperName -> $1
|
||||
| sexpr -> $1
|
||||
;
|
||||
|
||||
@@ -108,7 +104,7 @@ blockParams
|
||||
helperName
|
||||
: path -> $1
|
||||
| dataName -> $1
|
||||
| STRING -> new yy.StringLiteral($1, yy.locInfo(@$)), yy.locInfo(@$)
|
||||
| STRING -> new yy.StringLiteral($1, yy.locInfo(@$))
|
||||
| NUMBER -> new yy.NumberLiteral($1, yy.locInfo(@$))
|
||||
| BOOLEAN -> new yy.BooleanLiteral($1, yy.locInfo(@$))
|
||||
;
|
||||
|
||||
Reference in New Issue
Block a user