Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / handlebars / lib / handlebars / compiler / printer.js
1 /* eslint-disable new-cap */
2 import Visitor from './visitor';
3
4 export function print(ast) {
5   return new PrintVisitor().accept(ast);
6 }
7
8 export function PrintVisitor() {
9   this.padding = 0;
10 }
11
12 PrintVisitor.prototype = new Visitor();
13
14 PrintVisitor.prototype.pad = function(string) {
15   let out = '';
16
17   for (let i = 0, l = this.padding; i < l; i++) {
18     out += '  ';
19   }
20
21   out += string + '\n';
22   return out;
23 };
24
25 PrintVisitor.prototype.Program = function(program) {
26   let out = '',
27       body = program.body,
28       i, l;
29
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];
34     }
35     blockParams += ' ]';
36     out += this.pad(blockParams);
37   }
38
39   for (i = 0, l = body.length; i < l; i++) {
40     out += this.accept(body[i]);
41   }
42
43   this.padding--;
44
45   return out;
46 };
47
48 PrintVisitor.prototype.MustacheStatement = function(mustache) {
49   return this.pad('{{ ' + this.SubExpression(mustache) + ' }}');
50 };
51 PrintVisitor.prototype.Decorator = function(mustache) {
52   return this.pad('{{ DIRECTIVE ' + this.SubExpression(mustache) + ' }}');
53 };
54
55 PrintVisitor.prototype.BlockStatement =
56 PrintVisitor.prototype.DecoratorBlock = function(block) {
57   let out = '';
58
59   out += this.pad((block.type === 'DecoratorBlock' ? 'DIRECTIVE ' : '') + 'BLOCK:');
60   this.padding++;
61   out += this.pad(this.SubExpression(block));
62   if (block.program) {
63     out += this.pad('PROGRAM:');
64     this.padding++;
65     out += this.accept(block.program);
66     this.padding--;
67   }
68   if (block.inverse) {
69     if (block.program) { this.padding++; }
70     out += this.pad('{{^}}');
71     this.padding++;
72     out += this.accept(block.inverse);
73     this.padding--;
74     if (block.program) { this.padding--; }
75   }
76   this.padding--;
77
78   return out;
79 };
80
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]);
85   }
86   if (partial.hash) {
87     content += ' ' + this.accept(partial.hash);
88   }
89   return this.pad('{{> ' + content + ' }}');
90 };
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]);
95   }
96   if (partial.hash) {
97     content += ' ' + this.accept(partial.hash);
98   }
99
100   content += ' ' + this.pad('PROGRAM:');
101   this.padding++;
102   content += this.accept(partial.program);
103   this.padding--;
104
105   return this.pad('{{> ' + content + ' }}');
106 };
107
108 PrintVisitor.prototype.ContentStatement = function(content) {
109   return this.pad("CONTENT[ '" + content.value + "' ]");
110 };
111
112 PrintVisitor.prototype.CommentStatement = function(comment) {
113   return this.pad("{{! '" + comment.value + "' }}");
114 };
115
116 PrintVisitor.prototype.SubExpression = function(sexpr) {
117   let params = sexpr.params,
118       paramStrings = [],
119       hash;
120
121   for (let i = 0, l = params.length; i < l; i++) {
122     paramStrings.push(this.accept(params[i]));
123   }
124
125   params = '[' + paramStrings.join(', ') + ']';
126
127   hash = sexpr.hash ? ' ' + this.accept(sexpr.hash) : '';
128
129   return this.accept(sexpr.path) + ' ' + params + hash;
130 };
131
132 PrintVisitor.prototype.PathExpression = function(id) {
133   let path = id.parts.join('/');
134   return (id.data ? '@' : '') + 'PATH:' + path;
135 };
136
137
138 PrintVisitor.prototype.StringLiteral = function(string) {
139   return '"' + string.value + '"';
140 };
141
142 PrintVisitor.prototype.NumberLiteral = function(number) {
143   return 'NUMBER{' + number.value + '}';
144 };
145
146 PrintVisitor.prototype.BooleanLiteral = function(bool) {
147   return 'BOOLEAN{' + bool.value + '}';
148 };
149
150 PrintVisitor.prototype.UndefinedLiteral = function() {
151   return 'UNDEFINED';
152 };
153
154 PrintVisitor.prototype.NullLiteral = function() {
155   return 'NULL';
156 };
157
158 PrintVisitor.prototype.Hash = function(hash) {
159   let pairs = hash.pairs,
160       joinedPairs = [];
161
162   for (let i = 0, l = pairs.length; i < l; i++) {
163     joinedPairs.push(this.accept(pairs[i]));
164   }
165
166   return 'HASH{' + joinedPairs.join(', ') + '}';
167 };
168 PrintVisitor.prototype.HashPair = function(pair) {
169   return pair.key + '=' + this.accept(pair.value);
170 };
171 /* eslint-enable new-cap */