diff options
author | Yehuda Katz <wycats@gmail.com> | 2013-07-01 13:59:58 -0700 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2013-07-01 13:59:58 -0700 |
commit | 88ee4757e77f97afb206132eddb64e688700eb37 (patch) | |
tree | 08f7db62a3151dfbd44bfd6f1ec4ec6737a2bdb6 /lib/handlebars/compiler | |
parent | 8e2416dabb2056c07357a55b9259322f0d794ada (diff) | |
download | handlebars.js-88ee4757e77f97afb206132eddb64e688700eb37.zip handlebars.js-88ee4757e77f97afb206132eddb64e688700eb37.tar.gz handlebars.js-88ee4757e77f97afb206132eddb64e688700eb37.tar.bz2 |
Initial work on ES6 modules
Diffstat (limited to 'lib/handlebars/compiler')
-rw-r--r-- | lib/handlebars/compiler/ast.js | 39 | ||||
-rw-r--r-- | lib/handlebars/compiler/base.js | 25 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 46 | ||||
-rw-r--r-- | lib/handlebars/compiler/javascript-compiler.js | 24 |
4 files changed, 50 insertions, 84 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 567e297..fcf5c6e 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,15 +1,12 @@ -exports.attach = function(Handlebars) { +import { Exception } from "handlebars/utils"; -// BEGIN(BROWSER) -Handlebars.AST = {}; - -Handlebars.AST.ProgramNode = function(statements, inverse) { +export function ProgramNode(statements, inverse) { this.type = "program"; this.statements = statements; if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); } }; -Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) { +export function MustacheNode(rawParams, hash, unescaped) { this.type = "mustache"; this.escaped = !unescaped; this.hash = hash; @@ -31,15 +28,15 @@ Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) { // pass or at runtime. }; -Handlebars.AST.PartialNode = function(partialName, context) { +export function PartialNode(partialName, context) { this.type = "partial"; this.partialName = partialName; this.context = context; }; -Handlebars.AST.BlockNode = function(mustache, program, inverse, close) { +export function BlockNode(mustache, program, inverse, close) { if(mustache.id.original !== close.original) { - throw new Handlebars.Exception(mustache.id.original + " doesn't match " + close.original); + throw new Exception(mustache.id.original + " doesn't match " + close.original); } this.type = "block"; @@ -52,17 +49,17 @@ Handlebars.AST.BlockNode = function(mustache, program, inverse, close) { } }; -Handlebars.AST.ContentNode = function(string) { +export function ContentNode(string) { this.type = "content"; this.string = string; }; -Handlebars.AST.HashNode = function(pairs) { +export function HashNode(pairs) { this.type = "hash"; this.pairs = pairs; }; -Handlebars.AST.IdNode = function(parts) { +export function IdNode(part) { this.type = "ID"; var original = "", @@ -93,43 +90,37 @@ Handlebars.AST.IdNode = function(parts) { this.stringModeValue = this.string; }; -Handlebars.AST.PartialNameNode = function(name) { +export function PartialNameNode(name) { this.type = "PARTIAL_NAME"; this.name = name.original; }; -Handlebars.AST.DataNode = function(id) { +export function DataNode(id) { this.type = "DATA"; this.id = id; }; -Handlebars.AST.StringNode = function(string) { +export function StringNode(string) { this.type = "STRING"; this.original = this.string = this.stringModeValue = string; }; -Handlebars.AST.IntegerNode = function(integer) { +export function IntegerNode(integer) { this.type = "INTEGER"; this.original = this.integer = integer; this.stringModeValue = Number(integer); }; -Handlebars.AST.BooleanNode = function(bool) { +export function BooleanNode(bool) { this.type = "BOOLEAN"; this.bool = bool; this.stringModeValue = bool === "true"; }; -Handlebars.AST.CommentNode = function(comment) { +export function CommentNode(comment) { this.type = "comment"; this.comment = comment; }; - -// END(BROWSER) - -return Handlebars; -}; - diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js index 7594451..525cd5b 100644 --- a/lib/handlebars/compiler/base.js +++ b/lib/handlebars/compiler/base.js @@ -1,21 +1,12 @@ -var handlebars = require("./parser"); +import { parser } from "handlebars/compiler/parser"; +module AST from "handlebars/compiler/ast": -exports.attach = function(Handlebars) { - -// BEGIN(BROWSER) - -Handlebars.Parser = handlebars; - -Handlebars.parse = function(input) { +export Parser = parser; +export function(input) { // Just return if an already-compile AST was passed in. - if(input.constructor === Handlebars.AST.ProgramNode) { return input; } - - Handlebars.Parser.yy = Handlebars.AST; - return Handlebars.Parser.parse(input); -}; - -// END(BROWSER) + if(input.constructor === AST.ProgramNode) { return input; } -return Handlebars; -}; + parser.yy = AST; + return parser.parse(input); +} diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 98e12b1..a21ccc0 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -1,13 +1,11 @@ -var compilerbase = require("./base"); - -exports.attach = function(Handlebars) { - -compilerbase.attach(Handlebars); - -// BEGIN(BROWSER) +import { Exception } from "handlebars/utils"; +import parse from "handlebars/compiler/base"; +import { template } from "handlebars/runtime"; /*jshint eqnull:true*/ -var Compiler = Handlebars.Compiler = function() {}; + +var Compiler = function() {}; +export default Compiler; // the foundHelper register will disambiguate helper lookup from finding a // function in a context. This is necessary for mustache compatibility, which @@ -40,6 +38,7 @@ Compiler.prototype = { return out.join("\n"); }, + equals: function(other) { var len = this.opcodes.length; if (other.opcodes.length !== len) { @@ -301,7 +300,7 @@ Compiler.prototype = { DATA: function(data) { this.options.data = true; if (data.id.isScoped || data.id.depth) { - throw new Handlebars.Exception('Scoped data references are not supported: ' + data.original); + throw new Exception('Scoped data references are not supported: ' + data.original); } this.opcode('lookupData'); @@ -415,35 +414,39 @@ Compiler.prototype = { } }; -Handlebars.precompile = function(input, options) { +export function precompile(input, options) { if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) { - throw new Handlebars.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 || {}; if (!('data' in options)) { options.data = true; } - var ast = Handlebars.parse(input); + + var ast = parse(input); var environment = new Compiler().compile(ast, options); - return new Handlebars.JavaScriptCompiler().compile(environment, options); + return new JavaScriptCompiler().compile(environment, options); }; -Handlebars.compile = function(input, options) { +export function compile = function(input, options) { if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) { - throw new Handlebars.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 || {}; + if (!('data' in options)) { options.data = true; } + var compiled; + function compile() { - var ast = Handlebars.parse(input); + var ast = parse(input); var environment = new Compiler().compile(ast, options); - var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true); - return Handlebars.template(templateSpec); + var templateSpec = new JavaScriptCompiler().compile(environment, options, undefined, true); + return template(templateSpec); } // Template is only compiled on first use and cached after that point. @@ -454,10 +457,3 @@ Handlebars.compile = function(input, options) { return compiled.call(this, context, options); }; }; - - -// END(BROWSER) - -return Handlebars; - -}; diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index 4548c6a..49660ff 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -1,18 +1,12 @@ -var compilerbase = require("./base"); +import { COMPILER_REVISION, REVISION_CHANGES } from "handlebars/base"; -exports.attach = function(Handlebars) { - -compilerbase.attach(Handlebars); - -// BEGIN(BROWSER) -/*jshint eqnull:true*/ - -var Literal = function(value) { +function Literal(value) { this.value = value; }; -var JavaScriptCompiler = Handlebars.JavaScriptCompiler = function() {}; +function JavaScriptCompiler() {}; +export default JavaScriptCompiler; JavaScriptCompiler.prototype = { // PUBLIC API: You can override these methods in a subclass to provide @@ -162,8 +156,8 @@ JavaScriptCompiler.prototype = { var source = this.mergeSource(); if (!this.isChild) { - var revision = Handlebars.COMPILER_REVISION, - versions = Handlebars.REVISION_CHANGES[revision]; + var revision = COMPILER_REVISION, + versions = REVISION_CHANGES[revision]; source = "this.compilerInfo = ["+revision+",'"+versions+"'];\n"+source; } @@ -848,9 +842,3 @@ JavaScriptCompiler.isValidJavaScriptVariableName = function(name) { } return false; }; - -// END(BROWSER) - -return Handlebars; - -}; |