marko/compiler/ast/Vars.js
Patrick Steele-Idem c386da875e Fixes #349 - Inline Marko template compilation support
Also changed how JavaScript code is generated
2016-08-19 10:50:28 -06:00

76 lines
1.7 KiB
JavaScript

'use strict';
var Node = require('./Node');
class Vars extends Node {
constructor(def) {
super('Vars');
this.kind = def.kind || 'var';
this.declarations = def.declarations;
this.body = this.makeContainer(def.body);
}
generateCode(codegen) {
var declarations = this.declarations;
if (!declarations || !declarations.length) {
return null;
}
if (this.body && this.body.length) {
var scopedBody = [this].concat(this.body);
this.body = null;
return codegen.builder.selfInvokingFunction(scopedBody);
}
return this;
}
writeCode(writer) {
var declarations = this.declarations;
var kind = this.kind;
var isStatement = this.statement;
if (!declarations || !declarations.length) {
return;
}
writer.incIndent(4);
for (let i=0; i<declarations.length; i++) {
var declarator = declarations[i];
if (i === 0) {
writer.write(kind + ' ');
} else {
writer.writeLineIndent();
}
writer.write(declarator);
if (i < declarations.length - 1) {
writer.write(',\n');
} else {
if (isStatement) {
writer.write(';\n');
}
}
}
writer.decIndent(4);
}
walk(walker) {
this.argument = walker.walk(this.argument);
}
/**
* "noOutput" should be true if the Node.js does not result in any HTML or Text output
*/
get noOutput() {
return !(this.body && this.body.length);
}
}
module.exports = Vars;