assets_booster 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README +0 -0
- data/Rakefile +2 -0
- data/asset_booster.gemspec +21 -0
- data/lib/assets_booster.rb +7 -0
- data/lib/assets_booster/compiler/closure.rb +36 -0
- data/lib/assets_booster/compiler/dummy.rb +17 -0
- data/lib/assets_booster/compiler/jsmin.rb +234 -0
- data/lib/assets_booster/compiler/node-js/parse-js.js +1254 -0
- data/lib/assets_booster/compiler/node-js/process.js +1602 -0
- data/lib/assets_booster/compiler/node-js/squeeze-more.js +22 -0
- data/lib/assets_booster/compiler/node-js/uglify.js +203 -0
- data/lib/assets_booster/compiler/rainpress.rb +17 -0
- data/lib/assets_booster/compiler/uglify.rb +19 -0
- data/lib/assets_booster/configuration.rb +104 -0
- data/lib/assets_booster/merger/css.rb +79 -0
- data/lib/assets_booster/merger/simple.rb +22 -0
- data/lib/assets_booster/package/base.rb +71 -0
- data/lib/assets_booster/package/javascript.rb +24 -0
- data/lib/assets_booster/package/stylesheet.rb +24 -0
- data/lib/assets_booster/packager.rb +33 -0
- data/lib/assets_booster/railtie.rb +19 -0
- data/lib/assets_booster/tasks/tasks.rake +29 -0
- data/lib/assets_booster/version.rb +3 -0
- data/lib/assets_booster/view_helper.rb +17 -0
- metadata +93 -0
@@ -0,0 +1,1602 @@
|
|
1
|
+
/***********************************************************************
|
2
|
+
|
3
|
+
A JavaScript tokenizer / parser / beautifier / compressor.
|
4
|
+
|
5
|
+
This version is suitable for Node.js. With minimal changes (the
|
6
|
+
exports stuff) it should work on any JS platform.
|
7
|
+
|
8
|
+
This file implements some AST processors. They work on data built
|
9
|
+
by parse-js.
|
10
|
+
|
11
|
+
Exported functions:
|
12
|
+
|
13
|
+
- ast_mangle(ast, include_toplevel) -- mangles the
|
14
|
+
variable/function names in the AST. Returns an AST. Pass true
|
15
|
+
as second argument to mangle toplevel names too.
|
16
|
+
|
17
|
+
- ast_squeeze(ast) -- employs various optimizations to make the
|
18
|
+
final generated code even smaller. Returns an AST.
|
19
|
+
|
20
|
+
- gen_code(ast, beautify) -- generates JS code from the AST. Pass
|
21
|
+
true (or an object, see the code for some options) as second
|
22
|
+
argument to get "pretty" (indented) code.
|
23
|
+
|
24
|
+
-------------------------------- (C) ---------------------------------
|
25
|
+
|
26
|
+
Author: Mihai Bazon
|
27
|
+
<mihai.bazon@gmail.com>
|
28
|
+
http://mihai.bazon.net/blog
|
29
|
+
|
30
|
+
Distributed under the BSD license:
|
31
|
+
|
32
|
+
Copyright 2010 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
33
|
+
|
34
|
+
Redistribution and use in source and binary forms, with or without
|
35
|
+
modification, are permitted provided that the following conditions
|
36
|
+
are met:
|
37
|
+
|
38
|
+
* Redistributions of source code must retain the above
|
39
|
+
copyright notice, this list of conditions and the following
|
40
|
+
disclaimer.
|
41
|
+
|
42
|
+
* Redistributions in binary form must reproduce the above
|
43
|
+
copyright notice, this list of conditions and the following
|
44
|
+
disclaimer in the documentation and/or other materials
|
45
|
+
provided with the distribution.
|
46
|
+
|
47
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
|
48
|
+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
49
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
50
|
+
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
|
51
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
52
|
+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
53
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
54
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
55
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
56
|
+
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
57
|
+
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
58
|
+
SUCH DAMAGE.
|
59
|
+
|
60
|
+
***********************************************************************/
|
61
|
+
|
62
|
+
var jsp = require("./parse-js"),
|
63
|
+
slice = jsp.slice,
|
64
|
+
member = jsp.member,
|
65
|
+
PRECEDENCE = jsp.PRECEDENCE,
|
66
|
+
OPERATORS = jsp.OPERATORS;
|
67
|
+
|
68
|
+
/* -----[ helper for AST traversal ]----- */
|
69
|
+
|
70
|
+
function ast_walker(ast) {
|
71
|
+
function _vardefs(defs) {
|
72
|
+
return [ this[0], MAP(defs, function(def){
|
73
|
+
var a = [ def[0] ];
|
74
|
+
if (def.length > 1)
|
75
|
+
a[1] = walk(def[1]);
|
76
|
+
return a;
|
77
|
+
}) ];
|
78
|
+
};
|
79
|
+
var walkers = {
|
80
|
+
"string": function(str) {
|
81
|
+
return [ this[0], str ];
|
82
|
+
},
|
83
|
+
"num": function(num) {
|
84
|
+
return [ this[0], num ];
|
85
|
+
},
|
86
|
+
"name": function(name) {
|
87
|
+
return [ this[0], name ];
|
88
|
+
},
|
89
|
+
"toplevel": function(statements) {
|
90
|
+
return [ this[0], MAP(statements, walk) ];
|
91
|
+
},
|
92
|
+
"block": function(statements) {
|
93
|
+
var out = [ this[0] ];
|
94
|
+
if (statements != null)
|
95
|
+
out.push(MAP(statements, walk));
|
96
|
+
return out;
|
97
|
+
},
|
98
|
+
"var": _vardefs,
|
99
|
+
"const": _vardefs,
|
100
|
+
"try": function(t, c, f) {
|
101
|
+
return [
|
102
|
+
this[0],
|
103
|
+
MAP(t, walk),
|
104
|
+
c != null ? [ c[0], MAP(c[1], walk) ] : null,
|
105
|
+
f != null ? MAP(f, walk) : null
|
106
|
+
];
|
107
|
+
},
|
108
|
+
"throw": function(expr) {
|
109
|
+
return [ this[0], walk(expr) ];
|
110
|
+
},
|
111
|
+
"new": function(ctor, args) {
|
112
|
+
return [ this[0], walk(ctor), MAP(args, walk) ];
|
113
|
+
},
|
114
|
+
"switch": function(expr, body) {
|
115
|
+
return [ this[0], walk(expr), MAP(body, function(branch){
|
116
|
+
return [ branch[0] ? walk(branch[0]) : null,
|
117
|
+
MAP(branch[1], walk) ];
|
118
|
+
}) ];
|
119
|
+
},
|
120
|
+
"break": function(label) {
|
121
|
+
return [ this[0], label ];
|
122
|
+
},
|
123
|
+
"continue": function(label) {
|
124
|
+
return [ this[0], label ];
|
125
|
+
},
|
126
|
+
"conditional": function(cond, t, e) {
|
127
|
+
return [ this[0], walk(cond), walk(t), walk(e) ];
|
128
|
+
},
|
129
|
+
"assign": function(op, lvalue, rvalue) {
|
130
|
+
return [ this[0], op, walk(lvalue), walk(rvalue) ];
|
131
|
+
},
|
132
|
+
"dot": function(expr) {
|
133
|
+
return [ this[0], walk(expr) ].concat(slice(arguments, 1));
|
134
|
+
},
|
135
|
+
"call": function(expr, args) {
|
136
|
+
return [ this[0], walk(expr), MAP(args, walk) ];
|
137
|
+
},
|
138
|
+
"function": function(name, args, body) {
|
139
|
+
return [ this[0], name, args.slice(), MAP(body, walk) ];
|
140
|
+
},
|
141
|
+
"defun": function(name, args, body) {
|
142
|
+
return [ this[0], name, args.slice(), MAP(body, walk) ];
|
143
|
+
},
|
144
|
+
"if": function(conditional, t, e) {
|
145
|
+
return [ this[0], walk(conditional), walk(t), walk(e) ];
|
146
|
+
},
|
147
|
+
"for": function(init, cond, step, block) {
|
148
|
+
return [ this[0], walk(init), walk(cond), walk(step), walk(block) ];
|
149
|
+
},
|
150
|
+
"for-in": function(has_var, key, hash, block) {
|
151
|
+
return [ this[0], has_var, key, walk(hash), walk(block) ];
|
152
|
+
},
|
153
|
+
"while": function(cond, block) {
|
154
|
+
return [ this[0], walk(cond), walk(block) ];
|
155
|
+
},
|
156
|
+
"do": function(cond, block) {
|
157
|
+
return [ this[0], walk(cond), walk(block) ];
|
158
|
+
},
|
159
|
+
"return": function(expr) {
|
160
|
+
return [ this[0], walk(expr) ];
|
161
|
+
},
|
162
|
+
"binary": function(op, left, right) {
|
163
|
+
return [ this[0], op, walk(left), walk(right) ];
|
164
|
+
},
|
165
|
+
"unary-prefix": function(op, expr) {
|
166
|
+
return [ this[0], op, walk(expr) ];
|
167
|
+
},
|
168
|
+
"unary-postfix": function(op, expr) {
|
169
|
+
return [ this[0], op, walk(expr) ];
|
170
|
+
},
|
171
|
+
"sub": function(expr, subscript) {
|
172
|
+
return [ this[0], walk(expr), walk(subscript) ];
|
173
|
+
},
|
174
|
+
"object": function(props) {
|
175
|
+
return [ this[0], MAP(props, function(p){
|
176
|
+
return p.length == 2
|
177
|
+
? [ p[0], walk(p[1]) ]
|
178
|
+
: [ p[0], walk(p[1]), p[2] ]; // get/set-ter
|
179
|
+
}) ];
|
180
|
+
},
|
181
|
+
"regexp": function(rx, mods) {
|
182
|
+
return [ this[0], rx, mods ];
|
183
|
+
},
|
184
|
+
"array": function(elements) {
|
185
|
+
return [ this[0], MAP(elements, walk) ];
|
186
|
+
},
|
187
|
+
"stat": function(stat) {
|
188
|
+
return [ this[0], walk(stat) ];
|
189
|
+
},
|
190
|
+
"seq": function() {
|
191
|
+
return [ this[0] ].concat(MAP(slice(arguments), walk));
|
192
|
+
},
|
193
|
+
"label": function(name, block) {
|
194
|
+
return [ this[0], name, walk(block) ];
|
195
|
+
},
|
196
|
+
"with": function(expr, block) {
|
197
|
+
return [ this[0], walk(expr), walk(block) ];
|
198
|
+
},
|
199
|
+
"atom": function(name) {
|
200
|
+
return [ this[0], name ];
|
201
|
+
}
|
202
|
+
};
|
203
|
+
|
204
|
+
var user = {};
|
205
|
+
var stack = [];
|
206
|
+
function walk(ast) {
|
207
|
+
if (ast == null)
|
208
|
+
return null;
|
209
|
+
try {
|
210
|
+
stack.push(ast);
|
211
|
+
var type = ast[0];
|
212
|
+
var gen = user[type];
|
213
|
+
if (gen) {
|
214
|
+
var ret = gen.apply(ast, ast.slice(1));
|
215
|
+
if (ret != null)
|
216
|
+
return ret;
|
217
|
+
}
|
218
|
+
gen = walkers[type];
|
219
|
+
return gen.apply(ast, ast.slice(1));
|
220
|
+
} finally {
|
221
|
+
stack.pop();
|
222
|
+
}
|
223
|
+
};
|
224
|
+
|
225
|
+
function with_walkers(walkers, cont){
|
226
|
+
var save = {}, i;
|
227
|
+
for (i in walkers) if (HOP(walkers, i)) {
|
228
|
+
save[i] = user[i];
|
229
|
+
user[i] = walkers[i];
|
230
|
+
}
|
231
|
+
var ret = cont();
|
232
|
+
for (i in save) if (HOP(save, i)) {
|
233
|
+
if (!save[i]) delete user[i];
|
234
|
+
else user[i] = save[i];
|
235
|
+
}
|
236
|
+
return ret;
|
237
|
+
};
|
238
|
+
|
239
|
+
return {
|
240
|
+
walk: walk,
|
241
|
+
with_walkers: with_walkers,
|
242
|
+
parent: function() {
|
243
|
+
return stack[stack.length - 2]; // last one is current node
|
244
|
+
},
|
245
|
+
stack: function() {
|
246
|
+
return stack;
|
247
|
+
}
|
248
|
+
};
|
249
|
+
};
|
250
|
+
|
251
|
+
/* -----[ Scope and mangling ]----- */
|
252
|
+
|
253
|
+
function Scope(parent) {
|
254
|
+
this.names = {}; // names defined in this scope
|
255
|
+
this.mangled = {}; // mangled names (orig.name => mangled)
|
256
|
+
this.rev_mangled = {}; // reverse lookup (mangled => orig.name)
|
257
|
+
this.cname = -1; // current mangled name
|
258
|
+
this.refs = {}; // names referenced from this scope
|
259
|
+
this.uses_with = false; // will become TRUE if eval() is detected in this or any subscopes
|
260
|
+
this.uses_eval = false; // will become TRUE if with() is detected in this or any subscopes
|
261
|
+
this.parent = parent; // parent scope
|
262
|
+
this.children = []; // sub-scopes
|
263
|
+
if (parent) {
|
264
|
+
this.level = parent.level + 1;
|
265
|
+
parent.children.push(this);
|
266
|
+
} else {
|
267
|
+
this.level = 0;
|
268
|
+
}
|
269
|
+
};
|
270
|
+
|
271
|
+
var base54 = (function(){
|
272
|
+
var DIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
|
273
|
+
return function(num) {
|
274
|
+
var ret = "";
|
275
|
+
do {
|
276
|
+
ret = DIGITS.charAt(num % 54) + ret;
|
277
|
+
num = Math.floor(num / 54);
|
278
|
+
} while (num > 0);
|
279
|
+
return ret;
|
280
|
+
};
|
281
|
+
})();
|
282
|
+
|
283
|
+
Scope.prototype = {
|
284
|
+
has: function(name) {
|
285
|
+
for (var s = this; s; s = s.parent)
|
286
|
+
if (HOP(s.names, name))
|
287
|
+
return s;
|
288
|
+
},
|
289
|
+
has_mangled: function(mname) {
|
290
|
+
for (var s = this; s; s = s.parent)
|
291
|
+
if (HOP(s.rev_mangled, mname))
|
292
|
+
return s;
|
293
|
+
},
|
294
|
+
toJSON: function() {
|
295
|
+
return {
|
296
|
+
names: this.names,
|
297
|
+
uses_eval: this.uses_eval,
|
298
|
+
uses_with: this.uses_with
|
299
|
+
};
|
300
|
+
},
|
301
|
+
|
302
|
+
next_mangled: function() {
|
303
|
+
// we must be careful that the new mangled name:
|
304
|
+
//
|
305
|
+
// 1. doesn't shadow a mangled name from a parent
|
306
|
+
// scope, unless we don't reference the original
|
307
|
+
// name from this scope OR from any sub-scopes!
|
308
|
+
// This will get slow.
|
309
|
+
//
|
310
|
+
// 2. doesn't shadow an original name from a parent
|
311
|
+
// scope, in the event that the name is not mangled
|
312
|
+
// in the parent scope and we reference that name
|
313
|
+
// here OR IN ANY SUBSCOPES!
|
314
|
+
//
|
315
|
+
// 3. doesn't shadow a name that is referenced but not
|
316
|
+
// defined (possibly global defined elsewhere).
|
317
|
+
for (;;) {
|
318
|
+
var m = base54(++this.cname), prior;
|
319
|
+
|
320
|
+
// case 1.
|
321
|
+
prior = this.has_mangled(m);
|
322
|
+
if (prior && this.refs[prior.rev_mangled[m]] === prior)
|
323
|
+
continue;
|
324
|
+
|
325
|
+
// case 2.
|
326
|
+
prior = this.has(m);
|
327
|
+
if (prior && prior !== this && this.refs[m] === prior && !prior.has_mangled(m))
|
328
|
+
continue;
|
329
|
+
|
330
|
+
// case 3.
|
331
|
+
if (HOP(this.refs, m) && this.refs[m] == null)
|
332
|
+
continue;
|
333
|
+
|
334
|
+
// I got "do" once. :-/
|
335
|
+
if (!is_identifier(m))
|
336
|
+
continue;
|
337
|
+
|
338
|
+
return m;
|
339
|
+
}
|
340
|
+
},
|
341
|
+
get_mangled: function(name, newMangle) {
|
342
|
+
if (this.uses_eval || this.uses_with) return name; // no mangle if eval or with is in use
|
343
|
+
var s = this.has(name);
|
344
|
+
if (!s) return name; // not in visible scope, no mangle
|
345
|
+
if (HOP(s.mangled, name)) return s.mangled[name]; // already mangled in this scope
|
346
|
+
if (!newMangle) return name; // not found and no mangling requested
|
347
|
+
|
348
|
+
var m = s.next_mangled();
|
349
|
+
s.rev_mangled[m] = name;
|
350
|
+
return s.mangled[name] = m;
|
351
|
+
},
|
352
|
+
define: function(name) {
|
353
|
+
if (name != null)
|
354
|
+
return this.names[name] = name;
|
355
|
+
}
|
356
|
+
};
|
357
|
+
|
358
|
+
function ast_add_scope(ast) {
|
359
|
+
|
360
|
+
var current_scope = null;
|
361
|
+
var w = ast_walker(), walk = w.walk;
|
362
|
+
var having_eval = [];
|
363
|
+
|
364
|
+
function with_new_scope(cont) {
|
365
|
+
current_scope = new Scope(current_scope);
|
366
|
+
var ret = current_scope.body = cont();
|
367
|
+
ret.scope = current_scope;
|
368
|
+
current_scope = current_scope.parent;
|
369
|
+
return ret;
|
370
|
+
};
|
371
|
+
|
372
|
+
function define(name) {
|
373
|
+
return current_scope.define(name);
|
374
|
+
};
|
375
|
+
|
376
|
+
function reference(name) {
|
377
|
+
current_scope.refs[name] = true;
|
378
|
+
};
|
379
|
+
|
380
|
+
function _lambda(name, args, body) {
|
381
|
+
return [ this[0], define(name), args, with_new_scope(function(){
|
382
|
+
MAP(args, define);
|
383
|
+
return MAP(body, walk);
|
384
|
+
})];
|
385
|
+
};
|
386
|
+
|
387
|
+
return with_new_scope(function(){
|
388
|
+
// process AST
|
389
|
+
var ret = w.with_walkers({
|
390
|
+
"function": _lambda,
|
391
|
+
"defun": _lambda,
|
392
|
+
"with": function(expr, block) {
|
393
|
+
for (var s = current_scope; s; s = s.parent)
|
394
|
+
s.uses_with = true;
|
395
|
+
},
|
396
|
+
"var": function(defs) {
|
397
|
+
MAP(defs, function(d){ define(d[0]) });
|
398
|
+
},
|
399
|
+
"const": function(defs) {
|
400
|
+
MAP(defs, function(d){ define(d[0]) });
|
401
|
+
},
|
402
|
+
"try": function(t, c, f) {
|
403
|
+
if (c != null) return [
|
404
|
+
this[0],
|
405
|
+
MAP(t, walk),
|
406
|
+
[ define(c[0]), MAP(c[1], walk) ],
|
407
|
+
f != null ? MAP(f, walk) : null
|
408
|
+
];
|
409
|
+
},
|
410
|
+
"name": function(name) {
|
411
|
+
if (name == "eval")
|
412
|
+
having_eval.push(current_scope);
|
413
|
+
reference(name);
|
414
|
+
},
|
415
|
+
"for-in": function(has_var, name) {
|
416
|
+
if (has_var) define(name);
|
417
|
+
else reference(name);
|
418
|
+
}
|
419
|
+
}, function(){
|
420
|
+
return walk(ast);
|
421
|
+
});
|
422
|
+
|
423
|
+
// the reason why we need an additional pass here is
|
424
|
+
// that names can be used prior to their definition.
|
425
|
+
|
426
|
+
// scopes where eval was detected and their parents
|
427
|
+
// are marked with uses_eval, unless they define the
|
428
|
+
// "eval" name.
|
429
|
+
MAP(having_eval, function(scope){
|
430
|
+
if (!scope.has("eval")) while (scope) {
|
431
|
+
scope.uses_eval = true;
|
432
|
+
scope = scope.parent;
|
433
|
+
}
|
434
|
+
});
|
435
|
+
|
436
|
+
// for referenced names it might be useful to know
|
437
|
+
// their origin scope. current_scope here is the
|
438
|
+
// toplevel one.
|
439
|
+
function fixrefs(scope, i) {
|
440
|
+
// do children first; order shouldn't matter
|
441
|
+
for (i = scope.children.length; --i >= 0;)
|
442
|
+
fixrefs(scope.children[i]);
|
443
|
+
for (i in scope.refs) if (HOP(scope.refs, i)) {
|
444
|
+
// find origin scope and propagate the reference to origin
|
445
|
+
for (var origin = scope.has(i), s = scope; s; s = s.parent) {
|
446
|
+
s.refs[i] = origin;
|
447
|
+
if (s === origin) break;
|
448
|
+
}
|
449
|
+
}
|
450
|
+
};
|
451
|
+
fixrefs(current_scope);
|
452
|
+
|
453
|
+
return ret;
|
454
|
+
});
|
455
|
+
|
456
|
+
};
|
457
|
+
|
458
|
+
/* -----[ mangle names ]----- */
|
459
|
+
|
460
|
+
function ast_mangle(ast, options) {
|
461
|
+
var w = ast_walker(), walk = w.walk, scope;
|
462
|
+
options = options || {};
|
463
|
+
|
464
|
+
function get_mangled(name, newMangle) {
|
465
|
+
if (!options.toplevel && !scope.parent) return name; // don't mangle toplevel
|
466
|
+
if (options.except && member(name, options.except))
|
467
|
+
return name;
|
468
|
+
return scope.get_mangled(name, newMangle);
|
469
|
+
};
|
470
|
+
|
471
|
+
function _lambda(name, args, body) {
|
472
|
+
if (name) name = get_mangled(name);
|
473
|
+
body = with_scope(body.scope, function(){
|
474
|
+
args = MAP(args, function(name){ return get_mangled(name) });
|
475
|
+
return MAP(body, walk);
|
476
|
+
});
|
477
|
+
return [ this[0], name, args, body ];
|
478
|
+
};
|
479
|
+
|
480
|
+
function with_scope(s, cont) {
|
481
|
+
var _scope = scope;
|
482
|
+
scope = s;
|
483
|
+
for (var i in s.names) if (HOP(s.names, i)) {
|
484
|
+
get_mangled(i, true);
|
485
|
+
}
|
486
|
+
var ret = cont();
|
487
|
+
ret.scope = s;
|
488
|
+
scope = _scope;
|
489
|
+
return ret;
|
490
|
+
};
|
491
|
+
|
492
|
+
function _vardefs(defs) {
|
493
|
+
return [ this[0], MAP(defs, function(d){
|
494
|
+
return [ get_mangled(d[0]), walk(d[1]) ];
|
495
|
+
}) ];
|
496
|
+
};
|
497
|
+
|
498
|
+
return w.with_walkers({
|
499
|
+
"function": _lambda,
|
500
|
+
"defun": function() {
|
501
|
+
// move function declarations to the top when
|
502
|
+
// they are not in some block.
|
503
|
+
var ast = _lambda.apply(this, arguments);
|
504
|
+
switch (w.parent()[0]) {
|
505
|
+
case "toplevel":
|
506
|
+
case "function":
|
507
|
+
case "defun":
|
508
|
+
return MAP.at_top(ast);
|
509
|
+
}
|
510
|
+
return ast;
|
511
|
+
},
|
512
|
+
"var": _vardefs,
|
513
|
+
"const": _vardefs,
|
514
|
+
"name": function(name) {
|
515
|
+
return [ this[0], get_mangled(name) ];
|
516
|
+
},
|
517
|
+
"try": function(t, c, f) {
|
518
|
+
return [ this[0],
|
519
|
+
MAP(t, walk),
|
520
|
+
c != null ? [ get_mangled(c[0]), MAP(c[1], walk) ] : null,
|
521
|
+
f != null ? MAP(f, walk) : null ];
|
522
|
+
},
|
523
|
+
"toplevel": function(body) {
|
524
|
+
var self = this;
|
525
|
+
return with_scope(self.scope, function(){
|
526
|
+
return [ self[0], MAP(body, walk) ];
|
527
|
+
});
|
528
|
+
},
|
529
|
+
"for-in": function(has_var, name, obj, stat) {
|
530
|
+
return [ this[0], has_var, get_mangled(name), walk(obj), walk(stat) ];
|
531
|
+
}
|
532
|
+
}, function() {
|
533
|
+
return walk(ast_add_scope(ast));
|
534
|
+
});
|
535
|
+
};
|
536
|
+
|
537
|
+
/* -----[
|
538
|
+
- compress foo["bar"] into foo.bar,
|
539
|
+
- remove block brackets {} where possible
|
540
|
+
- join consecutive var declarations
|
541
|
+
- various optimizations for IFs:
|
542
|
+
- if (cond) foo(); else bar(); ==> cond?foo():bar();
|
543
|
+
- if (cond) foo(); ==> cond&&foo();
|
544
|
+
- if (foo) return bar(); else return baz(); ==> return foo?bar():baz(); // also for throw
|
545
|
+
- if (foo) return bar(); else something(); ==> {if(foo)return bar();something()}
|
546
|
+
]----- */
|
547
|
+
|
548
|
+
var warn = function(){};
|
549
|
+
|
550
|
+
function best_of(ast1, ast2) {
|
551
|
+
return gen_code(ast1).length > gen_code(ast2[0] == "stat" ? ast2[1] : ast2).length ? ast2 : ast1;
|
552
|
+
};
|
553
|
+
|
554
|
+
function last_stat(b) {
|
555
|
+
if (b[0] == "block" && b[1] && b[1].length > 0)
|
556
|
+
return b[1][b[1].length - 1];
|
557
|
+
return b;
|
558
|
+
}
|
559
|
+
|
560
|
+
function aborts(t) {
|
561
|
+
if (t) {
|
562
|
+
t = last_stat(t);
|
563
|
+
if (t[0] == "return" || t[0] == "break" || t[0] == "continue" || t[0] == "throw")
|
564
|
+
return true;
|
565
|
+
}
|
566
|
+
};
|
567
|
+
|
568
|
+
function boolean_expr(expr) {
|
569
|
+
return ( (expr[0] == "unary-prefix"
|
570
|
+
&& member(expr[1], [ "!", "delete" ])) ||
|
571
|
+
|
572
|
+
(expr[0] == "binary"
|
573
|
+
&& member(expr[1], [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ])) ||
|
574
|
+
|
575
|
+
(expr[0] == "binary"
|
576
|
+
&& member(expr[1], [ "&&", "||" ])
|
577
|
+
&& boolean_expr(expr[2])
|
578
|
+
&& boolean_expr(expr[3])) ||
|
579
|
+
|
580
|
+
(expr[0] == "conditional"
|
581
|
+
&& boolean_expr(expr[2])
|
582
|
+
&& boolean_expr(expr[3])) ||
|
583
|
+
|
584
|
+
(expr[0] == "assign"
|
585
|
+
&& expr[1] === true
|
586
|
+
&& boolean_expr(expr[3])) ||
|
587
|
+
|
588
|
+
(expr[0] == "seq"
|
589
|
+
&& boolean_expr(expr[expr.length - 1]))
|
590
|
+
);
|
591
|
+
};
|
592
|
+
|
593
|
+
function make_conditional(c, t, e) {
|
594
|
+
if (c[0] == "unary-prefix" && c[1] == "!") {
|
595
|
+
return e ? [ "conditional", c[2], e, t ] : [ "binary", "||", c[2], t ];
|
596
|
+
} else {
|
597
|
+
return e ? [ "conditional", c, t, e ] : [ "binary", "&&", c, t ];
|
598
|
+
}
|
599
|
+
};
|
600
|
+
|
601
|
+
function empty(b) {
|
602
|
+
return !b || (b[0] == "block" && (!b[1] || b[1].length == 0));
|
603
|
+
};
|
604
|
+
|
605
|
+
function is_string(node) {
|
606
|
+
return (node[0] == "string" ||
|
607
|
+
node[0] == "unary-prefix" && node[1] == "typeof" ||
|
608
|
+
node[0] == "binary" && node[1] == "+" &&
|
609
|
+
(is_string(node[2]) || is_string(node[3])));
|
610
|
+
};
|
611
|
+
|
612
|
+
var when_constant = (function(){
|
613
|
+
|
614
|
+
var $NOT_CONSTANT = {};
|
615
|
+
|
616
|
+
// this can only evaluate constant expressions. If it finds anything
|
617
|
+
// not constant, it throws $NOT_CONSTANT.
|
618
|
+
function evaluate(expr) {
|
619
|
+
switch (expr[0]) {
|
620
|
+
case "string":
|
621
|
+
case "num":
|
622
|
+
return expr[1];
|
623
|
+
case "name":
|
624
|
+
case "atom":
|
625
|
+
switch (expr[1]) {
|
626
|
+
case "true": return true;
|
627
|
+
case "false": return false;
|
628
|
+
}
|
629
|
+
break;
|
630
|
+
case "unary-prefix":
|
631
|
+
switch (expr[1]) {
|
632
|
+
case "!": return !evaluate(expr[2]);
|
633
|
+
case "typeof": return typeof evaluate(expr[2]);
|
634
|
+
case "~": return ~evaluate(expr[2]);
|
635
|
+
case "-": return -evaluate(expr[2]);
|
636
|
+
case "+": return +evaluate(expr[2]);
|
637
|
+
}
|
638
|
+
break;
|
639
|
+
case "binary":
|
640
|
+
var left = expr[2], right = expr[3];
|
641
|
+
switch (expr[1]) {
|
642
|
+
case "&&" : return evaluate(left) && evaluate(right);
|
643
|
+
case "||" : return evaluate(left) || evaluate(right);
|
644
|
+
case "|" : return evaluate(left) | evaluate(right);
|
645
|
+
case "&" : return evaluate(left) & evaluate(right);
|
646
|
+
case "^" : return evaluate(left) ^ evaluate(right);
|
647
|
+
case "+" : return evaluate(left) + evaluate(right);
|
648
|
+
case "*" : return evaluate(left) * evaluate(right);
|
649
|
+
case "/" : return evaluate(left) / evaluate(right);
|
650
|
+
case "-" : return evaluate(left) - evaluate(right);
|
651
|
+
case "<<" : return evaluate(left) << evaluate(right);
|
652
|
+
case ">>" : return evaluate(left) >> evaluate(right);
|
653
|
+
case ">>>" : return evaluate(left) >>> evaluate(right);
|
654
|
+
case "==" : return evaluate(left) == evaluate(right);
|
655
|
+
case "===" : return evaluate(left) === evaluate(right);
|
656
|
+
case "!=" : return evaluate(left) != evaluate(right);
|
657
|
+
case "!==" : return evaluate(left) !== evaluate(right);
|
658
|
+
case "<" : return evaluate(left) < evaluate(right);
|
659
|
+
case "<=" : return evaluate(left) <= evaluate(right);
|
660
|
+
case ">" : return evaluate(left) > evaluate(right);
|
661
|
+
case ">=" : return evaluate(left) >= evaluate(right);
|
662
|
+
case "in" : return evaluate(left) in evaluate(right);
|
663
|
+
case "instanceof" : return evaluate(left) instanceof evaluate(right);
|
664
|
+
}
|
665
|
+
}
|
666
|
+
throw $NOT_CONSTANT;
|
667
|
+
};
|
668
|
+
|
669
|
+
return function(expr, yes, no) {
|
670
|
+
try {
|
671
|
+
var val = evaluate(expr), ast;
|
672
|
+
switch (typeof val) {
|
673
|
+
case "string": ast = [ "string", val ]; break;
|
674
|
+
case "number": ast = [ "num", val ]; break;
|
675
|
+
case "boolean": ast = [ "name", String(val) ]; break;
|
676
|
+
default: throw new Error("Can't handle constant of type: " + (typeof val));
|
677
|
+
}
|
678
|
+
return yes.call(expr, ast, val);
|
679
|
+
} catch(ex) {
|
680
|
+
if (ex === $NOT_CONSTANT) {
|
681
|
+
if (expr[0] == "binary"
|
682
|
+
&& (expr[1] == "===" || expr[1] == "!==")
|
683
|
+
&& ((is_string(expr[2]) && is_string(expr[3]))
|
684
|
+
|| (boolean_expr(expr[2]) && boolean_expr(expr[3])))) {
|
685
|
+
expr[1] = expr[1].substr(0, 2);
|
686
|
+
}
|
687
|
+
return no ? no.call(expr, expr) : null;
|
688
|
+
}
|
689
|
+
else throw ex;
|
690
|
+
}
|
691
|
+
};
|
692
|
+
|
693
|
+
})();
|
694
|
+
|
695
|
+
function warn_unreachable(ast) {
|
696
|
+
if (!empty(ast))
|
697
|
+
warn("Dropping unreachable code: " + gen_code(ast, true));
|
698
|
+
};
|
699
|
+
|
700
|
+
function ast_squeeze(ast, options) {
|
701
|
+
options = defaults(options, {
|
702
|
+
make_seqs : true,
|
703
|
+
dead_code : true,
|
704
|
+
keep_comps : true,
|
705
|
+
no_warnings : false
|
706
|
+
});
|
707
|
+
|
708
|
+
var w = ast_walker(), walk = w.walk, scope;
|
709
|
+
|
710
|
+
function negate(c) {
|
711
|
+
var not_c = [ "unary-prefix", "!", c ];
|
712
|
+
switch (c[0]) {
|
713
|
+
case "unary-prefix":
|
714
|
+
return c[1] == "!" && boolean_expr(c[2]) ? c[2] : not_c;
|
715
|
+
case "seq":
|
716
|
+
c = slice(c);
|
717
|
+
c[c.length - 1] = negate(c[c.length - 1]);
|
718
|
+
return c;
|
719
|
+
case "conditional":
|
720
|
+
return best_of(not_c, [ "conditional", c[1], negate(c[2]), negate(c[3]) ]);
|
721
|
+
case "binary":
|
722
|
+
var op = c[1], left = c[2], right = c[3];
|
723
|
+
if (!options.keep_comps) switch (op) {
|
724
|
+
case "<=" : return [ "binary", ">", left, right ];
|
725
|
+
case "<" : return [ "binary", ">=", left, right ];
|
726
|
+
case ">=" : return [ "binary", "<", left, right ];
|
727
|
+
case ">" : return [ "binary", "<=", left, right ];
|
728
|
+
}
|
729
|
+
switch (op) {
|
730
|
+
case "==" : return [ "binary", "!=", left, right ];
|
731
|
+
case "!=" : return [ "binary", "==", left, right ];
|
732
|
+
case "===" : return [ "binary", "!==", left, right ];
|
733
|
+
case "!==" : return [ "binary", "===", left, right ];
|
734
|
+
case "&&" : return best_of(not_c, [ "binary", "||", negate(left), negate(right) ]);
|
735
|
+
case "||" : return best_of(not_c, [ "binary", "&&", negate(left), negate(right) ]);
|
736
|
+
}
|
737
|
+
break;
|
738
|
+
}
|
739
|
+
return not_c;
|
740
|
+
};
|
741
|
+
|
742
|
+
function with_scope(s, cont) {
|
743
|
+
var _scope = scope;
|
744
|
+
scope = s;
|
745
|
+
var ret = cont();
|
746
|
+
ret.scope = s;
|
747
|
+
scope = _scope;
|
748
|
+
return ret;
|
749
|
+
};
|
750
|
+
|
751
|
+
function rmblock(block) {
|
752
|
+
if (block != null && block[0] == "block" && block[1]) {
|
753
|
+
if (block[1].length == 1)
|
754
|
+
block = block[1][0];
|
755
|
+
else if (block[1].length == 0)
|
756
|
+
block = [ "block" ];
|
757
|
+
}
|
758
|
+
return block;
|
759
|
+
};
|
760
|
+
|
761
|
+
function _lambda(name, args, body) {
|
762
|
+
return [ this[0], name, args, with_scope(body.scope, function(){
|
763
|
+
return tighten(MAP(body, walk), "lambda");
|
764
|
+
}) ];
|
765
|
+
};
|
766
|
+
|
767
|
+
// we get here for blocks that have been already transformed.
|
768
|
+
// this function does a few things:
|
769
|
+
// 1. discard useless blocks
|
770
|
+
// 2. join consecutive var declarations
|
771
|
+
// 3. remove obviously dead code
|
772
|
+
// 4. transform consecutive statements using the comma operator
|
773
|
+
// 5. if block_type == "lambda" and it detects constructs like if(foo) return ... - rewrite like if (!foo) { ... }
|
774
|
+
function tighten(statements, block_type) {
|
775
|
+
statements = statements.reduce(function(a, stat){
|
776
|
+
if (stat[0] == "block") {
|
777
|
+
if (stat[1]) {
|
778
|
+
a.push.apply(a, stat[1]);
|
779
|
+
}
|
780
|
+
} else {
|
781
|
+
a.push(stat);
|
782
|
+
}
|
783
|
+
return a;
|
784
|
+
}, []);
|
785
|
+
|
786
|
+
statements = (function(a, prev){
|
787
|
+
statements.forEach(function(cur){
|
788
|
+
if (prev && ((cur[0] == "var" && prev[0] == "var") ||
|
789
|
+
(cur[0] == "const" && prev[0] == "const"))) {
|
790
|
+
prev[1] = prev[1].concat(cur[1]);
|
791
|
+
} else {
|
792
|
+
a.push(cur);
|
793
|
+
prev = cur;
|
794
|
+
}
|
795
|
+
});
|
796
|
+
return a;
|
797
|
+
})([]);
|
798
|
+
|
799
|
+
if (options.dead_code) statements = (function(a, has_quit){
|
800
|
+
statements.forEach(function(st){
|
801
|
+
if (has_quit) {
|
802
|
+
if (member(st[0], [ "function", "defun" , "var", "const" ])) {
|
803
|
+
a.push(st);
|
804
|
+
}
|
805
|
+
else if (!options.no_warnings)
|
806
|
+
warn_unreachable(st);
|
807
|
+
}
|
808
|
+
else {
|
809
|
+
a.push(st);
|
810
|
+
if (member(st[0], [ "return", "throw", "break", "continue" ]))
|
811
|
+
has_quit = true;
|
812
|
+
}
|
813
|
+
});
|
814
|
+
return a;
|
815
|
+
})([]);
|
816
|
+
|
817
|
+
if (options.make_seqs) statements = (function(a, prev) {
|
818
|
+
statements.forEach(function(cur){
|
819
|
+
if (prev && prev[0] == "stat" && cur[0] == "stat") {
|
820
|
+
prev[1] = [ "seq", prev[1], cur[1] ];
|
821
|
+
} else {
|
822
|
+
a.push(cur);
|
823
|
+
prev = cur;
|
824
|
+
}
|
825
|
+
});
|
826
|
+
return a;
|
827
|
+
})([]);
|
828
|
+
|
829
|
+
if (block_type == "lambda") statements = (function(i, a, stat){
|
830
|
+
while (i < statements.length) {
|
831
|
+
stat = statements[i++];
|
832
|
+
if (stat[0] == "if" && !stat[3]) {
|
833
|
+
if (stat[2][0] == "return" && stat[2][1] == null) {
|
834
|
+
a.push(make_if(negate(stat[1]), [ "block", statements.slice(i) ]));
|
835
|
+
break;
|
836
|
+
}
|
837
|
+
var last = last_stat(stat[2]);
|
838
|
+
if (last[0] == "return" && last[1] == null) {
|
839
|
+
a.push(make_if(stat[1], [ "block", stat[2][1].slice(0, -1) ], [ "block", statements.slice(i) ]));
|
840
|
+
break;
|
841
|
+
}
|
842
|
+
}
|
843
|
+
a.push(stat);
|
844
|
+
}
|
845
|
+
return a;
|
846
|
+
})(0, []);
|
847
|
+
|
848
|
+
return statements;
|
849
|
+
};
|
850
|
+
|
851
|
+
function make_if(c, t, e) {
|
852
|
+
return when_constant(c, function(ast, val){
|
853
|
+
if (val) {
|
854
|
+
warn_unreachable(e);
|
855
|
+
return t;
|
856
|
+
} else {
|
857
|
+
warn_unreachable(t);
|
858
|
+
return e;
|
859
|
+
}
|
860
|
+
}, function() {
|
861
|
+
return make_real_if(c, t, e);
|
862
|
+
});
|
863
|
+
};
|
864
|
+
|
865
|
+
function make_real_if(c, t, e) {
|
866
|
+
c = walk(c);
|
867
|
+
t = walk(t);
|
868
|
+
e = walk(e);
|
869
|
+
|
870
|
+
if (empty(t)) {
|
871
|
+
c = negate(c);
|
872
|
+
t = e;
|
873
|
+
e = null;
|
874
|
+
} else if (empty(e)) {
|
875
|
+
e = null;
|
876
|
+
} else {
|
877
|
+
// if we have both else and then, maybe it makes sense to switch them?
|
878
|
+
(function(){
|
879
|
+
var a = gen_code(c);
|
880
|
+
var n = negate(c);
|
881
|
+
var b = gen_code(n);
|
882
|
+
if (b.length < a.length) {
|
883
|
+
var tmp = t;
|
884
|
+
t = e;
|
885
|
+
e = tmp;
|
886
|
+
c = n;
|
887
|
+
}
|
888
|
+
})();
|
889
|
+
}
|
890
|
+
if (empty(e) && empty(t))
|
891
|
+
return [ "stat", c ];
|
892
|
+
var ret = [ "if", c, t, e ];
|
893
|
+
if (t[0] == "if" && empty(t[3]) && empty(e)) {
|
894
|
+
ret = best_of(ret, walk([ "if", [ "binary", "&&", c, t[1] ], t[2] ]));
|
895
|
+
}
|
896
|
+
else if (t[0] == "stat") {
|
897
|
+
if (e) {
|
898
|
+
if (e[0] == "stat") {
|
899
|
+
ret = best_of(ret, [ "stat", make_conditional(c, t[1], e[1]) ]);
|
900
|
+
}
|
901
|
+
}
|
902
|
+
else {
|
903
|
+
ret = best_of(ret, [ "stat", make_conditional(c, t[1]) ]);
|
904
|
+
}
|
905
|
+
}
|
906
|
+
else if (e && t[0] == e[0] && (t[0] == "return" || t[0] == "throw")) {
|
907
|
+
ret = best_of(ret, [ t[0], make_conditional(c, t[1], e[1] ) ]);
|
908
|
+
}
|
909
|
+
else if (e && aborts(t)) {
|
910
|
+
ret = [ [ "if", c, t ] ];
|
911
|
+
if (e[0] == "block") {
|
912
|
+
if (e[1]) ret = ret.concat(e[1]);
|
913
|
+
}
|
914
|
+
else {
|
915
|
+
ret.push(e);
|
916
|
+
}
|
917
|
+
ret = walk([ "block", ret ]);
|
918
|
+
}
|
919
|
+
else if (t && aborts(e)) {
|
920
|
+
ret = [ [ "if", negate(c), e ] ];
|
921
|
+
if (t[0] == "block") {
|
922
|
+
if (t[1]) ret = ret.concat(t[1]);
|
923
|
+
} else {
|
924
|
+
ret.push(t);
|
925
|
+
}
|
926
|
+
ret = walk([ "block", ret ]);
|
927
|
+
}
|
928
|
+
return ret;
|
929
|
+
};
|
930
|
+
|
931
|
+
function _do_while(cond, body) {
|
932
|
+
return when_constant(cond, function(cond, val){
|
933
|
+
if (!val) {
|
934
|
+
warn_unreachable(body);
|
935
|
+
return [ "block" ];
|
936
|
+
} else {
|
937
|
+
return [ "for", null, null, null, walk(body) ];
|
938
|
+
}
|
939
|
+
});
|
940
|
+
};
|
941
|
+
|
942
|
+
return w.with_walkers({
|
943
|
+
"sub": function(expr, subscript) {
|
944
|
+
if (subscript[0] == "string") {
|
945
|
+
var name = subscript[1];
|
946
|
+
if (is_identifier(name)) {
|
947
|
+
return [ "dot", walk(expr), name ];
|
948
|
+
}
|
949
|
+
}
|
950
|
+
},
|
951
|
+
"if": make_if,
|
952
|
+
"toplevel": function(body) {
|
953
|
+
return [ "toplevel", with_scope(this.scope, function(){
|
954
|
+
return tighten(MAP(body, walk));
|
955
|
+
}) ];
|
956
|
+
},
|
957
|
+
"switch": function(expr, body) {
|
958
|
+
var last = body.length - 1;
|
959
|
+
return [ "switch", walk(expr), MAP(body, function(branch, i){
|
960
|
+
var block = tighten(MAP(branch[1], walk));
|
961
|
+
if (i == last && block.length > 0) {
|
962
|
+
var node = block[block.length - 1];
|
963
|
+
if (node[0] == "break" && !node[1])
|
964
|
+
block.pop();
|
965
|
+
}
|
966
|
+
return [ branch[0] ? walk(branch[0]) : null, block ];
|
967
|
+
}) ];
|
968
|
+
},
|
969
|
+
"function": function() {
|
970
|
+
var ret = _lambda.apply(this, arguments);
|
971
|
+
if (ret[1] && !HOP(scope.refs, ret[1])) {
|
972
|
+
ret[1] = null;
|
973
|
+
}
|
974
|
+
return ret;
|
975
|
+
},
|
976
|
+
"defun": _lambda,
|
977
|
+
"block": function(body) {
|
978
|
+
if (body) return rmblock([ "block", tighten(MAP(body, walk)) ]);
|
979
|
+
},
|
980
|
+
"binary": function(op, left, right) {
|
981
|
+
return when_constant([ "binary", op, walk(left), walk(right) ], function yes(c){
|
982
|
+
return best_of(walk(c), this);
|
983
|
+
}, function no() {
|
984
|
+
return this;
|
985
|
+
});
|
986
|
+
},
|
987
|
+
"conditional": function(c, t, e) {
|
988
|
+
return make_conditional(walk(c), walk(t), walk(e));
|
989
|
+
},
|
990
|
+
"try": function(t, c, f) {
|
991
|
+
return [
|
992
|
+
"try",
|
993
|
+
tighten(MAP(t, walk)),
|
994
|
+
c != null ? [ c[0], tighten(MAP(c[1], walk)) ] : null,
|
995
|
+
f != null ? tighten(MAP(f, walk)) : null
|
996
|
+
];
|
997
|
+
},
|
998
|
+
"unary-prefix": function(op, expr) {
|
999
|
+
expr = walk(expr);
|
1000
|
+
var ret = [ "unary-prefix", op, expr ];
|
1001
|
+
if (op == "!")
|
1002
|
+
ret = best_of(ret, negate(expr));
|
1003
|
+
return when_constant(ret, function(ast, val){
|
1004
|
+
return walk(ast); // it's either true or false, so minifies to !0 or !1
|
1005
|
+
}, function() { return ret });
|
1006
|
+
},
|
1007
|
+
"name": function(name) {
|
1008
|
+
switch (name) {
|
1009
|
+
case "true": return [ "unary-prefix", "!", [ "num", 0 ]];
|
1010
|
+
case "false": return [ "unary-prefix", "!", [ "num", 1 ]];
|
1011
|
+
}
|
1012
|
+
},
|
1013
|
+
"new": function(ctor, args) {
|
1014
|
+
if (ctor[0] == "name" && ctor[1] == "Array" && !scope.has("Array")) {
|
1015
|
+
if (args.length != 1) {
|
1016
|
+
return [ "array", args ];
|
1017
|
+
} else {
|
1018
|
+
return [ "call", [ "name", "Array" ], args ];
|
1019
|
+
}
|
1020
|
+
}
|
1021
|
+
},
|
1022
|
+
"call": function(expr, args) {
|
1023
|
+
if (expr[0] == "name" && expr[1] == "Array" && args.length != 1 && !scope.has("Array")) {
|
1024
|
+
return [ "array", args ];
|
1025
|
+
}
|
1026
|
+
},
|
1027
|
+
"while": _do_while,
|
1028
|
+
"do": _do_while
|
1029
|
+
}, function() {
|
1030
|
+
return walk(ast_add_scope(ast));
|
1031
|
+
});
|
1032
|
+
};
|
1033
|
+
|
1034
|
+
/* -----[ re-generate code from the AST ]----- */
|
1035
|
+
|
1036
|
+
var DOT_CALL_NO_PARENS = jsp.array_to_hash([
|
1037
|
+
"name",
|
1038
|
+
"array",
|
1039
|
+
"string",
|
1040
|
+
"dot",
|
1041
|
+
"sub",
|
1042
|
+
"call",
|
1043
|
+
"regexp"
|
1044
|
+
]);
|
1045
|
+
|
1046
|
+
function make_string(str) {
|
1047
|
+
var dq = 0, sq = 0;
|
1048
|
+
str = str.replace(/[\\\b\f\n\r\t\x22\x27]/g, function(s){
|
1049
|
+
switch (s) {
|
1050
|
+
case "\\": return "\\\\";
|
1051
|
+
case "\b": return "\\b";
|
1052
|
+
case "\f": return "\\f";
|
1053
|
+
case "\n": return "\\n";
|
1054
|
+
case "\r": return "\\r";
|
1055
|
+
case "\t": return "\\t";
|
1056
|
+
case '"': ++dq; return '"';
|
1057
|
+
case "'": ++sq; return "'";
|
1058
|
+
}
|
1059
|
+
return s;
|
1060
|
+
});
|
1061
|
+
if (dq > sq) {
|
1062
|
+
return "'" + str.replace(/\x27/g, "\\'") + "'";
|
1063
|
+
} else {
|
1064
|
+
return '"' + str.replace(/\x22/g, '\\"') + '"';
|
1065
|
+
}
|
1066
|
+
};
|
1067
|
+
|
1068
|
+
function gen_code(ast, beautify) {
|
1069
|
+
if (beautify) beautify = defaults(beautify, {
|
1070
|
+
indent_start : 0,
|
1071
|
+
indent_level : 4,
|
1072
|
+
quote_keys : false,
|
1073
|
+
space_colon : false
|
1074
|
+
});
|
1075
|
+
var indentation = 0,
|
1076
|
+
newline = beautify ? "\n" : "",
|
1077
|
+
space = beautify ? " " : "";
|
1078
|
+
|
1079
|
+
function indent(line) {
|
1080
|
+
if (line == null)
|
1081
|
+
line = "";
|
1082
|
+
if (beautify)
|
1083
|
+
line = repeat_string(" ", beautify.indent_start + indentation * beautify.indent_level) + line;
|
1084
|
+
return line;
|
1085
|
+
};
|
1086
|
+
|
1087
|
+
function with_indent(cont, incr) {
|
1088
|
+
if (incr == null) incr = 1;
|
1089
|
+
indentation += incr;
|
1090
|
+
try { return cont.apply(null, slice(arguments, 1)); }
|
1091
|
+
finally { indentation -= incr; }
|
1092
|
+
};
|
1093
|
+
|
1094
|
+
function add_spaces(a) {
|
1095
|
+
if (beautify)
|
1096
|
+
return a.join(" ");
|
1097
|
+
var b = [];
|
1098
|
+
for (var i = 0; i < a.length; ++i) {
|
1099
|
+
var next = a[i + 1];
|
1100
|
+
b.push(a[i]);
|
1101
|
+
if (next &&
|
1102
|
+
((/[a-z0-9_\x24]$/i.test(a[i].toString()) && /^[a-z0-9_\x24]/i.test(next.toString())) ||
|
1103
|
+
(/[\+\-]$/.test(a[i].toString()) && /^[\+\-]/.test(next.toString())))) {
|
1104
|
+
b.push(" ");
|
1105
|
+
}
|
1106
|
+
}
|
1107
|
+
return b.join("");
|
1108
|
+
};
|
1109
|
+
|
1110
|
+
function add_commas(a) {
|
1111
|
+
return a.join("," + space);
|
1112
|
+
};
|
1113
|
+
|
1114
|
+
function parenthesize(expr) {
|
1115
|
+
var gen = make(expr);
|
1116
|
+
for (var i = 1; i < arguments.length; ++i) {
|
1117
|
+
var el = arguments[i];
|
1118
|
+
if ((el instanceof Function && el(expr)) || expr[0] == el)
|
1119
|
+
return "(" + gen + ")";
|
1120
|
+
}
|
1121
|
+
return gen;
|
1122
|
+
};
|
1123
|
+
|
1124
|
+
function best_of(a) {
|
1125
|
+
if (a.length == 1) {
|
1126
|
+
return a[0];
|
1127
|
+
}
|
1128
|
+
if (a.length == 2) {
|
1129
|
+
var b = a[1];
|
1130
|
+
a = a[0];
|
1131
|
+
return a.length <= b.length ? a : b;
|
1132
|
+
}
|
1133
|
+
return best_of([ a[0], best_of(a.slice(1)) ]);
|
1134
|
+
};
|
1135
|
+
|
1136
|
+
function needs_parens(expr) {
|
1137
|
+
if (expr[0] == "function") {
|
1138
|
+
// dot/call on a literal function requires the
|
1139
|
+
// function literal itself to be parenthesized
|
1140
|
+
// only if it's the first "thing" in a
|
1141
|
+
// statement. This means that the parent is
|
1142
|
+
// "stat", but it could also be a "seq" and
|
1143
|
+
// we're the first in this "seq" and the
|
1144
|
+
// parent is "stat", and so on. Messy stuff,
|
1145
|
+
// but it worths the trouble.
|
1146
|
+
var a = slice($stack), self = a.pop(), p = a.pop();
|
1147
|
+
while (p) {
|
1148
|
+
if (p[0] == "stat") return true;
|
1149
|
+
if ((p[0] == "seq" && p[1] === self) ||
|
1150
|
+
(p[0] == "call" && p[1] === self) ||
|
1151
|
+
(p[0] == "binary" && p[2] === self)) {
|
1152
|
+
self = p;
|
1153
|
+
p = a.pop();
|
1154
|
+
} else {
|
1155
|
+
return false;
|
1156
|
+
}
|
1157
|
+
}
|
1158
|
+
}
|
1159
|
+
return !HOP(DOT_CALL_NO_PARENS, expr[0]);
|
1160
|
+
};
|
1161
|
+
|
1162
|
+
function make_num(num) {
|
1163
|
+
var str = num.toString(10), a = [ str.replace(/^0\./, ".") ], m;
|
1164
|
+
if (Math.floor(num) === num) {
|
1165
|
+
a.push("0x" + num.toString(16).toLowerCase(), // probably pointless
|
1166
|
+
"0" + num.toString(8)); // same.
|
1167
|
+
if ((m = /^(.*?)(0+)$/.exec(num))) {
|
1168
|
+
a.push(m[1] + "e" + m[2].length);
|
1169
|
+
}
|
1170
|
+
} else if ((m = /^0?\.(0+)(.*)$/.exec(num))) {
|
1171
|
+
a.push(m[2] + "e-" + (m[1].length + m[2].length),
|
1172
|
+
str.substr(str.indexOf(".")));
|
1173
|
+
}
|
1174
|
+
return best_of(a);
|
1175
|
+
};
|
1176
|
+
|
1177
|
+
var generators = {
|
1178
|
+
"string": make_string,
|
1179
|
+
"num": make_num,
|
1180
|
+
"name": make_name,
|
1181
|
+
"toplevel": function(statements) {
|
1182
|
+
return make_block_statements(statements)
|
1183
|
+
.join(newline + newline);
|
1184
|
+
},
|
1185
|
+
"block": make_block,
|
1186
|
+
"var": function(defs) {
|
1187
|
+
return "var " + add_commas(MAP(defs, make_1vardef)) + ";";
|
1188
|
+
},
|
1189
|
+
"const": function(defs) {
|
1190
|
+
return "const " + add_commas(MAP(defs, make_1vardef)) + ";";
|
1191
|
+
},
|
1192
|
+
"try": function(tr, ca, fi) {
|
1193
|
+
var out = [ "try", make_block(tr) ];
|
1194
|
+
if (ca) out.push("catch", "(" + ca[0] + ")", make_block(ca[1]));
|
1195
|
+
if (fi) out.push("finally", make_block(fi));
|
1196
|
+
return add_spaces(out);
|
1197
|
+
},
|
1198
|
+
"throw": function(expr) {
|
1199
|
+
return add_spaces([ "throw", make(expr) ]) + ";";
|
1200
|
+
},
|
1201
|
+
"new": function(ctor, args) {
|
1202
|
+
args = args.length > 0 ? "(" + add_commas(MAP(args, make)) + ")" : "";
|
1203
|
+
return add_spaces([ "new", parenthesize(ctor, "seq", "binary", "conditional", "assign", function(expr){
|
1204
|
+
var w = ast_walker(), has_call = {};
|
1205
|
+
try {
|
1206
|
+
w.with_walkers({
|
1207
|
+
"call": function() { throw has_call },
|
1208
|
+
"function": function() { return this }
|
1209
|
+
}, function(){
|
1210
|
+
w.walk(expr);
|
1211
|
+
});
|
1212
|
+
} catch(ex) {
|
1213
|
+
if (ex === has_call)
|
1214
|
+
return true;
|
1215
|
+
throw ex;
|
1216
|
+
}
|
1217
|
+
}) + args ]);
|
1218
|
+
},
|
1219
|
+
"switch": function(expr, body) {
|
1220
|
+
return add_spaces([ "switch", "(" + make(expr) + ")", make_switch_block(body) ]);
|
1221
|
+
},
|
1222
|
+
"break": function(label) {
|
1223
|
+
var out = "break";
|
1224
|
+
if (label != null)
|
1225
|
+
out += " " + make_name(label);
|
1226
|
+
return out + ";";
|
1227
|
+
},
|
1228
|
+
"continue": function(label) {
|
1229
|
+
var out = "continue";
|
1230
|
+
if (label != null)
|
1231
|
+
out += " " + make_name(label);
|
1232
|
+
return out + ";";
|
1233
|
+
},
|
1234
|
+
"conditional": function(co, th, el) {
|
1235
|
+
return add_spaces([ parenthesize(co, "assign", "seq", "conditional"), "?",
|
1236
|
+
parenthesize(th, "seq"), ":",
|
1237
|
+
parenthesize(el, "seq") ]);
|
1238
|
+
},
|
1239
|
+
"assign": function(op, lvalue, rvalue) {
|
1240
|
+
if (op && op !== true) op += "=";
|
1241
|
+
else op = "=";
|
1242
|
+
return add_spaces([ make(lvalue), op, parenthesize(rvalue, "seq") ]);
|
1243
|
+
},
|
1244
|
+
"dot": function(expr) {
|
1245
|
+
var out = make(expr), i = 1;
|
1246
|
+
if (expr[0] == "num")
|
1247
|
+
out += ".";
|
1248
|
+
else if (needs_parens(expr))
|
1249
|
+
out = "(" + out + ")";
|
1250
|
+
while (i < arguments.length)
|
1251
|
+
out += "." + make_name(arguments[i++]);
|
1252
|
+
return out;
|
1253
|
+
},
|
1254
|
+
"call": function(func, args) {
|
1255
|
+
var f = make(func);
|
1256
|
+
if (needs_parens(func))
|
1257
|
+
f = "(" + f + ")";
|
1258
|
+
return f + "(" + add_commas(MAP(args, function(expr){
|
1259
|
+
return parenthesize(expr, "seq");
|
1260
|
+
})) + ")";
|
1261
|
+
},
|
1262
|
+
"function": make_function,
|
1263
|
+
"defun": make_function,
|
1264
|
+
"if": function(co, th, el) {
|
1265
|
+
var out = [ "if", "(" + make(co) + ")", el ? make_then(th) : make(th) ];
|
1266
|
+
if (el) {
|
1267
|
+
out.push("else", make(el));
|
1268
|
+
}
|
1269
|
+
return add_spaces(out);
|
1270
|
+
},
|
1271
|
+
"for": function(init, cond, step, block) {
|
1272
|
+
var out = [ "for" ];
|
1273
|
+
init = (init != null ? make(init) : "").replace(/;*\s*$/, ";" + space);
|
1274
|
+
cond = (cond != null ? make(cond) : "").replace(/;*\s*$/, ";" + space);
|
1275
|
+
step = (step != null ? make(step) : "").replace(/;*\s*$/, "");
|
1276
|
+
var args = init + cond + step;
|
1277
|
+
if (args == "; ; ") args = ";;";
|
1278
|
+
out.push("(" + args + ")", make(block));
|
1279
|
+
return add_spaces(out);
|
1280
|
+
},
|
1281
|
+
"for-in": function(has_var, key, hash, block) {
|
1282
|
+
var out = add_spaces([ "for", "(" ]);
|
1283
|
+
if (has_var)
|
1284
|
+
out += "var ";
|
1285
|
+
out += add_spaces([ make_name(key) + " in " + make(hash) + ")", make(block) ]);
|
1286
|
+
return out;
|
1287
|
+
},
|
1288
|
+
"while": function(condition, block) {
|
1289
|
+
return add_spaces([ "while", "(" + make(condition) + ")", make(block) ]);
|
1290
|
+
},
|
1291
|
+
"do": function(condition, block) {
|
1292
|
+
return add_spaces([ "do", make(block), "while", "(" + make(condition) + ")" ]) + ";";
|
1293
|
+
},
|
1294
|
+
"return": function(expr) {
|
1295
|
+
var out = [ "return" ];
|
1296
|
+
if (expr != null) out.push(make(expr));
|
1297
|
+
return add_spaces(out) + ";";
|
1298
|
+
},
|
1299
|
+
"binary": function(operator, lvalue, rvalue) {
|
1300
|
+
var left = make(lvalue), right = make(rvalue);
|
1301
|
+
// XXX: I'm pretty sure other cases will bite here.
|
1302
|
+
// we need to be smarter.
|
1303
|
+
// adding parens all the time is the safest bet.
|
1304
|
+
if (member(lvalue[0], [ "assign", "conditional", "seq" ]) ||
|
1305
|
+
lvalue[0] == "binary" && PRECEDENCE[operator] > PRECEDENCE[lvalue[1]]) {
|
1306
|
+
left = "(" + left + ")";
|
1307
|
+
}
|
1308
|
+
if (member(rvalue[0], [ "assign", "conditional", "seq" ]) ||
|
1309
|
+
rvalue[0] == "binary" && PRECEDENCE[operator] >= PRECEDENCE[rvalue[1]] &&
|
1310
|
+
!(rvalue[1] == operator && member(operator, [ "&&", "||", "*" ]))) {
|
1311
|
+
right = "(" + right + ")";
|
1312
|
+
}
|
1313
|
+
return add_spaces([ left, operator, right ]);
|
1314
|
+
},
|
1315
|
+
"unary-prefix": function(operator, expr) {
|
1316
|
+
var val = make(expr);
|
1317
|
+
if (!(expr[0] == "num" || (expr[0] == "unary-prefix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
|
1318
|
+
val = "(" + val + ")";
|
1319
|
+
return operator + (jsp.is_alphanumeric_char(operator.charAt(0)) ? " " : "") + val;
|
1320
|
+
},
|
1321
|
+
"unary-postfix": function(operator, expr) {
|
1322
|
+
var val = make(expr);
|
1323
|
+
if (!(expr[0] == "num" || (expr[0] == "unary-postfix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
|
1324
|
+
val = "(" + val + ")";
|
1325
|
+
return val + operator;
|
1326
|
+
},
|
1327
|
+
"sub": function(expr, subscript) {
|
1328
|
+
var hash = make(expr);
|
1329
|
+
if (needs_parens(expr))
|
1330
|
+
hash = "(" + hash + ")";
|
1331
|
+
return hash + "[" + make(subscript) + "]";
|
1332
|
+
},
|
1333
|
+
"object": function(props) {
|
1334
|
+
if (props.length == 0)
|
1335
|
+
return "{}";
|
1336
|
+
return "{" + newline + with_indent(function(){
|
1337
|
+
return MAP(props, function(p){
|
1338
|
+
if (p.length == 3) {
|
1339
|
+
// getter/setter. The name is in p[0], the arg.list in p[1][2], the
|
1340
|
+
// body in p[1][3] and type ("get" / "set") in p[2].
|
1341
|
+
return indent(make_function(p[0], p[1][2], p[1][3], p[2]));
|
1342
|
+
}
|
1343
|
+
var key = p[0], val = make(p[1]);
|
1344
|
+
if (beautify && beautify.quote_keys) {
|
1345
|
+
key = make_string(key);
|
1346
|
+
} else if ((typeof key == "number" || !beautify && +key + "" == key)
|
1347
|
+
&& parseFloat(key) >= 0) {
|
1348
|
+
key = make_num(+key);
|
1349
|
+
} else if (!is_identifier(key)) {
|
1350
|
+
key = make_string(key);
|
1351
|
+
}
|
1352
|
+
return indent(add_spaces(beautify && beautify.space_colon
|
1353
|
+
? [ key, ":", val ]
|
1354
|
+
: [ key + ":", val ]));
|
1355
|
+
}).join("," + newline);
|
1356
|
+
}) + newline + indent("}");
|
1357
|
+
},
|
1358
|
+
"regexp": function(rx, mods) {
|
1359
|
+
return "/" + rx + "/" + mods;
|
1360
|
+
},
|
1361
|
+
"array": function(elements) {
|
1362
|
+
if (elements.length == 0) return "[]";
|
1363
|
+
return add_spaces([ "[", add_commas(MAP(elements, function(el){
|
1364
|
+
if (!beautify && el[0] == "atom" && el[1] == "undefined") return "";
|
1365
|
+
return parenthesize(el, "seq");
|
1366
|
+
})), "]" ]);
|
1367
|
+
},
|
1368
|
+
"stat": function(stmt) {
|
1369
|
+
return make(stmt).replace(/;*\s*$/, ";");
|
1370
|
+
},
|
1371
|
+
"seq": function() {
|
1372
|
+
return add_commas(MAP(slice(arguments), make));
|
1373
|
+
},
|
1374
|
+
"label": function(name, block) {
|
1375
|
+
return add_spaces([ make_name(name), ":", make(block) ]);
|
1376
|
+
},
|
1377
|
+
"with": function(expr, block) {
|
1378
|
+
return add_spaces([ "with", "(" + make(expr) + ")", make(block) ]);
|
1379
|
+
},
|
1380
|
+
"atom": function(name) {
|
1381
|
+
return make_name(name);
|
1382
|
+
}
|
1383
|
+
};
|
1384
|
+
|
1385
|
+
// The squeezer replaces "block"-s that contain only a single
|
1386
|
+
// statement with the statement itself; technically, the AST
|
1387
|
+
// is correct, but this can create problems when we output an
|
1388
|
+
// IF having an ELSE clause where the THEN clause ends in an
|
1389
|
+
// IF *without* an ELSE block (then the outer ELSE would refer
|
1390
|
+
// to the inner IF). This function checks for this case and
|
1391
|
+
// adds the block brackets if needed.
|
1392
|
+
function make_then(th) {
|
1393
|
+
if (th[0] == "do") {
|
1394
|
+
// https://github.com/mishoo/UglifyJS/issues/#issue/57
|
1395
|
+
// IE croaks with "syntax error" on code like this:
|
1396
|
+
// if (foo) do ... while(cond); else ...
|
1397
|
+
// we need block brackets around do/while
|
1398
|
+
return make([ "block", [ th ]]);
|
1399
|
+
}
|
1400
|
+
var b = th;
|
1401
|
+
while (true) {
|
1402
|
+
var type = b[0];
|
1403
|
+
if (type == "if") {
|
1404
|
+
if (!b[3])
|
1405
|
+
// no else, we must add the block
|
1406
|
+
return make([ "block", [ th ]]);
|
1407
|
+
b = b[3];
|
1408
|
+
}
|
1409
|
+
else if (type == "while" || type == "do") b = b[2];
|
1410
|
+
else if (type == "for" || type == "for-in") b = b[4];
|
1411
|
+
else break;
|
1412
|
+
}
|
1413
|
+
return make(th);
|
1414
|
+
};
|
1415
|
+
|
1416
|
+
function make_function(name, args, body, keyword) {
|
1417
|
+
var out = keyword || "function";
|
1418
|
+
if (name) {
|
1419
|
+
out += " " + make_name(name);
|
1420
|
+
}
|
1421
|
+
out += "(" + add_commas(MAP(args, make_name)) + ")";
|
1422
|
+
return add_spaces([ out, make_block(body) ]);
|
1423
|
+
};
|
1424
|
+
|
1425
|
+
function make_name(name) {
|
1426
|
+
return name.toString();
|
1427
|
+
};
|
1428
|
+
|
1429
|
+
function make_block_statements(statements) {
|
1430
|
+
for (var a = [], last = statements.length - 1, i = 0; i <= last; ++i) {
|
1431
|
+
var stat = statements[i];
|
1432
|
+
var code = make(stat);
|
1433
|
+
if (code != ";") {
|
1434
|
+
if (!beautify && i == last) {
|
1435
|
+
if ((stat[0] == "while" && empty(stat[2])) ||
|
1436
|
+
(member(stat[0], [ "for", "for-in"] ) && empty(stat[4])) ||
|
1437
|
+
(stat[0] == "if" && empty(stat[2]) && !stat[3]) ||
|
1438
|
+
(stat[0] == "if" && stat[3] && empty(stat[3]))) {
|
1439
|
+
code = code.replace(/;*\s*$/, ";");
|
1440
|
+
} else {
|
1441
|
+
code = code.replace(/;+\s*$/, "");
|
1442
|
+
}
|
1443
|
+
}
|
1444
|
+
a.push(code);
|
1445
|
+
}
|
1446
|
+
}
|
1447
|
+
return MAP(a, indent);
|
1448
|
+
};
|
1449
|
+
|
1450
|
+
function make_switch_block(body) {
|
1451
|
+
var n = body.length;
|
1452
|
+
if (n == 0) return "{}";
|
1453
|
+
return "{" + newline + MAP(body, function(branch, i){
|
1454
|
+
var has_body = branch[1].length > 0, code = with_indent(function(){
|
1455
|
+
return indent(branch[0]
|
1456
|
+
? add_spaces([ "case", make(branch[0]) + ":" ])
|
1457
|
+
: "default:");
|
1458
|
+
}, 0.5) + (has_body ? newline + with_indent(function(){
|
1459
|
+
return make_block_statements(branch[1]).join(newline);
|
1460
|
+
}) : "");
|
1461
|
+
if (!beautify && has_body && i < n - 1)
|
1462
|
+
code += ";";
|
1463
|
+
return code;
|
1464
|
+
}).join(newline) + newline + indent("}");
|
1465
|
+
};
|
1466
|
+
|
1467
|
+
function make_block(statements) {
|
1468
|
+
if (!statements) return ";";
|
1469
|
+
if (statements.length == 0) return "{}";
|
1470
|
+
return "{" + newline + with_indent(function(){
|
1471
|
+
return make_block_statements(statements).join(newline);
|
1472
|
+
}) + newline + indent("}");
|
1473
|
+
};
|
1474
|
+
|
1475
|
+
function make_1vardef(def) {
|
1476
|
+
var name = def[0], val = def[1];
|
1477
|
+
if (val != null)
|
1478
|
+
name = add_spaces([ name, "=", make(val) ]);
|
1479
|
+
return name;
|
1480
|
+
};
|
1481
|
+
|
1482
|
+
var $stack = [];
|
1483
|
+
|
1484
|
+
function make(node) {
|
1485
|
+
var type = node[0];
|
1486
|
+
var gen = generators[type];
|
1487
|
+
if (!gen)
|
1488
|
+
throw new Error("Can't find generator for \"" + type + "\"");
|
1489
|
+
$stack.push(node);
|
1490
|
+
var ret = gen.apply(type, node.slice(1));
|
1491
|
+
$stack.pop();
|
1492
|
+
return ret;
|
1493
|
+
};
|
1494
|
+
|
1495
|
+
return make(ast);
|
1496
|
+
};
|
1497
|
+
|
1498
|
+
function split_lines(code, max_line_length) {
|
1499
|
+
var splits = [ 0 ];
|
1500
|
+
jsp.parse(function(){
|
1501
|
+
var next_token = jsp.tokenizer(code);
|
1502
|
+
var last_split = 0;
|
1503
|
+
var prev_token;
|
1504
|
+
function current_length(tok) {
|
1505
|
+
return tok.pos - last_split;
|
1506
|
+
};
|
1507
|
+
function split_here(tok) {
|
1508
|
+
last_split = tok.pos;
|
1509
|
+
splits.push(last_split);
|
1510
|
+
};
|
1511
|
+
function custom(){
|
1512
|
+
var tok = next_token.apply(this, arguments);
|
1513
|
+
out: {
|
1514
|
+
if (prev_token) {
|
1515
|
+
if (prev_token.type == "keyword") break out;
|
1516
|
+
}
|
1517
|
+
if (current_length(tok) > max_line_length) {
|
1518
|
+
switch (tok.type) {
|
1519
|
+
case "keyword":
|
1520
|
+
case "atom":
|
1521
|
+
case "name":
|
1522
|
+
case "punc":
|
1523
|
+
split_here(tok);
|
1524
|
+
break out;
|
1525
|
+
}
|
1526
|
+
}
|
1527
|
+
}
|
1528
|
+
prev_token = tok;
|
1529
|
+
return tok;
|
1530
|
+
};
|
1531
|
+
custom.context = function() {
|
1532
|
+
return next_token.context.apply(this, arguments);
|
1533
|
+
};
|
1534
|
+
return custom;
|
1535
|
+
}());
|
1536
|
+
return splits.map(function(pos, i){
|
1537
|
+
return code.substring(pos, splits[i + 1] || code.length);
|
1538
|
+
}).join("\n");
|
1539
|
+
};
|
1540
|
+
|
1541
|
+
/* -----[ Utilities ]----- */
|
1542
|
+
|
1543
|
+
function repeat_string(str, i) {
|
1544
|
+
if (i <= 0) return "";
|
1545
|
+
if (i == 1) return str;
|
1546
|
+
var d = repeat_string(str, i >> 1);
|
1547
|
+
d += d;
|
1548
|
+
if (i & 1) d += str;
|
1549
|
+
return d;
|
1550
|
+
};
|
1551
|
+
|
1552
|
+
function defaults(args, defs) {
|
1553
|
+
var ret = {};
|
1554
|
+
if (args === true)
|
1555
|
+
args = {};
|
1556
|
+
for (var i in defs) if (HOP(defs, i)) {
|
1557
|
+
ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
|
1558
|
+
}
|
1559
|
+
return ret;
|
1560
|
+
};
|
1561
|
+
|
1562
|
+
function is_identifier(name) {
|
1563
|
+
return /^[a-z_$][a-z0-9_$]*$/i.test(name)
|
1564
|
+
&& name != "this"
|
1565
|
+
&& !HOP(jsp.KEYWORDS_ATOM, name)
|
1566
|
+
&& !HOP(jsp.RESERVED_WORDS, name)
|
1567
|
+
&& !HOP(jsp.KEYWORDS, name);
|
1568
|
+
};
|
1569
|
+
|
1570
|
+
function HOP(obj, prop) {
|
1571
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
1572
|
+
};
|
1573
|
+
|
1574
|
+
// some utilities
|
1575
|
+
|
1576
|
+
var MAP;
|
1577
|
+
|
1578
|
+
(function(){
|
1579
|
+
MAP = function(a, f, o) {
|
1580
|
+
var ret = [];
|
1581
|
+
for (var i = 0; i < a.length; ++i) {
|
1582
|
+
var val = f.call(o, a[i], i);
|
1583
|
+
if (val instanceof AtTop) ret.unshift(val.v);
|
1584
|
+
else ret.push(val);
|
1585
|
+
}
|
1586
|
+
return ret;
|
1587
|
+
};
|
1588
|
+
MAP.at_top = function(val) { return new AtTop(val) };
|
1589
|
+
function AtTop(val) { this.v = val };
|
1590
|
+
})();
|
1591
|
+
|
1592
|
+
/* -----[ Exports ]----- */
|
1593
|
+
|
1594
|
+
exports.ast_walker = ast_walker;
|
1595
|
+
exports.ast_mangle = ast_mangle;
|
1596
|
+
exports.ast_squeeze = ast_squeeze;
|
1597
|
+
exports.gen_code = gen_code;
|
1598
|
+
exports.ast_add_scope = ast_add_scope;
|
1599
|
+
exports.ast_squeeze_more = require("./squeeze-more").ast_squeeze_more;
|
1600
|
+
exports.set_logger = function(logger) { warn = logger };
|
1601
|
+
exports.make_string = make_string;
|
1602
|
+
exports.split_lines = split_lines;
|