mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Marko v3: Added support for static vars and local variables
This commit is contained in:
parent
7f65aade1d
commit
795a1d495f
@ -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);
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
14
test/fixtures/codegen/autotest/context-addVar/expected.js
vendored
Normal file
14
test/fixtures/codegen/autotest/context-addVar/expected.js
vendored
Normal file
@ -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("<div></div>");
|
||||
};
|
||||
}
|
||||
|
||||
(module.exports = require("marko").c(__filename)).c(create);
|
||||
14
test/fixtures/codegen/autotest/context-addVar/index.js
vendored
Normal file
14
test/fixtures/codegen/autotest/context-addVar/index.js
vendored
Normal file
@ -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;
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user