Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / handlebars / lib / handlebars / compiler / visitor.js
1 import Exception from '../exception';
2
3 function Visitor() {
4   this.parents = [];
5 }
6
7 Visitor.prototype = {
8   constructor: Visitor,
9   mutating: false,
10
11   // Visits a given value. If mutating, will replace the value if necessary.
12   acceptKey: function(node, name) {
13     let value = this.accept(node[name]);
14     if (this.mutating) {
15       // Hacky sanity check: This may have a few false positives for type for the helper
16       // methods but will generally do the right thing without a lot of overhead.
17       if (value && !Visitor.prototype[value.type]) {
18         throw new Exception('Unexpected node type "' + value.type + '" found when accepting ' + name + ' on ' + node.type);
19       }
20       node[name] = value;
21     }
22   },
23
24   // Performs an accept operation with added sanity check to ensure
25   // required keys are not removed.
26   acceptRequired: function(node, name) {
27     this.acceptKey(node, name);
28
29     if (!node[name]) {
30       throw new Exception(node.type + ' requires ' + name);
31     }
32   },
33
34   // Traverses a given array. If mutating, empty respnses will be removed
35   // for child elements.
36   acceptArray: function(array) {
37     for (let i = 0, l = array.length; i < l; i++) {
38       this.acceptKey(array, i);
39
40       if (!array[i]) {
41         array.splice(i, 1);
42         i--;
43         l--;
44       }
45     }
46   },
47
48   accept: function(object) {
49     if (!object) {
50       return;
51     }
52
53     /* istanbul ignore next: Sanity code */
54     if (!this[object.type]) {
55       throw new Exception('Unknown type: ' + object.type, object);
56     }
57
58     if (this.current) {
59       this.parents.unshift(this.current);
60     }
61     this.current = object;
62
63     let ret = this[object.type](object);
64
65     this.current = this.parents.shift();
66
67     if (!this.mutating || ret) {
68       return ret;
69     } else if (ret !== false) {
70       return object;
71     }
72   },
73
74   Program: function(program) {
75     this.acceptArray(program.body);
76   },
77
78   MustacheStatement: visitSubExpression,
79   Decorator: visitSubExpression,
80
81   BlockStatement: visitBlock,
82   DecoratorBlock: visitBlock,
83
84   PartialStatement: visitPartial,
85   PartialBlockStatement: function(partial) {
86     visitPartial.call(this, partial);
87
88     this.acceptKey(partial, 'program');
89   },
90
91   ContentStatement: function(/* content */) {},
92   CommentStatement: function(/* comment */) {},
93
94   SubExpression: visitSubExpression,
95
96   PathExpression: function(/* path */) {},
97
98   StringLiteral: function(/* string */) {},
99   NumberLiteral: function(/* number */) {},
100   BooleanLiteral: function(/* bool */) {},
101   UndefinedLiteral: function(/* literal */) {},
102   NullLiteral: function(/* literal */) {},
103
104   Hash: function(hash) {
105     this.acceptArray(hash.pairs);
106   },
107   HashPair: function(pair) {
108     this.acceptRequired(pair, 'value');
109   }
110 };
111
112 function visitSubExpression(mustache) {
113   this.acceptRequired(mustache, 'path');
114   this.acceptArray(mustache.params);
115   this.acceptKey(mustache, 'hash');
116 }
117 function visitBlock(block) {
118   visitSubExpression.call(this, block);
119
120   this.acceptKey(block, 'program');
121   this.acceptKey(block, 'inverse');
122 }
123 function visitPartial(partial) {
124   this.acceptRequired(partial, 'name');
125   this.acceptArray(partial.params);
126   this.acceptKey(partial, 'hash');
127 }
128
129 export default Visitor;