uglifyjs与AST
Updated:
UglifyJS 提供了一些工具用于分析JavaScript代码,包括:
- parser,把 JavaScript 代码解析成抽象语法树
- code generator,通过抽象语法树生成代码
- compressor, 压缩 JavaScript代码
- mangler,混淆 JavaScript 代码
- scope analyzer,分析变量定义的工具
- tree walker,遍历树节点
- tree transformer,改变树节点
parser生成抽象语法树
UglifyJS.parse可将code解析成抽象语法树1
2var toplevel = UglifyJS.parse(code);
console.log(JSON.stringify(toplevel.body[0]));
将下面代码进行parse操作:1
2var key = "Hello";
console.log(key);
得到的结果为:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270[
{
"end":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":18,
"endcol":18,
"endline":1,
"pos":17,
"col":17,
"line":1,
"value":";",
"type":"punc"
},
"start":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":3,
"endcol":3,
"endline":1,
"pos":0,
"col":0,
"line":1,
"value":"var",
"type":"keyword"
},
"definitions":[
{
"end":{
"raw":""Hello"",
"file":null,
"comments_before":[],
"nlb":false,
"endpos":17,
"endcol":17,
"endline":1,
"pos":10,
"col":10,
"line":1,
"value":"Hello",
"type":"string",
"quote":"""
},
"start":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":7,
"endcol":7,
"endline":1,
"pos":4,
"col":4,
"line":1,
"value":"key",
"type":"name"
},
"value":{
"end":{
"raw":""Hello"",
"file":null,
"comments_before":[],
"nlb":false,
"endpos":17,
"endcol":17,
"endline":1,
"pos":10,
"col":10,
"line":1,
"value":"Hello",
"type":"string",
"quote":"""
},
"start":{
"raw":""Hello"",
"file":null,
"comments_before":[],
"nlb":false,
"endpos":17,
"endcol":17,
"endline":1,
"pos":10,
"col":10,
"line":1,
"value":"Hello",
"type":"string",
"quote":"""
},
"quote":""",
"value":"Hello"
},
"name":{
"end":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":7,
"endcol":7,
"endline":1,
"pos":4,
"col":4,
"line":1,
"value":"key",
"type":"name"
},
"start":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":7,
"endcol":7,
"endline":1,
"pos":4,
"col":4,
"line":1,
"value":"key",
"type":"name"
},
"name":"key"
}
}
]
},
{
"end":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":37,
"endcol":17,
"endline":2,
"pos":36,
"col":16,
"line":2,
"value":";",
"type":"punc"
},
"start":{
"file":null,
"comments_before":[],
"nlb":true,
"endpos":27,
"endcol":7,
"endline":2,
"pos":20,
"col":0,
"line":2,
"value":"console",
"type":"name"
},
"body":{
"end":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":36,
"endcol":16,
"endline":2,
"pos":35,
"col":15,
"line":2,
"value":")",
"type":"punc"
},
"start":{
"file":null,
"comments_before":[],
"nlb":true,
"endpos":27,
"endcol":7,
"endline":2,
"pos":20,
"col":0,
"line":2,
"value":"console",
"type":"name"
},
"args":[
{
"end":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":35,
"endcol":15,
"endline":2,
"pos":32,
"col":12,
"line":2,
"value":"key",
"type":"name"
},
"start":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":35,
"endcol":15,
"endline":2,
"pos":32,
"col":12,
"line":2,
"value":"key",
"type":"name"
},
"name":"key"
}
],
"expression":{
"end":{
"file":null,
"comments_before":[],
"nlb":false,
"endpos":31,
"endcol":11,
"endline":2,
"pos":28,
"col":8,
"line":2,
"value":"log",
"type":"name"
},
"start":{
"file":null,
"comments_before":[],
"nlb":true,
"endpos":27,
"endcol":7,
"endline":2,
"pos":20,
"col":0,
"line":2,
"value":"console",
"type":"name"
},
"property":"log",
"expression":{
"end":{
"file":null,
"comments_before":[],
"nlb":true,
"endpos":27,
"endcol":7,
"endline":2,
"pos":20,
"col":0,
"line":2,
"value":"console",
"type":"name"
},
"start":{
"file":null,
"comments_before":[],
"nlb":true,
"endpos":27,
"endcol":7,
"endline":2,
"pos":20,
"col":0,
"line":2,
"value":"console",
"type":"name"
},
"name":"console"
}
}
}
}
]
transformer的用法
1 | function before(node, descend) { ... }; |
“before” visitor
在树中的每一个节点, before visitor在遍历子节点之前被调用。它接受2个参数,现在的节点和子函数。
- before visitorf返回undefined:
如果后续访问者存在,遍历树会复制当前节点并进入处理子节点然后调用after visitor,
如果没有after visitor, 它将直接处理当前节点。 - 如果before visitor 返回了一个值,则当前节点会被这个值替换。它的子节点不会被处理,after visitor也不会被调用。
“after” visitor
如果你使用了after visitor, 它只有在before visitor返回undefined,节点被复制的时候才会被调用。如果after visitor返回的不是undefined,则现节点的值将被这个返回值覆盖。