summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'lib/handlebars/compiler')
-rw-r--r--lib/handlebars/compiler/ast.js9
-rw-r--r--lib/handlebars/compiler/base.js10
-rw-r--r--lib/handlebars/compiler/code-gen.js9
-rw-r--r--lib/handlebars/compiler/compiler.js54
-rw-r--r--lib/handlebars/compiler/helpers.js6
-rw-r--r--lib/handlebars/compiler/javascript-compiler.js88
-rw-r--r--lib/handlebars/compiler/printer.js34
-rw-r--r--lib/handlebars/compiler/visitor.js4
-rw-r--r--lib/handlebars/compiler/whitespace-control.js23
9 files changed, 123 insertions, 114 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js
index e5904c6..1ecf59b 100644
--- a/lib/handlebars/compiler/ast.js
+++ b/lib/handlebars/compiler/ast.js
@@ -27,8 +27,8 @@ var AST = {
this.path = path;
this.params = params || [];
this.hash = hash;
- this.program = program;
- this.inverse = inverse;
+ this.program = program;
+ this.inverse = inverse;
this.openStrip = openStrip;
this.inverseStrip = inverseStrip;
@@ -76,8 +76,8 @@ var AST = {
this.data = data;
this.original = original;
- this.parts = parts;
- this.depth = depth;
+ this.parts = parts;
+ this.depth = depth;
},
StringLiteral: function(string, locInfo) {
@@ -130,7 +130,6 @@ var AST = {
// 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(node) {
return !!(node.type === 'SubExpression' || node.params.length || node.hash);
},
diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js
index 167b844..02e174f 100644
--- a/lib/handlebars/compiler/base.js
+++ b/lib/handlebars/compiler/base.js
@@ -1,8 +1,8 @@
-import parser from "./parser";
-import AST from "./ast";
-import WhitespaceControl from "./whitespace-control";
-module Helpers from "./helpers";
-import { extend } from "../utils";
+import parser from './parser';
+import AST from './ast';
+import WhitespaceControl from './whitespace-control';
+import * as Helpers from './helpers';
+import { extend } from '../utils';
export { parser };
diff --git a/lib/handlebars/compiler/code-gen.js b/lib/handlebars/compiler/code-gen.js
index 92020f0..0c40e00 100644
--- a/lib/handlebars/compiler/code-gen.js
+++ b/lib/handlebars/compiler/code-gen.js
@@ -1,4 +1,5 @@
-import {isArray} from "../utils";
+/*global define */
+import {isArray} from '../utils';
var SourceNode;
@@ -56,7 +57,7 @@ function castChunk(chunk, codeGen, loc) {
return ret;
} else if (typeof chunk === 'boolean' || typeof chunk === 'number') {
// Handle primitives that the SourceNode will throw up on
- return chunk+'';
+ return chunk + '';
}
return chunk;
}
@@ -90,7 +91,7 @@ CodeGen.prototype = {
},
empty: function(loc) {
- loc = loc || this.currentLocation || {start:{}};
+ loc = loc || this.currentLocation || {start: {}};
return new SourceNode(loc.start.line, loc.start.column, this.srcFile);
},
wrap: function(chunk, loc) {
@@ -98,7 +99,7 @@ CodeGen.prototype = {
return chunk;
}
- loc = loc || this.currentLocation || {start:{}};
+ loc = loc || this.currentLocation || {start: {}};
chunk = castChunk(chunk, this, loc);
return new SourceNode(loc.start.line, loc.start.column, this.srcFile, chunk);
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index a39082d..a9bfc85 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -1,6 +1,6 @@
-import Exception from "../exception";
-import {isArray, indexOf} from "../utils";
-import AST from "./ast";
+import Exception from '../exception';
+import {isArray, indexOf} from '../utils';
+import AST from './ast';
var slice = [].slice;
@@ -67,7 +67,9 @@ Compiler.prototype = {
};
if (knownHelpers) {
for (var name in knownHelpers) {
- options.knownHelpers[name] = knownHelpers[name];
+ if (name in knownHelpers) {
+ options.knownHelpers[name] = knownHelpers[name];
+ }
}
}
@@ -75,7 +77,7 @@ Compiler.prototype = {
},
compileProgram: function(program) {
- var result = new this.compiler().compile(program, this.options);
+ var result = new this.compiler().compile(program, this.options); // eslint-disable-line new-cap
var guid = this.guid++;
this.usePartial = this.usePartial || result.usePartial;
@@ -97,7 +99,7 @@ Compiler.prototype = {
this.options.blockParams.unshift(program.blockParams);
var body = program.body;
- for(var i=0, l=body.length; i<l; i++) {
+ for (var i = 0, l = body.length; i < l; i++) {
this.accept(body[i]);
}
@@ -174,9 +176,9 @@ Compiler.prototype = {
},
MustacheStatement: function(mustache) {
- this.SubExpression(mustache);
+ this.SubExpression(mustache); // eslint-disable-line new-cap
- if(mustache.escaped && !this.options.noEscape) {
+ if (mustache.escaped && !this.options.noEscape) {
this.opcode('appendEscaped');
} else {
this.opcode('append');
@@ -231,7 +233,7 @@ Compiler.prototype = {
if (this.options.knownHelpers[name]) {
this.opcode('invokeKnownHelper', params.length, name);
} else if (this.options.knownHelpersOnly) {
- throw new Exception("You specified knownHelpersOnly, but used the unknown helper " + name, sexpr);
+ throw new Exception('You specified knownHelpersOnly, but used the unknown helper ' + name, sexpr);
} else {
path.falsy = true;
@@ -250,7 +252,7 @@ Compiler.prototype = {
if (blockParamId) {
this.opcode('lookupBlockParam', blockParamId, path.parts);
- } else if (!name) {
+ } else if (!name) {
// Context reference, i.e. `{{foo .}}` or `{{foo ..}}`
this.opcode('pushContext');
} else if (path.data) {
@@ -286,7 +288,7 @@ Compiler.prototype = {
this.opcode('pushHash');
- for (i=0, l=pairs.length; i<l; i++) {
+ for (i = 0, l = pairs.length; i < l; i++) {
this.pushParam(pairs[i].value);
}
while (i--) {
@@ -336,13 +338,17 @@ Compiler.prototype = {
}
}
- if (isHelper) { return 'helper'; }
- else if (isEligible) { return 'ambiguous'; }
- else { return 'simple'; }
+ if (isHelper) {
+ return 'helper';
+ } else if (isEligible) {
+ return 'ambiguous';
+ } else {
+ return 'simple';
+ }
},
pushParams: function(params) {
- for(var i=0, l=params.length; i<l; i++) {
+ for (var i = 0, l = params.length; i < l; i++) {
this.pushParam(params[i]);
}
},
@@ -357,7 +363,7 @@ Compiler.prototype = {
.replace(/\//g, '.');
}
- if(val.depth) {
+ if (val.depth) {
this.addDepth(val.depth);
}
this.opcode('getContext', val.depth || 0);
@@ -421,7 +427,7 @@ Compiler.prototype = {
export function precompile(input, options, env) {
if (input == null || (typeof input !== 'string' && input.type !== 'Program')) {
- throw new Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input);
+ throw new Exception('You must pass a string or Handlebars AST to Handlebars.precompile. You passed ' + input);
}
options = options || {};
@@ -439,7 +445,7 @@ export function precompile(input, options, env) {
export function compile(input, options, env) {
if (input == null || (typeof input !== 'string' && input.type !== 'Program')) {
- throw new Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
+ throw new Exception('You must pass a string or Handlebars AST to Handlebars.compile. You passed ' + input);
}
options = options || {};
@@ -461,17 +467,17 @@ export function compile(input, options, env) {
}
// Template is only compiled on first use and cached after that point.
- var ret = function(context, options) {
+ function ret(context, execOptions) {
if (!compiled) {
compiled = compileInput();
}
- return compiled.call(this, context, options);
- };
- ret._setup = function(options) {
+ return compiled.call(this, context, execOptions);
+ }
+ ret._setup = function(setupOptions) {
if (!compiled) {
compiled = compileInput();
}
- return compiled._setup(options);
+ return compiled._setup(setupOptions);
};
ret._child = function(i, data, blockParams, depths) {
if (!compiled) {
@@ -502,6 +508,6 @@ function transformLiteralToPath(sexpr) {
var literal = sexpr.path;
// Casting to string here to make false and 0 literal values play nicely with the rest
// of the system.
- sexpr.path = new AST.PathExpression(false, 0, [literal.original+''], literal.original+'', literal.loc);
+ sexpr.path = new AST.PathExpression(false, 0, [literal.original + ''], literal.original + '', literal.loc);
}
}
diff --git a/lib/handlebars/compiler/helpers.js b/lib/handlebars/compiler/helpers.js
index 4a502d8..7f784ad 100644
--- a/lib/handlebars/compiler/helpers.js
+++ b/lib/handlebars/compiler/helpers.js
@@ -1,4 +1,4 @@
-import Exception from "../exception";
+import Exception from '../exception';
export function SourceLocation(source, locInfo) {
this.source = source;
@@ -23,7 +23,7 @@ export function id(token) {
export function stripFlags(open, close) {
return {
open: open.charAt(2) === '~',
- close: close.charAt(close.length-3) === '~'
+ close: close.charAt(close.length - 3) === '~'
};
}
@@ -41,7 +41,7 @@ export function preparePath(data, parts, locInfo) {
depth = 0,
depthString = '';
- for(var i=0,l=parts.length; i<l; i++) {
+ for (var i = 0, l = parts.length; i < l; i++) {
var part = parts[i].part,
// If we have [] syntax then we do not treat path references as operators,
// i.e. foo.[this] resolves to approximately context.foo['this']
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js
index a027edb..e278162 100644
--- a/lib/handlebars/compiler/javascript-compiler.js
+++ b/lib/handlebars/compiler/javascript-compiler.js
@@ -1,7 +1,7 @@
-import { COMPILER_REVISION, REVISION_CHANGES } from "../base";
-import Exception from "../exception";
-import {isArray} from "../utils";
-import CodeGen from "./code-gen";
+import { COMPILER_REVISION, REVISION_CHANGES } from '../base';
+import Exception from '../exception';
+import {isArray} from '../utils';
+import CodeGen from './code-gen';
function Literal(value) {
this.value = value;
@@ -14,7 +14,7 @@ JavaScriptCompiler.prototype = {
// alternative compiled forms for name lookup and buffering semantics
nameLookup: function(parent, name /* , type*/) {
if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
- return [parent, ".", name];
+ return [parent, '.', name];
} else {
return [parent, "['", name, "']"];
}
@@ -50,7 +50,7 @@ JavaScriptCompiler.prototype = {
},
initializeBuffer: function() {
- return this.quotedString("");
+ return this.quotedString('');
},
// END PUBLIC API
@@ -169,8 +169,8 @@ JavaScriptCompiler.prototype = {
var varDeclarations = '';
var locals = this.stackVars.concat(this.registers.list);
- if(locals.length > 0) {
- varDeclarations += ", " + locals.join(", ");
+ if (locals.length > 0) {
+ varDeclarations += ', ' + locals.join(', ');
}
// Generate minimizer alias mappings
@@ -180,7 +180,7 @@ JavaScriptCompiler.prototype = {
// aliases will not be used, but this case is already being run on the client and
// we aren't concern about minimizing the template size.
var aliasCount = 0;
- for (var alias in this.aliases) {
+ for (var alias in this.aliases) { // eslint-disable-line guard-for-in
var node = this.aliases[alias];
if (this.aliases.hasOwnProperty(alias) && node.children && node.referenceCount > 1) {
@@ -189,7 +189,7 @@ JavaScriptCompiler.prototype = {
}
}
- var params = ["depth0", "helpers", "partials", "data"];
+ var params = ['depth0', 'helpers', 'partials', 'data'];
if (this.useBlockParams || this.useDepths) {
params.push('blockParams');
@@ -252,7 +252,7 @@ JavaScriptCompiler.prototype = {
this.source.push('return "";');
}
} else {
- varDeclarations += ", buffer = " + (appendFirst ? '' : this.initializeBuffer());
+ varDeclarations += ', buffer = ' + (appendFirst ? '' : this.initializeBuffer());
if (bufferStart) {
bufferStart.prepend('return buffer + ');
@@ -446,6 +446,7 @@ JavaScriptCompiler.prototype = {
var len = parts.length;
for (; i < len; i++) {
+ /*eslint-disable no-loop-func */
this.replaceStack(function(current) {
var lookup = this.nameLookup(current, parts[i], type);
// We want to ensure that zero and false are handled properly if the context (falsy flag)
@@ -457,6 +458,7 @@ JavaScriptCompiler.prototype = {
return [' && ', lookup];
}
});
+ /*eslint-enable no-loop-func */
}
},
@@ -633,7 +635,7 @@ JavaScriptCompiler.prototype = {
'(', lookup,
(helper.paramsInit ? ['),(', helper.paramsInit] : []), '),',
'(typeof helper === ', this.aliasable('"function"'), ' ? ',
- this.source.functionCall('helper','call', helper.callParams), ' : helper))'
+ this.source.functionCall('helper', 'call', helper.callParams), ' : helper))'
]);
},
@@ -728,9 +730,9 @@ JavaScriptCompiler.prototype = {
compileChildren: function(environment, options) {
var children = environment.children, child, compiler;
- for(var i=0, l=children.length; i<l; i++) {
+ for (var i = 0, l = children.length; i < l; i++) {
child = children[i];
- compiler = new this.compiler();
+ compiler = new this.compiler(); // eslint-disable-line new-cap
var index = this.matchExistingProgram(child);
@@ -777,7 +779,7 @@ JavaScriptCompiler.prototype = {
},
useRegister: function(name) {
- if(!this.registers[name]) {
+ if (!this.registers[name]) {
this.registers[name] = true;
this.registers.list.push(name);
}
@@ -849,11 +851,11 @@ JavaScriptCompiler.prototype = {
incrStack: function() {
this.stackSlot++;
- if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
+ if (this.stackSlot > this.stackVars.length) { this.stackVars.push('stack' + this.stackSlot); }
return this.topStackName();
},
topStackName: function() {
- return "stack" + this.stackSlot;
+ return 'stack' + this.stackSlot;
},
flushInline: function() {
var inlineStack = this.inlineStack;
@@ -1019,33 +1021,35 @@ JavaScriptCompiler.prototype = {
};
-var reservedWords = (
- "break else new var" +
- " case finally return void" +
- " catch for switch while" +
- " continue function this with" +
- " default if throw" +
- " delete in try" +
- " do instanceof typeof" +
- " abstract enum int short" +
- " boolean export interface static" +
- " byte extends long super" +
- " char final native synchronized" +
- " class float package throws" +
- " const goto private transient" +
- " debugger implements protected volatile" +
- " double import public let yield await" +
- " null true false"
-).split(" ");
-
-var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
-
-for(var i=0, l=reservedWords.length; i<l; i++) {
- compilerWords[reservedWords[i]] = true;
-}
+(function() {
+ var reservedWords = (
+ 'break else new var' +
+ ' case finally return void' +
+ ' catch for switch while' +
+ ' continue function this with' +
+ ' default if throw' +
+ ' delete in try' +
+ ' do instanceof typeof' +
+ ' abstract enum int short' +
+ ' boolean export interface static' +
+ ' byte extends long super' +
+ ' char final native synchronized' +
+ ' class float package throws' +
+ ' const goto private transient' +
+ ' debugger implements protected volatile' +
+ ' double import public let yield await' +
+ ' null true false'
+ ).split(' ');
+
+ var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
+
+ for (var i = 0, l = reservedWords.length; i < l; i++) {
+ compilerWords[reservedWords[i]] = true;
+ }
+}());
JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
- return !JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(name);
+ return !JavaScriptCompiler.RESERVED_WORDS[name] && (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/).test(name);
};
function strictLookup(requireTerminal, compiler, parts, type) {
diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js
index dcc73c3..5c708a1 100644
--- a/lib/handlebars/compiler/printer.js
+++ b/lib/handlebars/compiler/printer.js
@@ -1,4 +1,5 @@
-import Visitor from "./visitor";
+/*eslint-disable new-cap */
+import Visitor from './visitor';
export function print(ast) {
return new PrintVisitor().accept(ast);
@@ -11,13 +12,13 @@ export function PrintVisitor() {
PrintVisitor.prototype = new Visitor();
PrintVisitor.prototype.pad = function(string) {
- var out = "";
+ var out = '';
- for(var i=0,l=this.padding; i<l; i++) {
- out = out + " ";
+ for (var i = 0, l = this.padding; i < l; i++) {
+ out = out + ' ';
}
- out = out + string + "\n";
+ out = out + string + '\n';
return out;
};
@@ -28,14 +29,14 @@ PrintVisitor.prototype.Program = function(program) {
if (program.blockParams) {
var blockParams = 'BLOCK PARAMS: [';
- for(i=0, l=program.blockParams.length; i<l; i++) {
+ for (i = 0, l = program.blockParams.length; i < l; i++) {
blockParams += ' ' + program.blockParams[i];
}
blockParams += ' ]';
out += this.pad(blockParams);
}
- for(i=0, l=body.length; i<l; i++) {
+ for (i = 0, l = body.length; i < l; i++) {
out = out + this.accept(body[i]);
}
@@ -49,7 +50,7 @@ PrintVisitor.prototype.MustacheStatement = function(mustache) {
};
PrintVisitor.prototype.BlockStatement = function(block) {
- var out = "";
+ var out = '';
out = out + this.pad('BLOCK:');
this.padding++;
@@ -75,7 +76,7 @@ PrintVisitor.prototype.BlockStatement = function(block) {
PrintVisitor.prototype.PartialStatement = function(partial) {
var content = 'PARTIAL:' + partial.name.original;
- if(partial.params[0]) {
+ if (partial.params[0]) {
content += ' ' + this.accept(partial.params[0]);
}
if (partial.hash) {
@@ -95,15 +96,15 @@ PrintVisitor.prototype.CommentStatement = function(comment) {
PrintVisitor.prototype.SubExpression = function(sexpr) {
var params = sexpr.params, paramStrings = [], hash;
- for(var i=0, l=params.length; i<l; i++) {
+ for (var i = 0, l = params.length; i < l; i++) {
paramStrings.push(this.accept(params[i]));
}
- params = "[" + paramStrings.join(", ") + "]";
+ params = '[' + paramStrings.join(', ') + ']';
- hash = sexpr.hash ? " " + this.accept(sexpr.hash) : "";
+ hash = sexpr.hash ? ' ' + this.accept(sexpr.hash) : '';
- return this.accept(sexpr.path) + " " + params + hash;
+ return this.accept(sexpr.path) + ' ' + params + hash;
};
PrintVisitor.prototype.PathExpression = function(id) {
@@ -117,11 +118,11 @@ PrintVisitor.prototype.StringLiteral = function(string) {
};
PrintVisitor.prototype.NumberLiteral = function(number) {
- return "NUMBER{" + number.value + "}";
+ return 'NUMBER{' + number.value + '}';
};
PrintVisitor.prototype.BooleanLiteral = function(bool) {
- return "BOOLEAN{" + bool.value + "}";
+ return 'BOOLEAN{' + bool.value + '}';
};
PrintVisitor.prototype.UndefinedLiteral = function() {
@@ -136,7 +137,7 @@ PrintVisitor.prototype.Hash = function(hash) {
var pairs = hash.pairs;
var joinedPairs = [];
- for (var i=0, l=pairs.length; i<l; i++) {
+ for (var i = 0, l = pairs.length; i < l; i++) {
joinedPairs.push(this.accept(pairs[i]));
}
@@ -145,3 +146,4 @@ PrintVisitor.prototype.Hash = function(hash) {
PrintVisitor.prototype.HashPair = function(pair) {
return pair.key + '=' + this.accept(pair.value);
};
+/*eslint-enable new-cap */
diff --git a/lib/handlebars/compiler/visitor.js b/lib/handlebars/compiler/visitor.js
index b45b268..692a511 100644
--- a/lib/handlebars/compiler/visitor.js
+++ b/lib/handlebars/compiler/visitor.js
@@ -1,5 +1,5 @@
-import Exception from "../exception";
-import AST from "./ast";
+import Exception from '../exception';
+import AST from './ast';
function Visitor() {
this.parents = [];
diff --git a/lib/handlebars/compiler/whitespace-control.js b/lib/handlebars/compiler/whitespace-control.js
index 9786612..aabe45a 100644
--- a/lib/handlebars/compiler/whitespace-control.js
+++ b/lib/handlebars/compiler/whitespace-control.js
@@ -1,4 +1,4 @@
-import Visitor from "./visitor";
+import Visitor from './visitor';
function WhitespaceControl() {
}
@@ -38,7 +38,7 @@ WhitespaceControl.prototype.Program = function(program) {
// If we are on a standalone node, save the indent info for partials
if (current.type === 'PartialStatement') {
// Pull out the whitespace from the final line
- current.indent = (/([ \t]+$)/).exec(body[i-1].original)[1];
+ current.indent = (/([ \t]+$)/).exec(body[i - 1].original)[1];
}
}
}
@@ -73,7 +73,7 @@ WhitespaceControl.prototype.BlockStatement = function(block) {
// Walk the inverse chain to find the last inverse that is actually in the chain.
while (lastInverse.chained) {
- lastInverse = lastInverse.body[lastInverse.body.length-1].program;
+ lastInverse = lastInverse.body[lastInverse.body.length - 1].program;
}
}
@@ -108,14 +108,11 @@ WhitespaceControl.prototype.BlockStatement = function(block) {
// Find standalone else statments
if (isPrevWhitespace(program.body)
&& isNextWhitespace(firstInverse.body)) {
-
omitLeft(program.body);
omitRight(firstInverse.body);
}
- } else {
- if (block.closeStrip.open) {
- omitLeft(program.body, null, true);
- }
+ } else if (block.closeStrip.open) {
+ omitLeft(program.body, null, true);
}
return strip;
@@ -125,7 +122,7 @@ WhitespaceControl.prototype.MustacheStatement = function(mustache) {
return mustache.strip;
};
-WhitespaceControl.prototype.PartialStatement =
+WhitespaceControl.prototype.PartialStatement =
WhitespaceControl.prototype.CommentStatement = function(node) {
/* istanbul ignore next */
var strip = node.strip || {};
@@ -144,8 +141,8 @@ function isPrevWhitespace(body, i, isRoot) {
// Nodes that end with newlines are considered whitespace (but are special
// cased for strip operations)
- var prev = body[i-1],
- sibling = body[i-2];
+ var prev = body[i - 1],
+ sibling = body[i - 2];
if (!prev) {
return isRoot;
}
@@ -159,8 +156,8 @@ function isNextWhitespace(body, i, isRoot) {
i = -1;
}
- var next = body[i+1],
- sibling = body[i+2];
+ var next = body[i + 1],
+ sibling = body[i + 2];
if (!next) {
return isRoot;
}