Simplify Path and Sexpr calculated flags

This commit is contained in:
kpdecker
2014-11-27 07:32:15 -06:00
parent 5c921cafeb
commit e1cba432a6
3 changed files with 39 additions and 26 deletions
+2 -21
View File
@@ -69,21 +69,8 @@ var AST = {
this.type = "sexpr";
this.hash = hash;
var id = this.id = rawParams[0];
var params = this.params = rawParams.slice(1);
// a mustache is definitely a helper if:
// * it is an eligible helper, and
// * it has at least one parameter or hash segment
this.isHelper = !!(params.length || hash);
// a mustache is an eligible helper if:
// * its id is simple (a single part, not `this` or `..`)
this.eligibleHelper = this.isHelper || id.isSimple;
// if a mustache is an eligible helper but not a definite
// helper, it is ambiguous, and will be resolved in a later
// pass or at runtime.
this.id = rawParams[0];
this.params = rawParams.slice(1);
},
HashNode: function(pairs, locInfo) {
@@ -111,8 +98,6 @@ var AST = {
} else if (part === '..') {
depth++;
depthString += '../';
} else {
this.isScoped = true;
}
} else {
dig.push(part);
@@ -123,10 +108,6 @@ var AST = {
this.original = (data ? '@' : '') + original;
this.parts = dig;
this.depth = depth;
// an ID is simple if it only has one part, and that part is not
// `..` or `this`.
this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
},
StringNode: function(string, locInfo) {
+36 -4
View File
@@ -3,6 +3,26 @@ import {isArray} from "../utils";
var slice = [].slice;
// a mustache is definitely a helper if:
// * it is an eligible helper, and
// * it has at least one parameter or hash segment
function helperExpr(sexpr) {
return !!(sexpr.isHelper || sexpr.params.length || sexpr.hash);
}
function scopedId(id) {
return (/^\.|this\b/).test(id.original);
}
// an ID is simple if it only has one part, and that part is not
// `..` or `this`.
function simpleId(id) {
var part = id.parts[0];
return id.parts.length === 1 && !scopedId(id) && !id.depth;
}
export function Compiler() {}
// the foundHelper register will disambiguate helper lookup from finding a
@@ -245,7 +265,7 @@ Compiler.prototype = {
id.falsy = true;
this.accept(id);
this.opcode('invokeHelper', sexpr, params.length, id.original, id.isSimple);
this.opcode('invokeHelper', sexpr, params.length, id.original, simpleId(id));
}
},
@@ -275,7 +295,7 @@ Compiler.prototype = {
this.options.data = true;
this.opcode('lookupData', id, id.depth, id.parts);
} else {
this.opcode('lookupOnContext', id, id.parts, id.falsy, id.isScoped);
this.opcode('lookupOnContext', id, id.parts, id.falsy, scopedId(id));
}
},
@@ -306,8 +326,15 @@ Compiler.prototype = {
},
classifySexpr: function(sexpr) {
var isHelper = sexpr.isHelper;
var isEligible = sexpr.eligibleHelper;
// a mustache is an eligible helper if:
// * its id is simple (a single part, not `this` or `..`)
var isHelper = helperExpr(sexpr);
// if a mustache is an eligible helper but not a definite
// helper, it is ambiguous, and will be resolved in a later
// pass or at runtime.
var isEligible = isHelper || simpleId(sexpr.id);
var options = this.options;
// if ambiguous, we can possibly resolve the ambiguity now
@@ -336,6 +363,11 @@ Compiler.prototype = {
pushParam: function(val) {
var value = val.value != null ? val.value : val.original || '';
// Force helper evaluation
if (val.type === 'sexpr') {
val.isHelper = true;
}
if (this.stringParams) {
if (value.replace) {
value = value
+1 -1
View File
@@ -92,7 +92,7 @@ param
| NUMBER -> new yy.NumberNode($1, yy.locInfo(@$))
| BOOLEAN -> new yy.BooleanNode($1, yy.locInfo(@$))
| dataName -> $1
| OPEN_SEXPR sexpr CLOSE_SEXPR {$2.isHelper = true; $$ = $2;}
| OPEN_SEXPR sexpr CLOSE_SEXPR -> $2
;
hash