Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / handlebars / lib / handlebars / compiler / code-gen.js
1 /* global define */
2 import {isArray} from '../utils';
3
4 let SourceNode;
5
6 try {
7   /* istanbul ignore next */
8   if (typeof define !== 'function' || !define.amd) {
9     // We don't support this in AMD environments. For these environments, we asusme that
10     // they are running on the browser and thus have no need for the source-map library.
11     let SourceMap = require('source-map');
12     SourceNode = SourceMap.SourceNode;
13   }
14 } catch (err) {
15   /* NOP */
16 }
17
18 /* istanbul ignore if: tested but not covered in istanbul due to dist build  */
19 if (!SourceNode) {
20   SourceNode = function(line, column, srcFile, chunks) {
21     this.src = '';
22     if (chunks) {
23       this.add(chunks);
24     }
25   };
26   /* istanbul ignore next */
27   SourceNode.prototype = {
28     add: function(chunks) {
29       if (isArray(chunks)) {
30         chunks = chunks.join('');
31       }
32       this.src += chunks;
33     },
34     prepend: function(chunks) {
35       if (isArray(chunks)) {
36         chunks = chunks.join('');
37       }
38       this.src = chunks + this.src;
39     },
40     toStringWithSourceMap: function() {
41       return {code: this.toString()};
42     },
43     toString: function() {
44       return this.src;
45     }
46   };
47 }
48
49
50 function castChunk(chunk, codeGen, loc) {
51   if (isArray(chunk)) {
52     let ret = [];
53
54     for (let i = 0, len = chunk.length; i < len; i++) {
55       ret.push(codeGen.wrap(chunk[i], loc));
56     }
57     return ret;
58   } else if (typeof chunk === 'boolean' || typeof chunk === 'number') {
59     // Handle primitives that the SourceNode will throw up on
60     return chunk + '';
61   }
62   return chunk;
63 }
64
65
66 function CodeGen(srcFile) {
67   this.srcFile = srcFile;
68   this.source = [];
69 }
70
71 CodeGen.prototype = {
72   isEmpty() {
73     return !this.source.length;
74   },
75   prepend: function(source, loc) {
76     this.source.unshift(this.wrap(source, loc));
77   },
78   push: function(source, loc) {
79     this.source.push(this.wrap(source, loc));
80   },
81
82   merge: function() {
83     let source = this.empty();
84     this.each(function(line) {
85       source.add(['  ', line, '\n']);
86     });
87     return source;
88   },
89
90   each: function(iter) {
91     for (let i = 0, len = this.source.length; i < len; i++) {
92       iter(this.source[i]);
93     }
94   },
95
96   empty: function() {
97     let loc = this.currentLocation || {start: {}};
98     return new SourceNode(loc.start.line, loc.start.column, this.srcFile);
99   },
100   wrap: function(chunk, loc = this.currentLocation || {start: {}}) {
101     if (chunk instanceof SourceNode) {
102       return chunk;
103     }
104
105     chunk = castChunk(chunk, this, loc);
106
107     return new SourceNode(loc.start.line, loc.start.column, this.srcFile, chunk);
108   },
109
110   functionCall: function(fn, type, params) {
111     params = this.generateList(params);
112     return this.wrap([fn, type ? '.' + type + '(' : '(', params, ')']);
113   },
114
115   quotedString: function(str) {
116     return '"' + (str + '')
117       .replace(/\\/g, '\\\\')
118       .replace(/"/g, '\\"')
119       .replace(/\n/g, '\\n')
120       .replace(/\r/g, '\\r')
121       .replace(/\u2028/g, '\\u2028')   // Per Ecma-262 7.3 + 7.8.4
122       .replace(/\u2029/g, '\\u2029') + '"';
123   },
124
125   objectLiteral: function(obj) {
126     let pairs = [];
127
128     for (let key in obj) {
129       if (obj.hasOwnProperty(key)) {
130         let value = castChunk(obj[key], this);
131         if (value !== 'undefined') {
132           pairs.push([this.quotedString(key), ':', value]);
133         }
134       }
135     }
136
137     let ret = this.generateList(pairs);
138     ret.prepend('{');
139     ret.add('}');
140     return ret;
141   },
142
143
144   generateList: function(entries) {
145     let ret = this.empty();
146
147     for (let i = 0, len = entries.length; i < len; i++) {
148       if (i) {
149         ret.add(',');
150       }
151
152       ret.add(castChunk(entries[i], this));
153     }
154
155     return ret;
156   },
157
158   generateArray: function(entries) {
159     let ret = this.generateList(entries);
160     ret.prepend('[');
161     ret.add(']');
162
163     return ret;
164   }
165 };
166
167 export default CodeGen;
168