diff options
author | kpdecker <kpdecker@gmail.com> | 2015-04-20 02:38:28 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2015-04-20 02:38:28 -0500 |
commit | 4bed826d0e210c336fb9e500835b1c1926562da5 (patch) | |
tree | afd32d4dc7b4c2fcf6c38d0355def38ddaffdcc0 /lib/handlebars/compiler/compiler.js | |
parent | 0263aa48bc8c8cdcd332edd01a644a9a0fd1cc81 (diff) | |
download | handlebars.js-4bed826d0e210c336fb9e500835b1c1926562da5.zip handlebars.js-4bed826d0e210c336fb9e500835b1c1926562da5.tar.gz handlebars.js-4bed826d0e210c336fb9e500835b1c1926562da5.tar.bz2 |
Update for let and optional parameters
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 100 |
1 files changed, 50 insertions, 50 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index a9bfc85..4575421 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -2,8 +2,7 @@ import Exception from '../exception'; import {isArray, indexOf} from '../utils'; import AST from './ast'; -var slice = [].slice; - +const slice = [].slice; export function Compiler() {} @@ -16,13 +15,13 @@ Compiler.prototype = { compiler: Compiler, equals: function(other) { - var len = this.opcodes.length; + let len = this.opcodes.length; if (other.opcodes.length !== len) { return false; } - for (var i = 0; i < len; i++) { - var opcode = this.opcodes[i], + for (let i = 0; i < len; i++) { + let opcode = this.opcodes[i], otherOpcode = other.opcodes[i]; if (opcode.opcode !== otherOpcode.opcode || !argEquals(opcode.args, otherOpcode.args)) { return false; @@ -32,7 +31,7 @@ Compiler.prototype = { // We know that length is the same between the two arrays because they are directly tied // to the opcode behavior above. len = this.children.length; - for (i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { if (!this.children[i].equals(other.children[i])) { return false; } @@ -54,7 +53,7 @@ Compiler.prototype = { options.blockParams = options.blockParams || []; // These changes will propagate to the other compiler components - var knownHelpers = options.knownHelpers; + let knownHelpers = options.knownHelpers; options.knownHelpers = { 'helperMissing': true, 'blockHelperMissing': true, @@ -66,7 +65,7 @@ Compiler.prototype = { 'lookup': true }; if (knownHelpers) { - for (var name in knownHelpers) { + for (let name in knownHelpers) { if (name in knownHelpers) { options.knownHelpers[name] = knownHelpers[name]; } @@ -77,8 +76,9 @@ Compiler.prototype = { }, compileProgram: function(program) { - var result = new this.compiler().compile(program, this.options); // eslint-disable-line new-cap - var guid = this.guid++; + let childCompiler = new this.compiler(), // eslint-disable-line new-cap + result = childCompiler.compile(program, this.options), + guid = this.guid++; this.usePartial = this.usePartial || result.usePartial; @@ -90,7 +90,7 @@ Compiler.prototype = { accept: function(node) { this.sourceNode.unshift(node); - var ret = this[node.type](node); + let ret = this[node.type](node); this.sourceNode.shift(); return ret; }, @@ -98,14 +98,15 @@ Compiler.prototype = { Program: function(program) { this.options.blockParams.unshift(program.blockParams); - var body = program.body; - for (var i = 0, l = body.length; i < l; i++) { + let body = program.body, + bodyLength = body.length; + for (let i = 0; i < bodyLength; i++) { this.accept(body[i]); } this.options.blockParams.shift(); - this.isSimple = l === 1; + this.isSimple = bodyLength === 1; this.blockParams = program.blockParams ? program.blockParams.length : 0; return this; @@ -114,13 +115,13 @@ Compiler.prototype = { BlockStatement: function(block) { transformLiteralToPath(block); - var program = block.program, + let program = block.program, inverse = block.inverse; program = program && this.compileProgram(program); inverse = inverse && this.compileProgram(inverse); - var type = this.classifySexpr(block); + let type = this.classifySexpr(block); if (type === 'helper') { this.helperSexpr(block, program, inverse); @@ -150,14 +151,14 @@ Compiler.prototype = { PartialStatement: function(partial) { this.usePartial = true; - var params = partial.params; + let params = partial.params; if (params.length > 1) { throw new Exception('Unsupported number of partial arguments: ' + params.length, partial); } else if (!params.length) { params.push({type: 'PathExpression', parts: [], depth: 0}); } - var partialName = partial.name.original, + let partialName = partial.name.original, isDynamic = partial.name.type === 'SubExpression'; if (isDynamic) { this.accept(partial.name); @@ -165,7 +166,7 @@ Compiler.prototype = { this.setupFullMustacheParams(partial, undefined, undefined, true); - var indent = partial.indent || ''; + let indent = partial.indent || ''; if (this.options.preventIndent && indent) { this.opcode('appendContent', indent); indent = ''; @@ -195,7 +196,7 @@ Compiler.prototype = { SubExpression: function(sexpr) { transformLiteralToPath(sexpr); - var type = this.classifySexpr(sexpr); + let type = this.classifySexpr(sexpr); if (type === 'simple') { this.simpleSexpr(sexpr); @@ -206,7 +207,7 @@ Compiler.prototype = { } }, ambiguousSexpr: function(sexpr, program, inverse) { - var path = sexpr.path, + let path = sexpr.path, name = path.parts[0], isBlock = program != null || inverse != null; @@ -226,7 +227,7 @@ Compiler.prototype = { }, helperSexpr: function(sexpr, program, inverse) { - var params = this.setupFullMustacheParams(sexpr, program, inverse), + let params = this.setupFullMustacheParams(sexpr, program, inverse), path = sexpr.path, name = path.parts[0]; @@ -246,7 +247,7 @@ Compiler.prototype = { this.addDepth(path.depth); this.opcode('getContext', path.depth); - var name = path.parts[0], + let name = path.parts[0], scoped = AST.helpers.scopedId(path), blockParamId = !path.depth && !scoped && this.blockParamIndex(name); @@ -284,11 +285,13 @@ Compiler.prototype = { }, Hash: function(hash) { - var pairs = hash.pairs, i, l; + let pairs = hash.pairs, + i = 0, + l = pairs.length; this.opcode('pushHash'); - for (i = 0, l = pairs.length; i < l; i++) { + for (; i < l; i++) { this.pushParam(pairs[i].value); } while (i--) { @@ -311,25 +314,24 @@ Compiler.prototype = { }, classifySexpr: function(sexpr) { - var isSimple = AST.helpers.simpleId(sexpr.path); + let isSimple = AST.helpers.simpleId(sexpr.path); - var isBlockParam = isSimple && !!this.blockParamIndex(sexpr.path.parts[0]); + let isBlockParam = isSimple && !!this.blockParamIndex(sexpr.path.parts[0]); // a mustache is an eligible helper if: // * its id is simple (a single part, not `this` or `..`) - var isHelper = !isBlockParam && AST.helpers.helperExpression(sexpr); + let isHelper = !isBlockParam && 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 = !isBlockParam && (isHelper || isSimple); - - var options = this.options; + let isEligible = !isBlockParam && (isHelper || isSimple); // if ambiguous, we can possibly resolve the ambiguity now // An eligible helper is one that does not have a complex path, i.e. `this.foo`, `../foo` etc. if (isEligible && !isHelper) { - var name = sexpr.path.parts[0]; + let name = sexpr.path.parts[0], + options = this.options; if (options.knownHelpers[name]) { isHelper = true; @@ -348,13 +350,13 @@ Compiler.prototype = { }, pushParams: function(params) { - for (var i = 0, l = params.length; i < l; i++) { + for (let i = 0, l = params.length; i < l; i++) { this.pushParam(params[i]); } }, pushParam: function(val) { - var value = val.value != null ? val.value : val.original || ''; + let value = val.value != null ? val.value : val.original || ''; if (this.stringParams) { if (value.replace) { @@ -376,12 +378,12 @@ Compiler.prototype = { } } else { if (this.trackIds) { - var blockParamIndex; + let blockParamIndex; if (val.parts && !AST.helpers.scopedId(val) && !val.depth) { blockParamIndex = this.blockParamIndex(val.parts[0]); } if (blockParamIndex) { - var blockParamChild = val.parts.slice(1).join('.'); + let blockParamChild = val.parts.slice(1).join('.'); this.opcode('pushId', 'BlockParam', blockParamIndex, blockParamChild); } else { value = val.original || value; @@ -399,7 +401,7 @@ Compiler.prototype = { }, setupFullMustacheParams: function(sexpr, program, inverse, omitEmpty) { - var params = sexpr.params; + let params = sexpr.params; this.pushParams(params); this.opcode('pushProgram', program); @@ -415,8 +417,8 @@ Compiler.prototype = { }, blockParamIndex: function(name) { - for (var depth = 0, len = this.options.blockParams.length; depth < len; depth++) { - var blockParams = this.options.blockParams[depth], + for (let depth = 0, len = this.options.blockParams.length; depth < len; depth++) { + let blockParams = this.options.blockParams[depth], param = blockParams && indexOf(blockParams, name); if (blockParams && param >= 0) { return [depth, param]; @@ -438,18 +440,16 @@ export function precompile(input, options, env) { options.useDepths = true; } - var ast = env.parse(input, options); - var environment = new env.Compiler().compile(ast, options); + let ast = env.parse(input, options), + environment = new env.Compiler().compile(ast, options); return new env.JavaScriptCompiler().compile(environment, options); } -export function compile(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); } - options = options || {}; - if (!('data' in options)) { options.data = true; } @@ -457,12 +457,12 @@ export function compile(input, options, env) { options.useDepths = true; } - var compiled; + let compiled; function compileInput() { - var ast = env.parse(input, options); - var environment = new env.Compiler().compile(ast, options); - var templateSpec = new env.JavaScriptCompiler().compile(environment, options, undefined, true); + let ast = env.parse(input, options), + environment = new env.Compiler().compile(ast, options), + templateSpec = new env.JavaScriptCompiler().compile(environment, options, undefined, true); return env.template(templateSpec); } @@ -494,7 +494,7 @@ function argEquals(a, b) { } if (isArray(a) && isArray(b) && a.length === b.length) { - for (var i = 0; i < a.length; i++) { + for (let i = 0; i < a.length; i++) { if (!argEquals(a[i], b[i])) { return false; } @@ -505,7 +505,7 @@ function argEquals(a, b) { function transformLiteralToPath(sexpr) { if (!sexpr.path.parts) { - var literal = sexpr.path; + let 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); |