Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
/***********************************************************************
2
3
A JavaScript tokenizer / parser / beautifier / compressor.
4
https://github.com/mishoo/UglifyJS2
5
6
-------------------------------- (C) ---------------------------------
7
8
Author: Mihai Bazon
9
<[email protected]>
10
http://mihai.bazon.net/blog
11
12
Distributed under the BSD license:
13
14
Copyright 2012 (c) Mihai Bazon <[email protected]>
15
16
Redistribution and use in source and binary forms, with or without
17
modification, are permitted provided that the following conditions
18
are met:
19
20
* Redistributions of source code must retain the above
21
copyright notice, this list of conditions and the following
22
disclaimer.
23
24
* Redistributions in binary form must reproduce the above
25
copyright notice, this list of conditions and the following
26
disclaimer in the documentation and/or other materials
27
provided with the distribution.
28
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
30
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
33
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
34
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
39
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
SUCH DAMAGE.
41
42
***********************************************************************/
43
44
"use strict";
45
46
// Tree transformer helpers.
47
48
function TreeTransformer(before, after) {
49
TreeWalker.call(this);
50
this.before = before;
51
this.after = after;
52
}
53
TreeTransformer.prototype = new TreeWalker;
54
55
(function(undefined){
56
57
function _(node, descend) {
58
node.DEFMETHOD("transform", function(tw, in_list){
59
var x, y;
60
tw.push(this);
61
if (tw.before) x = tw.before(this, descend, in_list);
62
if (x === undefined) {
63
if (!tw.after) {
64
x = this;
65
descend(x, tw);
66
} else {
67
tw.stack[tw.stack.length - 1] = x = this.clone();
68
descend(x, tw);
69
y = tw.after(x, in_list);
70
if (y !== undefined) x = y;
71
}
72
}
73
tw.pop();
74
return x;
75
});
76
};
77
78
function do_list(list, tw) {
79
return MAP(list, function(node){
80
return node.transform(tw, true);
81
});
82
};
83
84
_(AST_Node, noop);
85
86
_(AST_LabeledStatement, function(self, tw){
87
self.label = self.label.transform(tw);
88
self.body = self.body.transform(tw);
89
});
90
91
_(AST_SimpleStatement, function(self, tw){
92
self.body = self.body.transform(tw);
93
});
94
95
_(AST_Block, function(self, tw){
96
self.body = do_list(self.body, tw);
97
});
98
99
_(AST_DWLoop, function(self, tw){
100
self.condition = self.condition.transform(tw);
101
self.body = self.body.transform(tw);
102
});
103
104
_(AST_For, function(self, tw){
105
if (self.init) self.init = self.init.transform(tw);
106
if (self.condition) self.condition = self.condition.transform(tw);
107
if (self.step) self.step = self.step.transform(tw);
108
self.body = self.body.transform(tw);
109
});
110
111
_(AST_ForIn, function(self, tw){
112
self.init = self.init.transform(tw);
113
self.object = self.object.transform(tw);
114
self.body = self.body.transform(tw);
115
});
116
117
_(AST_With, function(self, tw){
118
self.expression = self.expression.transform(tw);
119
self.body = self.body.transform(tw);
120
});
121
122
_(AST_Exit, function(self, tw){
123
if (self.value) self.value = self.value.transform(tw);
124
});
125
126
_(AST_LoopControl, function(self, tw){
127
if (self.label) self.label = self.label.transform(tw);
128
});
129
130
_(AST_If, function(self, tw){
131
self.condition = self.condition.transform(tw);
132
self.body = self.body.transform(tw);
133
if (self.alternative) self.alternative = self.alternative.transform(tw);
134
});
135
136
_(AST_Switch, function(self, tw){
137
self.expression = self.expression.transform(tw);
138
self.body = do_list(self.body, tw);
139
});
140
141
_(AST_Case, function(self, tw){
142
self.expression = self.expression.transform(tw);
143
self.body = do_list(self.body, tw);
144
});
145
146
_(AST_Try, function(self, tw){
147
self.body = do_list(self.body, tw);
148
if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
149
if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
150
});
151
152
_(AST_Catch, function(self, tw){
153
self.argname = self.argname.transform(tw);
154
self.body = do_list(self.body, tw);
155
});
156
157
_(AST_Definitions, function(self, tw){
158
self.definitions = do_list(self.definitions, tw);
159
});
160
161
_(AST_VarDef, function(self, tw){
162
if (self.value) self.value = self.value.transform(tw);
163
});
164
165
_(AST_Lambda, function(self, tw){
166
if (self.name) self.name = self.name.transform(tw);
167
self.argnames = do_list(self.argnames, tw);
168
self.body = do_list(self.body, tw);
169
});
170
171
_(AST_Call, function(self, tw){
172
self.expression = self.expression.transform(tw);
173
self.args = do_list(self.args, tw);
174
});
175
176
_(AST_Seq, function(self, tw){
177
self.car = self.car.transform(tw);
178
self.cdr = self.cdr.transform(tw);
179
});
180
181
_(AST_Dot, function(self, tw){
182
self.expression = self.expression.transform(tw);
183
});
184
185
_(AST_Sub, function(self, tw){
186
self.expression = self.expression.transform(tw);
187
self.property = self.property.transform(tw);
188
});
189
190
_(AST_Unary, function(self, tw){
191
self.expression = self.expression.transform(tw);
192
});
193
194
_(AST_Binary, function(self, tw){
195
self.left = self.left.transform(tw);
196
self.right = self.right.transform(tw);
197
});
198
199
_(AST_Conditional, function(self, tw){
200
self.condition = self.condition.transform(tw);
201
self.consequent = self.consequent.transform(tw);
202
self.alternative = self.alternative.transform(tw);
203
});
204
205
_(AST_Array, function(self, tw){
206
self.elements = do_list(self.elements, tw);
207
});
208
209
_(AST_Object, function(self, tw){
210
self.properties = do_list(self.properties, tw);
211
});
212
213
_(AST_ObjectProperty, function(self, tw){
214
self.value = self.value.transform(tw);
215
});
216
217
})();
218
219