terser 5.12.1 → 5.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/bundle.min.js +5125 -3384
- package/lib/ast.js +1676 -350
- package/lib/compress/common.js +48 -0
- package/lib/compress/evaluate.js +4 -3
- package/lib/compress/index.js +23 -546
- package/lib/compress/inline.js +641 -0
- package/lib/compress/tighten-body.js +1 -1
- package/lib/equivalent-to.js +81 -107
- package/lib/minify.js +47 -14
- package/lib/mozilla-ast.js +474 -81
- package/lib/output.js +17 -0
- package/lib/parse.js +58 -56
- package/lib/propmangle.js +1 -1
- package/lib/size.js +6 -8
- package/lib/sourcemap.js +60 -26
- package/package.json +5 -3
- package/tools/domprops.js +12 -0
- package/tools/terser.d.ts +5 -3
package/lib/compress/index.js
CHANGED
@@ -85,7 +85,6 @@ import {
|
|
85
85
|
AST_If,
|
86
86
|
AST_Import,
|
87
87
|
AST_Infinity,
|
88
|
-
AST_IterationStatement,
|
89
88
|
AST_LabeledStatement,
|
90
89
|
AST_Lambda,
|
91
90
|
AST_Let,
|
@@ -141,7 +140,6 @@ import {
|
|
141
140
|
TreeWalker,
|
142
141
|
walk,
|
143
142
|
walk_abort,
|
144
|
-
walk_parent,
|
145
143
|
|
146
144
|
_INLINE,
|
147
145
|
_NOINLINE,
|
@@ -155,12 +153,11 @@ import {
|
|
155
153
|
makePredicate,
|
156
154
|
map_add,
|
157
155
|
MAP,
|
158
|
-
member,
|
159
156
|
remove,
|
160
157
|
return_false,
|
161
158
|
return_true,
|
162
159
|
regexp_source_fix,
|
163
|
-
has_annotation
|
160
|
+
has_annotation,
|
164
161
|
} from "../utils/index.js";
|
165
162
|
import { first_in_statement } from "../utils/first_in_statement.js";
|
166
163
|
import { equivalent_to } from "../equivalent-to.js";
|
@@ -191,7 +188,6 @@ import {
|
|
191
188
|
import {
|
192
189
|
SQUEEZED,
|
193
190
|
OPTIMIZED,
|
194
|
-
INLINED,
|
195
191
|
CLEAR_BETWEEN_PASSES,
|
196
192
|
TOP,
|
197
193
|
WRITE_ONLY,
|
@@ -213,16 +209,17 @@ import {
|
|
213
209
|
get_simple_key,
|
214
210
|
has_break_or_continue,
|
215
211
|
maintain_this_binding,
|
216
|
-
|
212
|
+
is_empty,
|
217
213
|
is_identifier_atom,
|
218
|
-
|
214
|
+
is_reachable,
|
219
215
|
is_ref_of,
|
220
216
|
can_be_evicted_from_block,
|
221
217
|
as_statement_array,
|
222
|
-
|
223
|
-
|
218
|
+
retain_top_func,
|
219
|
+
is_func_expr,
|
224
220
|
} from "./common.js";
|
225
221
|
import { tighten_body, trim_unreachable_code } from "./tighten-body.js";
|
222
|
+
import { inline_into_symbolref, inline_into_call } from "./inline.js";
|
226
223
|
|
227
224
|
class Compressor extends TreeWalker {
|
228
225
|
constructor(options, { false_by_default = false, mangle_options = false }) {
|
@@ -571,15 +568,6 @@ AST_SymbolRef.DEFMETHOD("is_immutable", function() {
|
|
571
568
|
return orig.length == 1 && orig[0] instanceof AST_SymbolLambda;
|
572
569
|
});
|
573
570
|
|
574
|
-
function find_scope(tw) {
|
575
|
-
for (let i = 0;;i++) {
|
576
|
-
const p = tw.parent(i);
|
577
|
-
if (p instanceof AST_Toplevel) return p;
|
578
|
-
if (p instanceof AST_Lambda) return p;
|
579
|
-
if (p.block_scope) return p.block_scope;
|
580
|
-
}
|
581
|
-
}
|
582
|
-
|
583
571
|
function find_variable(compressor, name) {
|
584
572
|
var scope, i = 0;
|
585
573
|
while (scope = compressor.parent(i++)) {
|
@@ -592,13 +580,6 @@ function find_variable(compressor, name) {
|
|
592
580
|
return scope.find_variable(name);
|
593
581
|
}
|
594
582
|
|
595
|
-
function is_empty(thing) {
|
596
|
-
if (thing === null) return true;
|
597
|
-
if (thing instanceof AST_EmptyStatement) return true;
|
598
|
-
if (thing instanceof AST_BlockStatement) return thing.body.length == 0;
|
599
|
-
return false;
|
600
|
-
}
|
601
|
-
|
602
583
|
var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError");
|
603
584
|
AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
|
604
585
|
return !this.definition().undeclared
|
@@ -1218,7 +1199,7 @@ AST_Scope.DEFMETHOD("hoist_properties", function(compressor) {
|
|
1218
1199
|
const defs = new Map();
|
1219
1200
|
const assignments = [];
|
1220
1201
|
value.properties.forEach(({ key, value }) => {
|
1221
|
-
const scope = find_scope(
|
1202
|
+
const scope = hoister.find_scope();
|
1222
1203
|
const symbol = self.create_symbol(sym.CTOR, {
|
1223
1204
|
source: sym,
|
1224
1205
|
scope,
|
@@ -2028,15 +2009,6 @@ def_optimize(AST_Import, function(self) {
|
|
2028
2009
|
return self;
|
2029
2010
|
});
|
2030
2011
|
|
2031
|
-
// TODO this only works with AST_Defun, shouldn't it work for other ways of defining functions?
|
2032
|
-
function retain_top_func(fn, compressor) {
|
2033
|
-
return compressor.top_retain
|
2034
|
-
&& fn instanceof AST_Defun
|
2035
|
-
&& has_flag(fn, TOP)
|
2036
|
-
&& fn.name
|
2037
|
-
&& compressor.top_retain(fn.name);
|
2038
|
-
}
|
2039
|
-
|
2040
2012
|
def_optimize(AST_Call, function(self, compressor) {
|
2041
2013
|
var exp = self.expression;
|
2042
2014
|
var fn = exp;
|
@@ -2359,329 +2331,7 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
2359
2331
|
}
|
2360
2332
|
}
|
2361
2333
|
|
2362
|
-
|
2363
|
-
var is_regular_func = is_func && !fn.is_generator && !fn.async;
|
2364
|
-
var can_inline = is_regular_func && compressor.option("inline") && !self.is_callee_pure(compressor);
|
2365
|
-
if (can_inline && stat instanceof AST_Return) {
|
2366
|
-
let returned = stat.value;
|
2367
|
-
if (!returned || returned.is_constant_expression()) {
|
2368
|
-
if (returned) {
|
2369
|
-
returned = returned.clone(true);
|
2370
|
-
} else {
|
2371
|
-
returned = make_node(AST_Undefined, self);
|
2372
|
-
}
|
2373
|
-
const args = self.args.concat(returned);
|
2374
|
-
return make_sequence(self, args).optimize(compressor);
|
2375
|
-
}
|
2376
|
-
|
2377
|
-
// optimize identity function
|
2378
|
-
if (
|
2379
|
-
fn.argnames.length === 1
|
2380
|
-
&& (fn.argnames[0] instanceof AST_SymbolFunarg)
|
2381
|
-
&& self.args.length < 2
|
2382
|
-
&& returned instanceof AST_SymbolRef
|
2383
|
-
&& returned.name === fn.argnames[0].name
|
2384
|
-
) {
|
2385
|
-
const replacement =
|
2386
|
-
(self.args[0] || make_node(AST_Undefined)).optimize(compressor);
|
2387
|
-
|
2388
|
-
let parent;
|
2389
|
-
if (
|
2390
|
-
replacement instanceof AST_PropAccess
|
2391
|
-
&& (parent = compressor.parent()) instanceof AST_Call
|
2392
|
-
&& parent.expression === self
|
2393
|
-
) {
|
2394
|
-
// identity function was being used to remove `this`, like in
|
2395
|
-
//
|
2396
|
-
// id(bag.no_this)(...)
|
2397
|
-
//
|
2398
|
-
// Replace with a larger but more effish (0, bag.no_this) wrapper.
|
2399
|
-
|
2400
|
-
return make_sequence(self, [
|
2401
|
-
make_node(AST_Number, self, { value: 0 }),
|
2402
|
-
replacement
|
2403
|
-
]);
|
2404
|
-
}
|
2405
|
-
// replace call with first argument or undefined if none passed
|
2406
|
-
return replacement;
|
2407
|
-
}
|
2408
|
-
}
|
2409
|
-
|
2410
|
-
if (can_inline) {
|
2411
|
-
var scope, in_loop, level = -1;
|
2412
|
-
let def;
|
2413
|
-
let returned_value;
|
2414
|
-
let nearest_scope;
|
2415
|
-
if (simple_args
|
2416
|
-
&& !fn.uses_arguments
|
2417
|
-
&& !(compressor.parent() instanceof AST_Class)
|
2418
|
-
&& !(fn.name && fn instanceof AST_Function)
|
2419
|
-
&& (returned_value = can_flatten_body(stat))
|
2420
|
-
&& (exp === fn
|
2421
|
-
|| has_annotation(self, _INLINE)
|
2422
|
-
|| compressor.option("unused")
|
2423
|
-
&& (def = exp.definition()).references.length == 1
|
2424
|
-
&& !is_recursive_ref(compressor, def)
|
2425
|
-
&& fn.is_constant_expression(exp.scope))
|
2426
|
-
&& !has_annotation(self, _PURE | _NOINLINE)
|
2427
|
-
&& !fn.contains_this()
|
2428
|
-
&& can_inject_symbols()
|
2429
|
-
&& (nearest_scope = find_scope(compressor))
|
2430
|
-
&& !scope_encloses_variables_in_this_scope(nearest_scope, fn)
|
2431
|
-
&& !(function in_default_assign() {
|
2432
|
-
// Due to the fact function parameters have their own scope
|
2433
|
-
// which can't use `var something` in the function body within,
|
2434
|
-
// we simply don't inline into DefaultAssign.
|
2435
|
-
let i = 0;
|
2436
|
-
let p;
|
2437
|
-
while ((p = compressor.parent(i++))) {
|
2438
|
-
if (p instanceof AST_DefaultAssign) return true;
|
2439
|
-
if (p instanceof AST_Block) break;
|
2440
|
-
}
|
2441
|
-
return false;
|
2442
|
-
})()
|
2443
|
-
&& !(scope instanceof AST_Class)
|
2444
|
-
) {
|
2445
|
-
set_flag(fn, SQUEEZED);
|
2446
|
-
nearest_scope.add_child_scope(fn);
|
2447
|
-
return make_sequence(self, flatten_fn(returned_value)).optimize(compressor);
|
2448
|
-
}
|
2449
|
-
}
|
2450
|
-
|
2451
|
-
if (can_inline && has_annotation(self, _INLINE)) {
|
2452
|
-
set_flag(fn, SQUEEZED);
|
2453
|
-
fn = make_node(fn.CTOR === AST_Defun ? AST_Function : fn.CTOR, fn, fn);
|
2454
|
-
fn = fn.clone(true);
|
2455
|
-
fn.figure_out_scope({}, {
|
2456
|
-
parent_scope: find_scope(compressor),
|
2457
|
-
toplevel: compressor.get_toplevel()
|
2458
|
-
});
|
2459
|
-
|
2460
|
-
return make_node(AST_Call, self, {
|
2461
|
-
expression: fn,
|
2462
|
-
args: self.args,
|
2463
|
-
}).optimize(compressor);
|
2464
|
-
}
|
2465
|
-
|
2466
|
-
const can_drop_this_call = is_regular_func && compressor.option("side_effects") && fn.body.every(is_empty);
|
2467
|
-
if (can_drop_this_call) {
|
2468
|
-
var args = self.args.concat(make_node(AST_Undefined, self));
|
2469
|
-
return make_sequence(self, args).optimize(compressor);
|
2470
|
-
}
|
2471
|
-
|
2472
|
-
if (compressor.option("negate_iife")
|
2473
|
-
&& compressor.parent() instanceof AST_SimpleStatement
|
2474
|
-
&& is_iife_call(self)) {
|
2475
|
-
return self.negate(compressor, true);
|
2476
|
-
}
|
2477
|
-
|
2478
|
-
var ev = self.evaluate(compressor);
|
2479
|
-
if (ev !== self) {
|
2480
|
-
ev = make_node_from_constant(ev, self).optimize(compressor);
|
2481
|
-
return best_of(compressor, ev, self);
|
2482
|
-
}
|
2483
|
-
|
2484
|
-
return self;
|
2485
|
-
|
2486
|
-
function return_value(stat) {
|
2487
|
-
if (!stat) return make_node(AST_Undefined, self);
|
2488
|
-
if (stat instanceof AST_Return) {
|
2489
|
-
if (!stat.value) return make_node(AST_Undefined, self);
|
2490
|
-
return stat.value.clone(true);
|
2491
|
-
}
|
2492
|
-
if (stat instanceof AST_SimpleStatement) {
|
2493
|
-
return make_node(AST_UnaryPrefix, stat, {
|
2494
|
-
operator: "void",
|
2495
|
-
expression: stat.body.clone(true)
|
2496
|
-
});
|
2497
|
-
}
|
2498
|
-
}
|
2499
|
-
|
2500
|
-
function can_flatten_body(stat) {
|
2501
|
-
var body = fn.body;
|
2502
|
-
var len = body.length;
|
2503
|
-
if (compressor.option("inline") < 3) {
|
2504
|
-
return len == 1 && return_value(stat);
|
2505
|
-
}
|
2506
|
-
stat = null;
|
2507
|
-
for (var i = 0; i < len; i++) {
|
2508
|
-
var line = body[i];
|
2509
|
-
if (line instanceof AST_Var) {
|
2510
|
-
if (stat && !line.definitions.every((var_def) =>
|
2511
|
-
!var_def.value
|
2512
|
-
)) {
|
2513
|
-
return false;
|
2514
|
-
}
|
2515
|
-
} else if (stat) {
|
2516
|
-
return false;
|
2517
|
-
} else if (!(line instanceof AST_EmptyStatement)) {
|
2518
|
-
stat = line;
|
2519
|
-
}
|
2520
|
-
}
|
2521
|
-
return return_value(stat);
|
2522
|
-
}
|
2523
|
-
|
2524
|
-
function can_inject_args(block_scoped, safe_to_inject) {
|
2525
|
-
for (var i = 0, len = fn.argnames.length; i < len; i++) {
|
2526
|
-
var arg = fn.argnames[i];
|
2527
|
-
if (arg instanceof AST_DefaultAssign) {
|
2528
|
-
if (has_flag(arg.left, UNUSED)) continue;
|
2529
|
-
return false;
|
2530
|
-
}
|
2531
|
-
if (arg instanceof AST_Destructuring) return false;
|
2532
|
-
if (arg instanceof AST_Expansion) {
|
2533
|
-
if (has_flag(arg.expression, UNUSED)) continue;
|
2534
|
-
return false;
|
2535
|
-
}
|
2536
|
-
if (has_flag(arg, UNUSED)) continue;
|
2537
|
-
if (!safe_to_inject
|
2538
|
-
|| block_scoped.has(arg.name)
|
2539
|
-
|| identifier_atom.has(arg.name)
|
2540
|
-
|| scope.conflicting_def(arg.name)) {
|
2541
|
-
return false;
|
2542
|
-
}
|
2543
|
-
if (in_loop) in_loop.push(arg.definition());
|
2544
|
-
}
|
2545
|
-
return true;
|
2546
|
-
}
|
2547
|
-
|
2548
|
-
function can_inject_vars(block_scoped, safe_to_inject) {
|
2549
|
-
var len = fn.body.length;
|
2550
|
-
for (var i = 0; i < len; i++) {
|
2551
|
-
var stat = fn.body[i];
|
2552
|
-
if (!(stat instanceof AST_Var)) continue;
|
2553
|
-
if (!safe_to_inject) return false;
|
2554
|
-
for (var j = stat.definitions.length; --j >= 0;) {
|
2555
|
-
var name = stat.definitions[j].name;
|
2556
|
-
if (name instanceof AST_Destructuring
|
2557
|
-
|| block_scoped.has(name.name)
|
2558
|
-
|| identifier_atom.has(name.name)
|
2559
|
-
|| scope.conflicting_def(name.name)) {
|
2560
|
-
return false;
|
2561
|
-
}
|
2562
|
-
if (in_loop) in_loop.push(name.definition());
|
2563
|
-
}
|
2564
|
-
}
|
2565
|
-
return true;
|
2566
|
-
}
|
2567
|
-
|
2568
|
-
function can_inject_symbols() {
|
2569
|
-
var block_scoped = new Set();
|
2570
|
-
do {
|
2571
|
-
scope = compressor.parent(++level);
|
2572
|
-
if (scope.is_block_scope() && scope.block_scope) {
|
2573
|
-
// TODO this is sometimes undefined during compression.
|
2574
|
-
// But it should always have a value!
|
2575
|
-
scope.block_scope.variables.forEach(function (variable) {
|
2576
|
-
block_scoped.add(variable.name);
|
2577
|
-
});
|
2578
|
-
}
|
2579
|
-
if (scope instanceof AST_Catch) {
|
2580
|
-
// TODO can we delete? AST_Catch is a block scope.
|
2581
|
-
if (scope.argname) {
|
2582
|
-
block_scoped.add(scope.argname.name);
|
2583
|
-
}
|
2584
|
-
} else if (scope instanceof AST_IterationStatement) {
|
2585
|
-
in_loop = [];
|
2586
|
-
} else if (scope instanceof AST_SymbolRef) {
|
2587
|
-
if (scope.fixed_value() instanceof AST_Scope) return false;
|
2588
|
-
}
|
2589
|
-
} while (!(scope instanceof AST_Scope));
|
2590
|
-
|
2591
|
-
var safe_to_inject = !(scope instanceof AST_Toplevel) || compressor.toplevel.vars;
|
2592
|
-
var inline = compressor.option("inline");
|
2593
|
-
if (!can_inject_vars(block_scoped, inline >= 3 && safe_to_inject)) return false;
|
2594
|
-
if (!can_inject_args(block_scoped, inline >= 2 && safe_to_inject)) return false;
|
2595
|
-
return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop);
|
2596
|
-
}
|
2597
|
-
|
2598
|
-
function append_var(decls, expressions, name, value) {
|
2599
|
-
var def = name.definition();
|
2600
|
-
|
2601
|
-
// Name already exists, only when a function argument had the same name
|
2602
|
-
const already_appended = scope.variables.has(name.name);
|
2603
|
-
if (!already_appended) {
|
2604
|
-
scope.variables.set(name.name, def);
|
2605
|
-
scope.enclosed.push(def);
|
2606
|
-
decls.push(make_node(AST_VarDef, name, {
|
2607
|
-
name: name,
|
2608
|
-
value: null
|
2609
|
-
}));
|
2610
|
-
}
|
2611
|
-
|
2612
|
-
var sym = make_node(AST_SymbolRef, name, name);
|
2613
|
-
def.references.push(sym);
|
2614
|
-
if (value) expressions.push(make_node(AST_Assign, self, {
|
2615
|
-
operator: "=",
|
2616
|
-
logical: false,
|
2617
|
-
left: sym,
|
2618
|
-
right: value.clone()
|
2619
|
-
}));
|
2620
|
-
}
|
2621
|
-
|
2622
|
-
function flatten_args(decls, expressions) {
|
2623
|
-
var len = fn.argnames.length;
|
2624
|
-
for (var i = self.args.length; --i >= len;) {
|
2625
|
-
expressions.push(self.args[i]);
|
2626
|
-
}
|
2627
|
-
for (i = len; --i >= 0;) {
|
2628
|
-
var name = fn.argnames[i];
|
2629
|
-
var value = self.args[i];
|
2630
|
-
if (has_flag(name, UNUSED) || !name.name || scope.conflicting_def(name.name)) {
|
2631
|
-
if (value) expressions.push(value);
|
2632
|
-
} else {
|
2633
|
-
var symbol = make_node(AST_SymbolVar, name, name);
|
2634
|
-
name.definition().orig.push(symbol);
|
2635
|
-
if (!value && in_loop) value = make_node(AST_Undefined, self);
|
2636
|
-
append_var(decls, expressions, symbol, value);
|
2637
|
-
}
|
2638
|
-
}
|
2639
|
-
decls.reverse();
|
2640
|
-
expressions.reverse();
|
2641
|
-
}
|
2642
|
-
|
2643
|
-
function flatten_vars(decls, expressions) {
|
2644
|
-
var pos = expressions.length;
|
2645
|
-
for (var i = 0, lines = fn.body.length; i < lines; i++) {
|
2646
|
-
var stat = fn.body[i];
|
2647
|
-
if (!(stat instanceof AST_Var)) continue;
|
2648
|
-
for (var j = 0, defs = stat.definitions.length; j < defs; j++) {
|
2649
|
-
var var_def = stat.definitions[j];
|
2650
|
-
var name = var_def.name;
|
2651
|
-
append_var(decls, expressions, name, var_def.value);
|
2652
|
-
if (in_loop && fn.argnames.every((argname) =>
|
2653
|
-
argname.name != name.name
|
2654
|
-
)) {
|
2655
|
-
var def = fn.variables.get(name.name);
|
2656
|
-
var sym = make_node(AST_SymbolRef, name, name);
|
2657
|
-
def.references.push(sym);
|
2658
|
-
expressions.splice(pos++, 0, make_node(AST_Assign, var_def, {
|
2659
|
-
operator: "=",
|
2660
|
-
logical: false,
|
2661
|
-
left: sym,
|
2662
|
-
right: make_node(AST_Undefined, name)
|
2663
|
-
}));
|
2664
|
-
}
|
2665
|
-
}
|
2666
|
-
}
|
2667
|
-
}
|
2668
|
-
|
2669
|
-
function flatten_fn(returned_value) {
|
2670
|
-
var decls = [];
|
2671
|
-
var expressions = [];
|
2672
|
-
flatten_args(decls, expressions);
|
2673
|
-
flatten_vars(decls, expressions);
|
2674
|
-
expressions.push(returned_value);
|
2675
|
-
|
2676
|
-
if (decls.length) {
|
2677
|
-
const i = scope.body.indexOf(compressor.parent(level - 1)) + 1;
|
2678
|
-
scope.body.splice(i, 0, make_node(AST_Var, fn, {
|
2679
|
-
definitions: decls
|
2680
|
-
}));
|
2681
|
-
}
|
2682
|
-
|
2683
|
-
return expressions.map(exp => exp.clone(true));
|
2684
|
-
}
|
2334
|
+
return inline_into_call(self, fn, compressor);
|
2685
2335
|
});
|
2686
2336
|
|
2687
2337
|
def_optimize(AST_New, function(self, compressor) {
|
@@ -3334,19 +2984,6 @@ def_optimize(AST_SymbolExport, function(self) {
|
|
3334
2984
|
return self;
|
3335
2985
|
});
|
3336
2986
|
|
3337
|
-
function within_array_or_object_literal(compressor) {
|
3338
|
-
var node, level = 0;
|
3339
|
-
while (node = compressor.parent(level++)) {
|
3340
|
-
if (node instanceof AST_Statement) return false;
|
3341
|
-
if (node instanceof AST_Array
|
3342
|
-
|| node instanceof AST_ObjectKeyVal
|
3343
|
-
|| node instanceof AST_Object) {
|
3344
|
-
return true;
|
3345
|
-
}
|
3346
|
-
}
|
3347
|
-
return false;
|
3348
|
-
}
|
3349
|
-
|
3350
2987
|
def_optimize(AST_SymbolRef, function(self, compressor) {
|
3351
2988
|
if (
|
3352
2989
|
!compressor.option("ie8")
|
@@ -3365,156 +3002,12 @@ def_optimize(AST_SymbolRef, function(self, compressor) {
|
|
3365
3002
|
|
3366
3003
|
const parent = compressor.parent();
|
3367
3004
|
if (compressor.option("reduce_vars") && is_lhs(self, parent) !== self) {
|
3368
|
-
|
3369
|
-
|
3370
|
-
|
3371
|
-
def.fixed = false;
|
3372
|
-
def.single_use = false;
|
3373
|
-
return self;
|
3374
|
-
}
|
3375
|
-
|
3376
|
-
let fixed = self.fixed_value();
|
3377
|
-
let single_use = def.single_use
|
3378
|
-
&& !(parent instanceof AST_Call
|
3379
|
-
&& (parent.is_callee_pure(compressor))
|
3380
|
-
|| has_annotation(parent, _NOINLINE))
|
3381
|
-
&& !(parent instanceof AST_Export
|
3382
|
-
&& fixed instanceof AST_Lambda
|
3383
|
-
&& fixed.name);
|
3384
|
-
|
3385
|
-
if (single_use && fixed instanceof AST_Node) {
|
3386
|
-
single_use =
|
3387
|
-
!fixed.has_side_effects(compressor)
|
3388
|
-
&& !fixed.may_throw(compressor);
|
3389
|
-
}
|
3390
|
-
|
3391
|
-
if (single_use && (fixed instanceof AST_Lambda || fixed instanceof AST_Class)) {
|
3392
|
-
if (retain_top_func(fixed, compressor)) {
|
3393
|
-
single_use = false;
|
3394
|
-
} else if (def.scope !== self.scope
|
3395
|
-
&& (def.escaped == 1
|
3396
|
-
|| has_flag(fixed, INLINED)
|
3397
|
-
|| within_array_or_object_literal(compressor)
|
3398
|
-
|| !compressor.option("reduce_funcs"))) {
|
3399
|
-
single_use = false;
|
3400
|
-
} else if (is_recursive_ref(compressor, def)) {
|
3401
|
-
single_use = false;
|
3402
|
-
} else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) {
|
3403
|
-
single_use = fixed.is_constant_expression(self.scope);
|
3404
|
-
if (single_use == "f") {
|
3405
|
-
var scope = self.scope;
|
3406
|
-
do {
|
3407
|
-
if (scope instanceof AST_Defun || is_func_expr(scope)) {
|
3408
|
-
set_flag(scope, INLINED);
|
3409
|
-
}
|
3410
|
-
} while (scope = scope.parent_scope);
|
3411
|
-
}
|
3412
|
-
}
|
3413
|
-
}
|
3414
|
-
|
3415
|
-
if (single_use && fixed instanceof AST_Lambda) {
|
3416
|
-
single_use =
|
3417
|
-
def.scope === self.scope
|
3418
|
-
&& !scope_encloses_variables_in_this_scope(nearest_scope, fixed)
|
3419
|
-
|| parent instanceof AST_Call
|
3420
|
-
&& parent.expression === self
|
3421
|
-
&& !scope_encloses_variables_in_this_scope(nearest_scope, fixed)
|
3422
|
-
&& !(fixed.name && fixed.name.definition().recursive_refs > 0);
|
3423
|
-
}
|
3424
|
-
|
3425
|
-
if (single_use && fixed) {
|
3426
|
-
if (fixed instanceof AST_DefClass) {
|
3427
|
-
set_flag(fixed, SQUEEZED);
|
3428
|
-
fixed = make_node(AST_ClassExpression, fixed, fixed);
|
3429
|
-
}
|
3430
|
-
if (fixed instanceof AST_Defun) {
|
3431
|
-
set_flag(fixed, SQUEEZED);
|
3432
|
-
fixed = make_node(AST_Function, fixed, fixed);
|
3433
|
-
}
|
3434
|
-
if (def.recursive_refs > 0 && fixed.name instanceof AST_SymbolDefun) {
|
3435
|
-
const defun_def = fixed.name.definition();
|
3436
|
-
let lambda_def = fixed.variables.get(fixed.name.name);
|
3437
|
-
let name = lambda_def && lambda_def.orig[0];
|
3438
|
-
if (!(name instanceof AST_SymbolLambda)) {
|
3439
|
-
name = make_node(AST_SymbolLambda, fixed.name, fixed.name);
|
3440
|
-
name.scope = fixed;
|
3441
|
-
fixed.name = name;
|
3442
|
-
lambda_def = fixed.def_function(name);
|
3443
|
-
}
|
3444
|
-
walk(fixed, node => {
|
3445
|
-
if (node instanceof AST_SymbolRef && node.definition() === defun_def) {
|
3446
|
-
node.thedef = lambda_def;
|
3447
|
-
lambda_def.references.push(node);
|
3448
|
-
}
|
3449
|
-
});
|
3450
|
-
}
|
3451
|
-
if (
|
3452
|
-
(fixed instanceof AST_Lambda || fixed instanceof AST_Class)
|
3453
|
-
&& fixed.parent_scope !== nearest_scope
|
3454
|
-
) {
|
3455
|
-
fixed = fixed.clone(true, compressor.get_toplevel());
|
3456
|
-
|
3457
|
-
nearest_scope.add_child_scope(fixed);
|
3458
|
-
}
|
3459
|
-
return fixed.optimize(compressor);
|
3460
|
-
}
|
3461
|
-
|
3462
|
-
// multiple uses
|
3463
|
-
if (fixed) {
|
3464
|
-
let replace;
|
3465
|
-
|
3466
|
-
if (fixed instanceof AST_This) {
|
3467
|
-
if (!(def.orig[0] instanceof AST_SymbolFunarg)
|
3468
|
-
&& def.references.every((ref) =>
|
3469
|
-
def.scope === ref.scope
|
3470
|
-
)) {
|
3471
|
-
replace = fixed;
|
3472
|
-
}
|
3473
|
-
} else {
|
3474
|
-
var ev = fixed.evaluate(compressor);
|
3475
|
-
if (
|
3476
|
-
ev !== fixed
|
3477
|
-
&& (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))
|
3478
|
-
) {
|
3479
|
-
replace = make_node_from_constant(ev, fixed);
|
3480
|
-
}
|
3481
|
-
}
|
3482
|
-
|
3483
|
-
if (replace) {
|
3484
|
-
const name_length = self.size(compressor);
|
3485
|
-
const replace_size = replace.size(compressor);
|
3486
|
-
|
3487
|
-
let overhead = 0;
|
3488
|
-
if (compressor.option("unused") && !compressor.exposed(def)) {
|
3489
|
-
overhead =
|
3490
|
-
(name_length + 2 + replace_size) /
|
3491
|
-
(def.references.length - def.assignments);
|
3492
|
-
}
|
3493
|
-
|
3494
|
-
if (replace_size <= name_length + overhead) {
|
3495
|
-
return replace;
|
3496
|
-
}
|
3497
|
-
}
|
3498
|
-
}
|
3005
|
+
return inline_into_symbolref(self, compressor);
|
3006
|
+
} else {
|
3007
|
+
return self;
|
3499
3008
|
}
|
3500
|
-
|
3501
|
-
return self;
|
3502
3009
|
});
|
3503
3010
|
|
3504
|
-
function scope_encloses_variables_in_this_scope(scope, pulled_scope) {
|
3505
|
-
for (const enclosed of pulled_scope.enclosed) {
|
3506
|
-
if (pulled_scope.variables.has(enclosed.name)) {
|
3507
|
-
continue;
|
3508
|
-
}
|
3509
|
-
const looked_up = scope.find_variable(enclosed.name);
|
3510
|
-
if (looked_up) {
|
3511
|
-
if (looked_up === enclosed) continue;
|
3512
|
-
return true;
|
3513
|
-
}
|
3514
|
-
}
|
3515
|
-
return false;
|
3516
|
-
}
|
3517
|
-
|
3518
3011
|
function is_atomic(lhs, self) {
|
3519
3012
|
return lhs instanceof AST_SymbolRef || lhs.TYPE === self.TYPE;
|
3520
3013
|
}
|
@@ -3580,34 +3073,6 @@ def_optimize(AST_NaN, function(self, compressor) {
|
|
3580
3073
|
return self;
|
3581
3074
|
});
|
3582
3075
|
|
3583
|
-
function is_reachable(self, defs) {
|
3584
|
-
const find_ref = node => {
|
3585
|
-
if (node instanceof AST_SymbolRef && member(node.definition(), defs)) {
|
3586
|
-
return walk_abort;
|
3587
|
-
}
|
3588
|
-
};
|
3589
|
-
|
3590
|
-
return walk_parent(self, (node, info) => {
|
3591
|
-
if (node instanceof AST_Scope && node !== self) {
|
3592
|
-
var parent = info.parent();
|
3593
|
-
|
3594
|
-
if (
|
3595
|
-
parent instanceof AST_Call
|
3596
|
-
&& parent.expression === node
|
3597
|
-
// Async/Generators aren't guaranteed to sync evaluate all of
|
3598
|
-
// their body steps, so it's possible they close over the variable.
|
3599
|
-
&& !(node.async || node.is_generator)
|
3600
|
-
) {
|
3601
|
-
return;
|
3602
|
-
}
|
3603
|
-
|
3604
|
-
if (walk(node, find_ref)) return walk_abort;
|
3605
|
-
|
3606
|
-
return true;
|
3607
|
-
}
|
3608
|
-
});
|
3609
|
-
}
|
3610
|
-
|
3611
3076
|
const ASSIGN_OPS = makePredicate("+ - / * % >> << >>> | ^ &");
|
3612
3077
|
const ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &");
|
3613
3078
|
def_optimize(AST_Assign, function(self, compressor) {
|
@@ -3616,6 +3081,17 @@ def_optimize(AST_Assign, function(self, compressor) {
|
|
3616
3081
|
}
|
3617
3082
|
|
3618
3083
|
var def;
|
3084
|
+
// x = x ---> x
|
3085
|
+
if (
|
3086
|
+
self.operator === "="
|
3087
|
+
&& self.left instanceof AST_SymbolRef
|
3088
|
+
&& self.left.name !== "arguments"
|
3089
|
+
&& !(def = self.left.definition()).undeclared
|
3090
|
+
&& self.right.equivalent_to(self.left)
|
3091
|
+
) {
|
3092
|
+
return self.right;
|
3093
|
+
}
|
3094
|
+
|
3619
3095
|
if (compressor.option("dead_code")
|
3620
3096
|
&& self.left instanceof AST_SymbolRef
|
3621
3097
|
&& (def = self.left.definition()).scope === compressor.find_parent(AST_Lambda)) {
|
@@ -3638,6 +3114,7 @@ def_optimize(AST_Assign, function(self, compressor) {
|
|
3638
3114
|
|| parent instanceof AST_Sequence && parent.tail_node() === node);
|
3639
3115
|
}
|
3640
3116
|
self = self.lift_sequences(compressor);
|
3117
|
+
|
3641
3118
|
if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
|
3642
3119
|
// x = expr1 OP expr2
|
3643
3120
|
if (self.right.left instanceof AST_SymbolRef
|