Skip to content

Commit

Permalink
Simplify Path and Sexpr calculated flags
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Nov 27, 2014
1 parent 5c921ca commit e1cba43
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
23 changes: 2 additions & 21 deletions lib/handlebars/compiler/ast.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -111,8 +98,6 @@ var AST = {
} else if (part === '..') {
depth++;
depthString += '../';
} else {
this.isScoped = true;
}
} else {
dig.push(part);
Expand All @@ -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) {
Expand Down
40 changes: 36 additions & 4 deletions lib/handlebars/compiler/compiler.js
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
},

Expand Down Expand Up @@ -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));
}
},

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/handlebars.yy
Expand Up @@ -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
Expand Down

0 comments on commit e1cba43

Please sign in to comment.