diff --git a/compiler/CompileContext.js b/compiler/CompileContext.js index 5599ff95d..ca9d662d5 100644 --- a/compiler/CompileContext.js +++ b/compiler/CompileContext.js @@ -52,9 +52,11 @@ class CompileContext { this.taglibLookup = taglibLookup.buildLookup(this.dirname); this.data = {}; + this._vars = {}; + this._uniqueVars = new UniqueVars(); this._staticVars = {}; this._staticCode = null; - this._uniqueVars = new UniqueVars(); + this._uniqueStaticVars = new UniqueVars(); this._srcCharProps = null; this._flags = {}; this._errors = []; @@ -124,8 +126,18 @@ class CompileContext { } - addStaticVar(name, init) { + addVar(name, init) { var actualVarName = this._uniqueVars.addVar(name, init); + this._vars[actualVarName] = init; + return this.builder.identifier(actualVarName); + } + + getVars() { + return this._vars; + } + + addStaticVar(name, init) { + var actualVarName = this._uniqueStaticVars.addVar(name, init); this._staticVars[actualVarName] = init; return this.builder.identifier(actualVarName); } diff --git a/compiler/ast/TemplateRoot.js b/compiler/ast/TemplateRoot.js index 0584fe4f6..10d3a88d8 100644 --- a/compiler/ast/TemplateRoot.js +++ b/compiler/ast/TemplateRoot.js @@ -18,6 +18,8 @@ class TemplateRoot extends Node { } generateCode(codegen) { + var context = codegen.context; + var body = this.body; codegen.addStaticVar('str', '__helpers.s'); codegen.addStaticVar('empty', '__helpers.e'); @@ -27,11 +29,15 @@ class TemplateRoot extends Node { var builder = codegen.builder; var program = builder.program; var functionDeclaration = builder.functionDeclaration; - var vars = builder.vars; + var returnStatement = builder.returnStatement; var slot = builder.slot; var staticsSlot = slot(); + var varsSlot = slot(); + varsSlot.noOutput = true; + + body = [ varsSlot ].concat(body.items); var outputNode = program([ functionDeclaration('create', ['__helpers'], [ @@ -45,10 +51,10 @@ class TemplateRoot extends Node { codegen.generateCode(outputNode); - var staticVars = codegen.getStaticVars(); - var staticCodeArray = codegen.getStaticCode(); + var staticVars = context.getStaticVars(); + var staticCodeArray = context.getStaticCode(); - var staticContent = [vars(createVarsArray(staticVars))]; + var staticContent = [builder.vars(createVarsArray(staticVars))]; if (staticCodeArray) { staticCodeArray.forEach((code) => { staticContent.push(code); @@ -56,6 +62,9 @@ class TemplateRoot extends Node { } staticsSlot.setContent(staticContent); + + var vars = context.getVars(); + varsSlot.setContent(builder.vars(createVarsArray(vars))); } toJSON(prettyPrinter) { diff --git a/test/fixtures/codegen/autotest/context-addVar/expected.js b/test/fixtures/codegen/autotest/context-addVar/expected.js new file mode 100644 index 000000000..77a4735e2 --- /dev/null +++ b/test/fixtures/codegen/autotest/context-addVar/expected.js @@ -0,0 +1,14 @@ +function create(__helpers) { + var str = __helpers.s, + empty = __helpers.e, + notEmpty = __helpers.ne, + escapeXml = __helpers.x; + + return function render(data, out) { + var foo = "Hello World"; + + out.w("
"); + }; +} + +(module.exports = require("marko").c(__filename)).c(create); diff --git a/test/fixtures/codegen/autotest/context-addVar/index.js b/test/fixtures/codegen/autotest/context-addVar/index.js new file mode 100644 index 000000000..1ea58bc5d --- /dev/null +++ b/test/fixtures/codegen/autotest/context-addVar/index.js @@ -0,0 +1,14 @@ +'use strict'; + +module.exports = function(builder, codegen) { + + var templateRoot = builder.templateRoot([ + builder.htmlElement( + 'div', + []) + ]); + + codegen.context.addVar('foo', builder.literal('Hello World')); + + return templateRoot; +}; \ No newline at end of file