1 /* eslint-disable new-cap */
2 import Visitor from './visitor';
4 export function print(ast) {
5 return new PrintVisitor().accept(ast);
8 export function PrintVisitor() {
12 PrintVisitor.prototype = new Visitor();
14 PrintVisitor.prototype.pad = function(string) {
17 for (let i = 0, l = this.padding; i < l; i++) {
25 PrintVisitor.prototype.Program = function(program) {
30 if (program.blockParams) {
31 let blockParams = 'BLOCK PARAMS: [';
32 for (i = 0, l = program.blockParams.length; i < l; i++) {
33 blockParams += ' ' + program.blockParams[i];
36 out += this.pad(blockParams);
39 for (i = 0, l = body.length; i < l; i++) {
40 out += this.accept(body[i]);
48 PrintVisitor.prototype.MustacheStatement = function(mustache) {
49 return this.pad('{{ ' + this.SubExpression(mustache) + ' }}');
51 PrintVisitor.prototype.Decorator = function(mustache) {
52 return this.pad('{{ DIRECTIVE ' + this.SubExpression(mustache) + ' }}');
55 PrintVisitor.prototype.BlockStatement =
56 PrintVisitor.prototype.DecoratorBlock = function(block) {
59 out += this.pad((block.type === 'DecoratorBlock' ? 'DIRECTIVE ' : '') + 'BLOCK:');
61 out += this.pad(this.SubExpression(block));
63 out += this.pad('PROGRAM:');
65 out += this.accept(block.program);
69 if (block.program) { this.padding++; }
70 out += this.pad('{{^}}');
72 out += this.accept(block.inverse);
74 if (block.program) { this.padding--; }
81 PrintVisitor.prototype.PartialStatement = function(partial) {
82 let content = 'PARTIAL:' + partial.name.original;
83 if (partial.params[0]) {
84 content += ' ' + this.accept(partial.params[0]);
87 content += ' ' + this.accept(partial.hash);
89 return this.pad('{{> ' + content + ' }}');
91 PrintVisitor.prototype.PartialBlockStatement = function(partial) {
92 let content = 'PARTIAL BLOCK:' + partial.name.original;
93 if (partial.params[0]) {
94 content += ' ' + this.accept(partial.params[0]);
97 content += ' ' + this.accept(partial.hash);
100 content += ' ' + this.pad('PROGRAM:');
102 content += this.accept(partial.program);
105 return this.pad('{{> ' + content + ' }}');
108 PrintVisitor.prototype.ContentStatement = function(content) {
109 return this.pad("CONTENT[ '" + content.value + "' ]");
112 PrintVisitor.prototype.CommentStatement = function(comment) {
113 return this.pad("{{! '" + comment.value + "' }}");
116 PrintVisitor.prototype.SubExpression = function(sexpr) {
117 let params = sexpr.params,
121 for (let i = 0, l = params.length; i < l; i++) {
122 paramStrings.push(this.accept(params[i]));
125 params = '[' + paramStrings.join(', ') + ']';
127 hash = sexpr.hash ? ' ' + this.accept(sexpr.hash) : '';
129 return this.accept(sexpr.path) + ' ' + params + hash;
132 PrintVisitor.prototype.PathExpression = function(id) {
133 let path = id.parts.join('/');
134 return (id.data ? '@' : '') + 'PATH:' + path;
138 PrintVisitor.prototype.StringLiteral = function(string) {
139 return '"' + string.value + '"';
142 PrintVisitor.prototype.NumberLiteral = function(number) {
143 return 'NUMBER{' + number.value + '}';
146 PrintVisitor.prototype.BooleanLiteral = function(bool) {
147 return 'BOOLEAN{' + bool.value + '}';
150 PrintVisitor.prototype.UndefinedLiteral = function() {
154 PrintVisitor.prototype.NullLiteral = function() {
158 PrintVisitor.prototype.Hash = function(hash) {
159 let pairs = hash.pairs,
162 for (let i = 0, l = pairs.length; i < l; i++) {
163 joinedPairs.push(this.accept(pairs[i]));
166 return 'HASH{' + joinedPairs.join(', ') + '}';
168 PrintVisitor.prototype.HashPair = function(pair) {
169 return pair.key + '=' + this.accept(pair.value);
171 /* eslint-enable new-cap */