Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81169 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
// XXX: eventually I should refactor the compressor to use this infrastructure.
48
49
function TreeTransformer(before, after) {
50
TreeWalker.call(this);
51
this.before = before;
52
this.after = after;
53
}
54
TreeTransformer.prototype = new TreeWalker;
55
56
(function(undefined){
57
58
function _(node, descend) {
59
node.DEFMETHOD("transform", function(tw, in_list){
60
var x, y;
61
tw.push(this);
62
if (tw.before) x = tw.before(this, descend, in_list);
63
if (x === undefined) {
64
if (!tw.after) {
65
x = this;
66
descend(x, tw);
67
} else {
68
tw.stack[tw.stack.length - 1] = x = this.clone();
69
descend(x, tw);
70
y = tw.after(x, in_list);
71
if (y !== undefined) x = y;
72
}
73
}
74
tw.pop();
75
return x;
76
});
77
};
78
79
function do_list(list, tw) {
80
return MAP(list, function(node){
81
return node.transform(tw, true);
82
});
83
};
84
85
_(AST_Node, noop);
86
87
_(AST_LabeledStatement, function(self, tw){
88
self.label = self.label.transform(tw);
89
self.body = self.body.transform(tw);
90
});
91
92
_(AST_SimpleStatement, function(self, tw){
93
self.body = self.body.transform(tw);
94
});
95
96
_(AST_Block, function(self, tw){
97
self.body = do_list(self.body, tw);
98
});
99
100
_(AST_DWLoop, function(self, tw){
101
self.condition = self.condition.transform(tw);
102
self.body = self.body.transform(tw);
103
});
104
105
_(AST_For, function(self, tw){
106
if (self.init) self.init = self.init.transform(tw);
107
if (self.condition) self.condition = self.condition.transform(tw);
108
if (self.step) self.step = self.step.transform(tw);
109
self.body = self.body.transform(tw);
110
});
111
112
_(AST_ForIn, function(self, tw){
113
self.init = self.init.transform(tw);
114
self.object = self.object.transform(tw);
115
self.body = self.body.transform(tw);
116
});
117
118
_(AST_With, function(self, tw){
119
self.expression = self.expression.transform(tw);
120
self.body = self.body.transform(tw);
121
});
122
123
_(AST_Exit, function(self, tw){
124
if (self.value) self.value = self.value.transform(tw);
125
});
126
127
_(AST_LoopControl, function(self, tw){
128
if (self.label) self.label = self.label.transform(tw);
129
});
130
131
_(AST_If, function(self, tw){
132
self.condition = self.condition.transform(tw);
133
self.body = self.body.transform(tw);
134
if (self.alternative) self.alternative = self.alternative.transform(tw);
135
});
136
137
_(AST_Switch, function(self, tw){
138
self.expression = self.expression.transform(tw);
139
self.body = do_list(self.body, tw);
140
});
141
142
_(AST_Case, function(self, tw){
143
self.expression = self.expression.transform(tw);
144
self.body = do_list(self.body, tw);
145
});
146
147
_(AST_Try, function(self, tw){
148
self.body = do_list(self.body, tw);
149
if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
150
if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
151
});
152
153
_(AST_Catch, function(self, tw){
154
self.argname = self.argname.transform(tw);
155
self.body = do_list(self.body, tw);
156
});
157
158
_(AST_Definitions, function(self, tw){
159
self.definitions = do_list(self.definitions, tw);
160
});
161
162
_(AST_VarDef, function(self, tw){
163
if (self.value) self.value = self.value.transform(tw);
164
});
165
166
_(AST_Lambda, function(self, tw){
167
if (self.name) self.name = self.name.transform(tw);
168
self.argnames = do_list(self.argnames, tw);
169
self.body = do_list(self.body, tw);
170
});
171
172
_(AST_Call, function(self, tw){
173
self.expression = self.expression.transform(tw);
174
self.args = do_list(self.args, tw);
175
});
176
177
_(AST_Seq, function(self, tw){
178
self.car = self.car.transform(tw);
179
self.cdr = self.cdr.transform(tw);
180
});
181
182
_(AST_Dot, function(self, tw){
183
self.expression = self.expression.transform(tw);
184
});
185
186
_(AST_Sub, function(self, tw){
187
self.expression = self.expression.transform(tw);
188
self.property = self.property.transform(tw);
189
});
190
191
_(AST_Unary, function(self, tw){
192
self.expression = self.expression.transform(tw);
193
});
194
195
_(AST_Binary, function(self, tw){
196
self.left = self.left.transform(tw);
197
self.right = self.right.transform(tw);
198
});
199
200
_(AST_Conditional, function(self, tw){
201
self.condition = self.condition.transform(tw);
202
self.consequent = self.consequent.transform(tw);
203
self.alternative = self.alternative.transform(tw);
204
});
205
206
_(AST_Array, function(self, tw){
207
self.elements = do_list(self.elements, tw);
208
});
209
210
_(AST_Object, function(self, tw){
211
self.properties = do_list(self.properties, tw);
212
});
213
214
_(AST_ObjectProperty, function(self, tw){
215
self.value = self.value.transform(tw);
216
});
217
218
})();
219
220