Expose AST helpers in public API
This commit is contained in:
@@ -104,6 +104,27 @@ var AST = {
|
||||
this.type = 'HashPair';
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
},
|
||||
|
||||
// Public API used to evaluate derived attributes regarding AST nodes
|
||||
helpers: {
|
||||
// a mustache is definitely a helper if:
|
||||
// * it is an eligible helper, and
|
||||
// * it has at least one parameter or hash segment
|
||||
// TODO: Make these public utility methods
|
||||
helperExpression: function(sexpr) {
|
||||
return !!(sexpr.isHelper || sexpr.params.length || sexpr.hash);
|
||||
},
|
||||
|
||||
scopedId: function(path) {
|
||||
return (/^\.|this\b/).test(path.original);
|
||||
},
|
||||
|
||||
// an ID is simple if it only has one part, and that part is not
|
||||
// `..` or `this`.
|
||||
simpleId: function(path) {
|
||||
return path.parts.length === 1 && !AST.helpers.scopedId(path) && !path.depth;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,26 +1,10 @@
|
||||
import Exception from "../exception";
|
||||
import {isArray} from "../utils";
|
||||
import AST from "./ast";
|
||||
|
||||
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(path) {
|
||||
return (/^\.|this\b/).test(path.original);
|
||||
}
|
||||
|
||||
// an ID is simple if it only has one part, and that part is not
|
||||
// `..` or `this`.
|
||||
function simpleId(path) {
|
||||
return path.parts.length === 1 && !scopedId(path) && !path.depth;
|
||||
}
|
||||
|
||||
export function Compiler() {}
|
||||
|
||||
// the foundHelper register will disambiguate helper lookup from finding a
|
||||
@@ -239,7 +223,7 @@ Compiler.prototype = {
|
||||
path.falsy = true;
|
||||
|
||||
this.accept(path);
|
||||
this.opcode('invokeHelper', params.length, path.original, simpleId(path));
|
||||
this.opcode('invokeHelper', params.length, path.original, AST.helpers.simpleId(path));
|
||||
}
|
||||
},
|
||||
|
||||
@@ -255,7 +239,7 @@ Compiler.prototype = {
|
||||
this.options.data = true;
|
||||
this.opcode('lookupData', path.depth, path.parts);
|
||||
} else {
|
||||
this.opcode('lookupOnContext', path.parts, path.falsy, scopedId(path));
|
||||
this.opcode('lookupOnContext', path.parts, path.falsy, AST.helpers.scopedId(path));
|
||||
}
|
||||
},
|
||||
|
||||
@@ -301,12 +285,12 @@ Compiler.prototype = {
|
||||
classifySexpr: function(sexpr) {
|
||||
// a mustache is an eligible helper if:
|
||||
// * its id is simple (a single part, not `this` or `..`)
|
||||
var isHelper = helperExpr(sexpr);
|
||||
var isHelper = AST.helpers.helperExpression(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.path);
|
||||
var isEligible = isHelper || AST.helpers.simpleId(sexpr.path);
|
||||
|
||||
var options = this.options;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user