Update MustacheNode for new AST structure

This commit is contained in:
kpdecker
2014-11-26 09:31:46 -06:00
parent df421f1e4b
commit 5cfe6a0be1
6 changed files with 21 additions and 37 deletions
+5 -16
View File
@@ -10,10 +10,11 @@ var AST = {
this.strip = strip;
},
MustacheNode: function(rawParams, hash, open, strip, locInfo) {
MustacheNode: function(rawParams, open, strip, locInfo) {
this.loc = locInfo;
this.type = "mustache";
this.strip = strip;
this.type = 'MustacheStatement';
this.sexpr = rawParams;
// Open may be a string parsed from the parser or a passed boolean flag
if (open != null && open.charAt) {
@@ -24,19 +25,7 @@ var AST = {
this.escaped = !!open;
}
if (rawParams instanceof AST.SexprNode) {
this.sexpr = rawParams;
} else {
// Support old AST API
this.sexpr = new AST.SexprNode(rawParams, hash);
}
// Support old AST API that stored this info in MustacheNode
this.id = this.sexpr.id;
this.params = this.sexpr.params;
this.hash = this.sexpr.hash;
this.eligibleHelper = this.sexpr.eligibleHelper;
this.isHelper = this.sexpr.isHelper;
this.strip = strip;
},
SexprNode: function(rawParams, hash, locInfo) {
+1 -1
View File
@@ -193,7 +193,7 @@ Compiler.prototype = {
}
},
mustache: function(mustache) {
MustacheStatement: function(mustache) {
this.sexpr(mustache.sexpr);
if(mustache.escaped && !this.options.noEscape) {
+4 -4
View File
@@ -44,6 +44,10 @@ PrintVisitor.prototype.Program = function(program) {
return out;
};
PrintVisitor.prototype.MustacheStatement = function(mustache) {
return this.pad('{{ ' + this.accept(mustache.sexpr) + ' }}');
};
PrintVisitor.prototype.block = function(block) {
var out = "";
@@ -83,10 +87,6 @@ PrintVisitor.prototype.sexpr = function(sexpr) {
return this.accept(sexpr.id) + " " + params + hash;
};
PrintVisitor.prototype.mustache = function(mustache) {
return this.pad("{{ " + this.accept(mustache.sexpr) + " }}");
};
PrintVisitor.prototype.partial = function(partial) {
var content = this.accept(partial.partialName);
if(partial.context) {
+4 -4
View File
@@ -16,16 +16,16 @@ Visitor.prototype = {
}
},
MustacheStatement: function(mustache) {
this.accept(mustache.sexpr);
},
block: function(block) {
this.accept(block.mustache);
this.accept(block.program);
this.accept(block.inverse);
},
mustache: function(mustache) {
this.accept(mustache.sexpr);
},
sexpr: function(sexpr) {
var params = sexpr.params, paramStrings = [], hash;
+4 -9
View File
@@ -24,21 +24,16 @@ describe('ast', function() {
describe('MustacheNode', function() {
function testEscape(open, expected) {
var mustache = new handlebarsEnv.AST.MustacheNode([{}], {}, open, false);
var mustache = new handlebarsEnv.AST.MustacheNode([{}], open, false);
equals(mustache.escaped, expected);
}
it('should store args', function() {
var id = {isSimple: true},
hash = {},
mustache = new handlebarsEnv.AST.MustacheNode([id, 'param1'], hash, '', false, LOCATION_INFO);
equals(mustache.type, 'mustache');
equals(mustache.hash, hash);
mustache = new handlebarsEnv.AST.MustacheNode([id, 'param1'], '', false, LOCATION_INFO);
equals(mustache.type, 'MustacheStatement');
equals(mustache.escaped, true);
equals(mustache.id, id);
equals(mustache.params.length, 1);
equals(mustache.params[0], 'param1');
equals(!!mustache.isHelper, true);
testLocationInfoStorage(mustache);
});
it('should accept token for escape', function() {
@@ -280,7 +275,7 @@ describe('ast', function() {
block = ast.body[0];
equals(block.program.body[0].string, '');
equals(block.program.body[1].id.original, 'foo');
equals(block.program.body[1].sexpr.id.original, 'foo');
equals(block.program.body[2].string, '\n');
});
it('marks nested block mustaches as standalone', function() {
+3 -3
View File
@@ -47,7 +47,7 @@ openInverse
;
openInverseChain
: OPEN_INVERSE_CHAIN sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), yy.locInfo(@$))
: OPEN_INVERSE_CHAIN sexpr CLOSE -> new yy.MustacheNode($2, $1, yy.stripFlags($1, $3), yy.locInfo(@$))
;
inverseAndProgram
@@ -73,8 +73,8 @@ closeBlock
mustache
// Parsing out the '&' escape token at AST level saves ~500 bytes after min due to the removal of one parser node.
// This also allows for handler unification as all mustache node instances can utilize the same handler
: OPEN sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), yy.locInfo(@$))
| OPEN_UNESCAPED sexpr CLOSE_UNESCAPED -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), yy.locInfo(@$))
: OPEN sexpr CLOSE -> new yy.MustacheNode($2, $1, yy.stripFlags($1, $3), yy.locInfo(@$))
| OPEN_UNESCAPED sexpr CLOSE_UNESCAPED -> new yy.MustacheNode($2, $1, yy.stripFlags($1, $3), yy.locInfo(@$))
;
partial