diff options
author | kpdecker <kpdecker@gmail.com> | 2014-11-27 07:32:15 -0600 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2014-11-27 07:32:15 -0600 |
commit | e1cba432a68fe208782f8f943ebd57d7641d705a (patch) | |
tree | b7fc96a969a0290f953c38ed3a61053da35861e4 /lib/handlebars/compiler/compiler.js | |
parent | 5c921cafebee438fa27d417ae701b24323373a30 (diff) | |
download | handlebars.js-e1cba432a68fe208782f8f943ebd57d7641d705a.zip handlebars.js-e1cba432a68fe208782f8f943ebd57d7641d705a.tar.gz handlebars.js-e1cba432a68fe208782f8f943ebd57d7641d705a.tar.bz2 |
Simplify Path and Sexpr calculated flags
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index ea65583..5dbbfc4 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -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 |