terser 5.9.0 → 5.14.2
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 +63 -0
- package/README.md +30 -8
- package/dist/bundle.min.js +5407 -3433
- package/lib/ast.js +1685 -348
- package/lib/cli.js +13 -9
- package/lib/compress/common.js +48 -0
- package/lib/compress/evaluate.js +18 -17
- package/lib/compress/index.js +57 -551
- package/lib/compress/inline.js +641 -0
- package/lib/compress/native-objects.js +1 -0
- package/lib/compress/tighten-body.js +1 -1
- package/lib/equivalent-to.js +81 -107
- package/lib/minify.js +96 -15
- package/lib/mozilla-ast.js +522 -87
- package/lib/output.js +94 -24
- package/lib/parse.js +91 -72
- package/lib/propmangle.js +1 -1
- package/lib/scope.js +27 -4
- package/lib/size.js +6 -8
- package/lib/sourcemap.js +60 -26
- package/lib/utils/index.js +9 -1
- package/package.json +9 -5
- package/tools/domprops.js +12 -0
- package/tools/terser.d.ts +6 -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,12 @@ 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,
|
161
|
+
regexp_is_safe,
|
164
162
|
} from "../utils/index.js";
|
165
163
|
import { first_in_statement } from "../utils/first_in_statement.js";
|
166
164
|
import { equivalent_to } from "../equivalent-to.js";
|
@@ -191,7 +189,6 @@ import {
|
|
191
189
|
import {
|
192
190
|
SQUEEZED,
|
193
191
|
OPTIMIZED,
|
194
|
-
INLINED,
|
195
192
|
CLEAR_BETWEEN_PASSES,
|
196
193
|
TOP,
|
197
194
|
WRITE_ONLY,
|
@@ -213,16 +210,17 @@ import {
|
|
213
210
|
get_simple_key,
|
214
211
|
has_break_or_continue,
|
215
212
|
maintain_this_binding,
|
216
|
-
|
213
|
+
is_empty,
|
217
214
|
is_identifier_atom,
|
218
|
-
|
215
|
+
is_reachable,
|
219
216
|
is_ref_of,
|
220
217
|
can_be_evicted_from_block,
|
221
218
|
as_statement_array,
|
222
|
-
|
223
|
-
|
219
|
+
retain_top_func,
|
220
|
+
is_func_expr,
|
224
221
|
} from "./common.js";
|
225
222
|
import { tighten_body, trim_unreachable_code } from "./tighten-body.js";
|
223
|
+
import { inline_into_symbolref, inline_into_call } from "./inline.js";
|
226
224
|
|
227
225
|
class Compressor extends TreeWalker {
|
228
226
|
constructor(options, { false_by_default = false, mangle_options = false }) {
|
@@ -571,15 +569,6 @@ AST_SymbolRef.DEFMETHOD("is_immutable", function() {
|
|
571
569
|
return orig.length == 1 && orig[0] instanceof AST_SymbolLambda;
|
572
570
|
});
|
573
571
|
|
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
572
|
function find_variable(compressor, name) {
|
584
573
|
var scope, i = 0;
|
585
574
|
while (scope = compressor.parent(i++)) {
|
@@ -592,13 +581,6 @@ function find_variable(compressor, name) {
|
|
592
581
|
return scope.find_variable(name);
|
593
582
|
}
|
594
583
|
|
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
584
|
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
585
|
AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
|
604
586
|
return !this.definition().undeclared
|
@@ -1218,7 +1200,7 @@ AST_Scope.DEFMETHOD("hoist_properties", function(compressor) {
|
|
1218
1200
|
const defs = new Map();
|
1219
1201
|
const assignments = [];
|
1220
1202
|
value.properties.forEach(({ key, value }) => {
|
1221
|
-
const scope = find_scope(
|
1203
|
+
const scope = hoister.find_scope();
|
1222
1204
|
const symbol = self.create_symbol(sym.CTOR, {
|
1223
1205
|
source: sym,
|
1224
1206
|
scope,
|
@@ -1606,7 +1588,7 @@ def_optimize(AST_Switch, function(self, compressor) {
|
|
1606
1588
|
// that way the next micro-optimization will merge them.
|
1607
1589
|
// ** bail micro-optimization if not a simple switch case with breaks
|
1608
1590
|
if (body.every((branch, i) =>
|
1609
|
-
(branch === default_or_exact ||
|
1591
|
+
(branch === default_or_exact || branch.expression instanceof AST_Constant)
|
1610
1592
|
&& (branch.body.length === 0 || aborts(branch) || body.length - 1 === i))
|
1611
1593
|
) {
|
1612
1594
|
for (let i = 0; i < body.length; i++) {
|
@@ -1694,12 +1676,16 @@ def_optimize(AST_Switch, function(self, compressor) {
|
|
1694
1676
|
|
1695
1677
|
|
1696
1678
|
// Prune side-effect free branches that fall into default.
|
1697
|
-
if (default_or_exact) {
|
1679
|
+
DEFAULT: if (default_or_exact) {
|
1698
1680
|
let default_index = body.indexOf(default_or_exact);
|
1699
1681
|
let default_body_index = default_index;
|
1700
1682
|
for (; default_body_index < body.length - 1; default_body_index++) {
|
1701
1683
|
if (!is_inert_body(body[default_body_index])) break;
|
1702
1684
|
}
|
1685
|
+
if (default_body_index < body.length - 1) {
|
1686
|
+
break DEFAULT;
|
1687
|
+
}
|
1688
|
+
|
1703
1689
|
let side_effect_index = body.length - 1;
|
1704
1690
|
for (; side_effect_index >= 0; side_effect_index--) {
|
1705
1691
|
let branch = body[side_effect_index];
|
@@ -2024,15 +2010,6 @@ def_optimize(AST_Import, function(self) {
|
|
2024
2010
|
return self;
|
2025
2011
|
});
|
2026
2012
|
|
2027
|
-
// TODO this only works with AST_Defun, shouldn't it work for other ways of defining functions?
|
2028
|
-
function retain_top_func(fn, compressor) {
|
2029
|
-
return compressor.top_retain
|
2030
|
-
&& fn instanceof AST_Defun
|
2031
|
-
&& has_flag(fn, TOP)
|
2032
|
-
&& fn.name
|
2033
|
-
&& compressor.top_retain(fn.name);
|
2034
|
-
}
|
2035
|
-
|
2036
2013
|
def_optimize(AST_Call, function(self, compressor) {
|
2037
2014
|
var exp = self.expression;
|
2038
2015
|
var fn = exp;
|
@@ -2164,6 +2141,7 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
2164
2141
|
params.push(value);
|
2165
2142
|
return arg !== value;
|
2166
2143
|
})
|
2144
|
+
&& regexp_is_safe(params[0])
|
2167
2145
|
) {
|
2168
2146
|
let [ source, flags ] = params;
|
2169
2147
|
source = regexp_source_fix(new RegExp(source).source);
|
@@ -2355,329 +2333,7 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
2355
2333
|
}
|
2356
2334
|
}
|
2357
2335
|
|
2358
|
-
|
2359
|
-
var is_regular_func = is_func && !fn.is_generator && !fn.async;
|
2360
|
-
var can_inline = is_regular_func && compressor.option("inline") && !self.is_callee_pure(compressor);
|
2361
|
-
if (can_inline && stat instanceof AST_Return) {
|
2362
|
-
let returned = stat.value;
|
2363
|
-
if (!returned || returned.is_constant_expression()) {
|
2364
|
-
if (returned) {
|
2365
|
-
returned = returned.clone(true);
|
2366
|
-
} else {
|
2367
|
-
returned = make_node(AST_Undefined, self);
|
2368
|
-
}
|
2369
|
-
const args = self.args.concat(returned);
|
2370
|
-
return make_sequence(self, args).optimize(compressor);
|
2371
|
-
}
|
2372
|
-
|
2373
|
-
// optimize identity function
|
2374
|
-
if (
|
2375
|
-
fn.argnames.length === 1
|
2376
|
-
&& (fn.argnames[0] instanceof AST_SymbolFunarg)
|
2377
|
-
&& self.args.length < 2
|
2378
|
-
&& returned instanceof AST_SymbolRef
|
2379
|
-
&& returned.name === fn.argnames[0].name
|
2380
|
-
) {
|
2381
|
-
const replacement =
|
2382
|
-
(self.args[0] || make_node(AST_Undefined)).optimize(compressor);
|
2383
|
-
|
2384
|
-
let parent;
|
2385
|
-
if (
|
2386
|
-
replacement instanceof AST_PropAccess
|
2387
|
-
&& (parent = compressor.parent()) instanceof AST_Call
|
2388
|
-
&& parent.expression === self
|
2389
|
-
) {
|
2390
|
-
// identity function was being used to remove `this`, like in
|
2391
|
-
//
|
2392
|
-
// id(bag.no_this)(...)
|
2393
|
-
//
|
2394
|
-
// Replace with a larger but more effish (0, bag.no_this) wrapper.
|
2395
|
-
|
2396
|
-
return make_sequence(self, [
|
2397
|
-
make_node(AST_Number, self, { value: 0 }),
|
2398
|
-
replacement
|
2399
|
-
]);
|
2400
|
-
}
|
2401
|
-
// replace call with first argument or undefined if none passed
|
2402
|
-
return replacement;
|
2403
|
-
}
|
2404
|
-
}
|
2405
|
-
|
2406
|
-
if (can_inline) {
|
2407
|
-
var scope, in_loop, level = -1;
|
2408
|
-
let def;
|
2409
|
-
let returned_value;
|
2410
|
-
let nearest_scope;
|
2411
|
-
if (simple_args
|
2412
|
-
&& !fn.uses_arguments
|
2413
|
-
&& !(compressor.parent() instanceof AST_Class)
|
2414
|
-
&& !(fn.name && fn instanceof AST_Function)
|
2415
|
-
&& (returned_value = can_flatten_body(stat))
|
2416
|
-
&& (exp === fn
|
2417
|
-
|| has_annotation(self, _INLINE)
|
2418
|
-
|| compressor.option("unused")
|
2419
|
-
&& (def = exp.definition()).references.length == 1
|
2420
|
-
&& !is_recursive_ref(compressor, def)
|
2421
|
-
&& fn.is_constant_expression(exp.scope))
|
2422
|
-
&& !has_annotation(self, _PURE | _NOINLINE)
|
2423
|
-
&& !fn.contains_this()
|
2424
|
-
&& can_inject_symbols()
|
2425
|
-
&& (nearest_scope = find_scope(compressor))
|
2426
|
-
&& !scope_encloses_variables_in_this_scope(nearest_scope, fn)
|
2427
|
-
&& !(function in_default_assign() {
|
2428
|
-
// Due to the fact function parameters have their own scope
|
2429
|
-
// which can't use `var something` in the function body within,
|
2430
|
-
// we simply don't inline into DefaultAssign.
|
2431
|
-
let i = 0;
|
2432
|
-
let p;
|
2433
|
-
while ((p = compressor.parent(i++))) {
|
2434
|
-
if (p instanceof AST_DefaultAssign) return true;
|
2435
|
-
if (p instanceof AST_Block) break;
|
2436
|
-
}
|
2437
|
-
return false;
|
2438
|
-
})()
|
2439
|
-
&& !(scope instanceof AST_Class)
|
2440
|
-
) {
|
2441
|
-
set_flag(fn, SQUEEZED);
|
2442
|
-
nearest_scope.add_child_scope(fn);
|
2443
|
-
return make_sequence(self, flatten_fn(returned_value)).optimize(compressor);
|
2444
|
-
}
|
2445
|
-
}
|
2446
|
-
|
2447
|
-
if (can_inline && has_annotation(self, _INLINE)) {
|
2448
|
-
set_flag(fn, SQUEEZED);
|
2449
|
-
fn = make_node(fn.CTOR === AST_Defun ? AST_Function : fn.CTOR, fn, fn);
|
2450
|
-
fn = fn.clone(true);
|
2451
|
-
fn.figure_out_scope({}, {
|
2452
|
-
parent_scope: find_scope(compressor),
|
2453
|
-
toplevel: compressor.get_toplevel()
|
2454
|
-
});
|
2455
|
-
|
2456
|
-
return make_node(AST_Call, self, {
|
2457
|
-
expression: fn,
|
2458
|
-
args: self.args,
|
2459
|
-
}).optimize(compressor);
|
2460
|
-
}
|
2461
|
-
|
2462
|
-
const can_drop_this_call = is_regular_func && compressor.option("side_effects") && fn.body.every(is_empty);
|
2463
|
-
if (can_drop_this_call) {
|
2464
|
-
var args = self.args.concat(make_node(AST_Undefined, self));
|
2465
|
-
return make_sequence(self, args).optimize(compressor);
|
2466
|
-
}
|
2467
|
-
|
2468
|
-
if (compressor.option("negate_iife")
|
2469
|
-
&& compressor.parent() instanceof AST_SimpleStatement
|
2470
|
-
&& is_iife_call(self)) {
|
2471
|
-
return self.negate(compressor, true);
|
2472
|
-
}
|
2473
|
-
|
2474
|
-
var ev = self.evaluate(compressor);
|
2475
|
-
if (ev !== self) {
|
2476
|
-
ev = make_node_from_constant(ev, self).optimize(compressor);
|
2477
|
-
return best_of(compressor, ev, self);
|
2478
|
-
}
|
2479
|
-
|
2480
|
-
return self;
|
2481
|
-
|
2482
|
-
function return_value(stat) {
|
2483
|
-
if (!stat) return make_node(AST_Undefined, self);
|
2484
|
-
if (stat instanceof AST_Return) {
|
2485
|
-
if (!stat.value) return make_node(AST_Undefined, self);
|
2486
|
-
return stat.value.clone(true);
|
2487
|
-
}
|
2488
|
-
if (stat instanceof AST_SimpleStatement) {
|
2489
|
-
return make_node(AST_UnaryPrefix, stat, {
|
2490
|
-
operator: "void",
|
2491
|
-
expression: stat.body.clone(true)
|
2492
|
-
});
|
2493
|
-
}
|
2494
|
-
}
|
2495
|
-
|
2496
|
-
function can_flatten_body(stat) {
|
2497
|
-
var body = fn.body;
|
2498
|
-
var len = body.length;
|
2499
|
-
if (compressor.option("inline") < 3) {
|
2500
|
-
return len == 1 && return_value(stat);
|
2501
|
-
}
|
2502
|
-
stat = null;
|
2503
|
-
for (var i = 0; i < len; i++) {
|
2504
|
-
var line = body[i];
|
2505
|
-
if (line instanceof AST_Var) {
|
2506
|
-
if (stat && !line.definitions.every((var_def) =>
|
2507
|
-
!var_def.value
|
2508
|
-
)) {
|
2509
|
-
return false;
|
2510
|
-
}
|
2511
|
-
} else if (stat) {
|
2512
|
-
return false;
|
2513
|
-
} else if (!(line instanceof AST_EmptyStatement)) {
|
2514
|
-
stat = line;
|
2515
|
-
}
|
2516
|
-
}
|
2517
|
-
return return_value(stat);
|
2518
|
-
}
|
2519
|
-
|
2520
|
-
function can_inject_args(block_scoped, safe_to_inject) {
|
2521
|
-
for (var i = 0, len = fn.argnames.length; i < len; i++) {
|
2522
|
-
var arg = fn.argnames[i];
|
2523
|
-
if (arg instanceof AST_DefaultAssign) {
|
2524
|
-
if (has_flag(arg.left, UNUSED)) continue;
|
2525
|
-
return false;
|
2526
|
-
}
|
2527
|
-
if (arg instanceof AST_Destructuring) return false;
|
2528
|
-
if (arg instanceof AST_Expansion) {
|
2529
|
-
if (has_flag(arg.expression, UNUSED)) continue;
|
2530
|
-
return false;
|
2531
|
-
}
|
2532
|
-
if (has_flag(arg, UNUSED)) continue;
|
2533
|
-
if (!safe_to_inject
|
2534
|
-
|| block_scoped.has(arg.name)
|
2535
|
-
|| identifier_atom.has(arg.name)
|
2536
|
-
|| scope.conflicting_def(arg.name)) {
|
2537
|
-
return false;
|
2538
|
-
}
|
2539
|
-
if (in_loop) in_loop.push(arg.definition());
|
2540
|
-
}
|
2541
|
-
return true;
|
2542
|
-
}
|
2543
|
-
|
2544
|
-
function can_inject_vars(block_scoped, safe_to_inject) {
|
2545
|
-
var len = fn.body.length;
|
2546
|
-
for (var i = 0; i < len; i++) {
|
2547
|
-
var stat = fn.body[i];
|
2548
|
-
if (!(stat instanceof AST_Var)) continue;
|
2549
|
-
if (!safe_to_inject) return false;
|
2550
|
-
for (var j = stat.definitions.length; --j >= 0;) {
|
2551
|
-
var name = stat.definitions[j].name;
|
2552
|
-
if (name instanceof AST_Destructuring
|
2553
|
-
|| block_scoped.has(name.name)
|
2554
|
-
|| identifier_atom.has(name.name)
|
2555
|
-
|| scope.conflicting_def(name.name)) {
|
2556
|
-
return false;
|
2557
|
-
}
|
2558
|
-
if (in_loop) in_loop.push(name.definition());
|
2559
|
-
}
|
2560
|
-
}
|
2561
|
-
return true;
|
2562
|
-
}
|
2563
|
-
|
2564
|
-
function can_inject_symbols() {
|
2565
|
-
var block_scoped = new Set();
|
2566
|
-
do {
|
2567
|
-
scope = compressor.parent(++level);
|
2568
|
-
if (scope.is_block_scope() && scope.block_scope) {
|
2569
|
-
// TODO this is sometimes undefined during compression.
|
2570
|
-
// But it should always have a value!
|
2571
|
-
scope.block_scope.variables.forEach(function (variable) {
|
2572
|
-
block_scoped.add(variable.name);
|
2573
|
-
});
|
2574
|
-
}
|
2575
|
-
if (scope instanceof AST_Catch) {
|
2576
|
-
// TODO can we delete? AST_Catch is a block scope.
|
2577
|
-
if (scope.argname) {
|
2578
|
-
block_scoped.add(scope.argname.name);
|
2579
|
-
}
|
2580
|
-
} else if (scope instanceof AST_IterationStatement) {
|
2581
|
-
in_loop = [];
|
2582
|
-
} else if (scope instanceof AST_SymbolRef) {
|
2583
|
-
if (scope.fixed_value() instanceof AST_Scope) return false;
|
2584
|
-
}
|
2585
|
-
} while (!(scope instanceof AST_Scope));
|
2586
|
-
|
2587
|
-
var safe_to_inject = !(scope instanceof AST_Toplevel) || compressor.toplevel.vars;
|
2588
|
-
var inline = compressor.option("inline");
|
2589
|
-
if (!can_inject_vars(block_scoped, inline >= 3 && safe_to_inject)) return false;
|
2590
|
-
if (!can_inject_args(block_scoped, inline >= 2 && safe_to_inject)) return false;
|
2591
|
-
return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop);
|
2592
|
-
}
|
2593
|
-
|
2594
|
-
function append_var(decls, expressions, name, value) {
|
2595
|
-
var def = name.definition();
|
2596
|
-
|
2597
|
-
// Name already exists, only when a function argument had the same name
|
2598
|
-
const already_appended = scope.variables.has(name.name);
|
2599
|
-
if (!already_appended) {
|
2600
|
-
scope.variables.set(name.name, def);
|
2601
|
-
scope.enclosed.push(def);
|
2602
|
-
decls.push(make_node(AST_VarDef, name, {
|
2603
|
-
name: name,
|
2604
|
-
value: null
|
2605
|
-
}));
|
2606
|
-
}
|
2607
|
-
|
2608
|
-
var sym = make_node(AST_SymbolRef, name, name);
|
2609
|
-
def.references.push(sym);
|
2610
|
-
if (value) expressions.push(make_node(AST_Assign, self, {
|
2611
|
-
operator: "=",
|
2612
|
-
logical: false,
|
2613
|
-
left: sym,
|
2614
|
-
right: value.clone()
|
2615
|
-
}));
|
2616
|
-
}
|
2617
|
-
|
2618
|
-
function flatten_args(decls, expressions) {
|
2619
|
-
var len = fn.argnames.length;
|
2620
|
-
for (var i = self.args.length; --i >= len;) {
|
2621
|
-
expressions.push(self.args[i]);
|
2622
|
-
}
|
2623
|
-
for (i = len; --i >= 0;) {
|
2624
|
-
var name = fn.argnames[i];
|
2625
|
-
var value = self.args[i];
|
2626
|
-
if (has_flag(name, UNUSED) || !name.name || scope.conflicting_def(name.name)) {
|
2627
|
-
if (value) expressions.push(value);
|
2628
|
-
} else {
|
2629
|
-
var symbol = make_node(AST_SymbolVar, name, name);
|
2630
|
-
name.definition().orig.push(symbol);
|
2631
|
-
if (!value && in_loop) value = make_node(AST_Undefined, self);
|
2632
|
-
append_var(decls, expressions, symbol, value);
|
2633
|
-
}
|
2634
|
-
}
|
2635
|
-
decls.reverse();
|
2636
|
-
expressions.reverse();
|
2637
|
-
}
|
2638
|
-
|
2639
|
-
function flatten_vars(decls, expressions) {
|
2640
|
-
var pos = expressions.length;
|
2641
|
-
for (var i = 0, lines = fn.body.length; i < lines; i++) {
|
2642
|
-
var stat = fn.body[i];
|
2643
|
-
if (!(stat instanceof AST_Var)) continue;
|
2644
|
-
for (var j = 0, defs = stat.definitions.length; j < defs; j++) {
|
2645
|
-
var var_def = stat.definitions[j];
|
2646
|
-
var name = var_def.name;
|
2647
|
-
append_var(decls, expressions, name, var_def.value);
|
2648
|
-
if (in_loop && fn.argnames.every((argname) =>
|
2649
|
-
argname.name != name.name
|
2650
|
-
)) {
|
2651
|
-
var def = fn.variables.get(name.name);
|
2652
|
-
var sym = make_node(AST_SymbolRef, name, name);
|
2653
|
-
def.references.push(sym);
|
2654
|
-
expressions.splice(pos++, 0, make_node(AST_Assign, var_def, {
|
2655
|
-
operator: "=",
|
2656
|
-
logical: false,
|
2657
|
-
left: sym,
|
2658
|
-
right: make_node(AST_Undefined, name)
|
2659
|
-
}));
|
2660
|
-
}
|
2661
|
-
}
|
2662
|
-
}
|
2663
|
-
}
|
2664
|
-
|
2665
|
-
function flatten_fn(returned_value) {
|
2666
|
-
var decls = [];
|
2667
|
-
var expressions = [];
|
2668
|
-
flatten_args(decls, expressions);
|
2669
|
-
flatten_vars(decls, expressions);
|
2670
|
-
expressions.push(returned_value);
|
2671
|
-
|
2672
|
-
if (decls.length) {
|
2673
|
-
const i = scope.body.indexOf(compressor.parent(level - 1)) + 1;
|
2674
|
-
scope.body.splice(i, 0, make_node(AST_Var, fn, {
|
2675
|
-
definitions: decls
|
2676
|
-
}));
|
2677
|
-
}
|
2678
|
-
|
2679
|
-
return expressions.map(exp => exp.clone(true));
|
2680
|
-
}
|
2336
|
+
return inline_into_call(self, fn, compressor);
|
2681
2337
|
});
|
2682
2338
|
|
2683
2339
|
def_optimize(AST_New, function(self, compressor) {
|
@@ -2746,16 +2402,16 @@ def_optimize(AST_UnaryPostfix, function(self, compressor) {
|
|
2746
2402
|
|
2747
2403
|
def_optimize(AST_UnaryPrefix, function(self, compressor) {
|
2748
2404
|
var e = self.expression;
|
2749
|
-
if (
|
2750
|
-
|
2751
|
-
|
2752
|
-
|
2753
|
-
|
2754
|
-
|
2755
|
-
|
2756
|
-
|
2757
|
-
|
2758
|
-
return make_sequence(self, [
|
2405
|
+
if (
|
2406
|
+
self.operator == "delete" &&
|
2407
|
+
!(
|
2408
|
+
e instanceof AST_SymbolRef ||
|
2409
|
+
e instanceof AST_PropAccess ||
|
2410
|
+
e instanceof AST_Chain ||
|
2411
|
+
is_identifier_atom(e)
|
2412
|
+
)
|
2413
|
+
) {
|
2414
|
+
return make_sequence(self, [e, make_node(AST_True, self)]).optimize(compressor);
|
2759
2415
|
}
|
2760
2416
|
var seq = self.lift_sequences(compressor);
|
2761
2417
|
if (seq !== self) {
|
@@ -3330,19 +2986,6 @@ def_optimize(AST_SymbolExport, function(self) {
|
|
3330
2986
|
return self;
|
3331
2987
|
});
|
3332
2988
|
|
3333
|
-
function within_array_or_object_literal(compressor) {
|
3334
|
-
var node, level = 0;
|
3335
|
-
while (node = compressor.parent(level++)) {
|
3336
|
-
if (node instanceof AST_Statement) return false;
|
3337
|
-
if (node instanceof AST_Array
|
3338
|
-
|| node instanceof AST_ObjectKeyVal
|
3339
|
-
|| node instanceof AST_Object) {
|
3340
|
-
return true;
|
3341
|
-
}
|
3342
|
-
}
|
3343
|
-
return false;
|
3344
|
-
}
|
3345
|
-
|
3346
2989
|
def_optimize(AST_SymbolRef, function(self, compressor) {
|
3347
2990
|
if (
|
3348
2991
|
!compressor.option("ie8")
|
@@ -3361,156 +3004,12 @@ def_optimize(AST_SymbolRef, function(self, compressor) {
|
|
3361
3004
|
|
3362
3005
|
const parent = compressor.parent();
|
3363
3006
|
if (compressor.option("reduce_vars") && is_lhs(self, parent) !== self) {
|
3364
|
-
|
3365
|
-
|
3366
|
-
|
3367
|
-
def.fixed = false;
|
3368
|
-
def.single_use = false;
|
3369
|
-
return self;
|
3370
|
-
}
|
3371
|
-
|
3372
|
-
let fixed = self.fixed_value();
|
3373
|
-
let single_use = def.single_use
|
3374
|
-
&& !(parent instanceof AST_Call
|
3375
|
-
&& (parent.is_callee_pure(compressor))
|
3376
|
-
|| has_annotation(parent, _NOINLINE))
|
3377
|
-
&& !(parent instanceof AST_Export
|
3378
|
-
&& fixed instanceof AST_Lambda
|
3379
|
-
&& fixed.name);
|
3380
|
-
|
3381
|
-
if (single_use && fixed instanceof AST_Node) {
|
3382
|
-
single_use =
|
3383
|
-
!fixed.has_side_effects(compressor)
|
3384
|
-
&& !fixed.may_throw(compressor);
|
3385
|
-
}
|
3386
|
-
|
3387
|
-
if (single_use && (fixed instanceof AST_Lambda || fixed instanceof AST_Class)) {
|
3388
|
-
if (retain_top_func(fixed, compressor)) {
|
3389
|
-
single_use = false;
|
3390
|
-
} else if (def.scope !== self.scope
|
3391
|
-
&& (def.escaped == 1
|
3392
|
-
|| has_flag(fixed, INLINED)
|
3393
|
-
|| within_array_or_object_literal(compressor)
|
3394
|
-
|| !compressor.option("reduce_funcs"))) {
|
3395
|
-
single_use = false;
|
3396
|
-
} else if (is_recursive_ref(compressor, def)) {
|
3397
|
-
single_use = false;
|
3398
|
-
} else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) {
|
3399
|
-
single_use = fixed.is_constant_expression(self.scope);
|
3400
|
-
if (single_use == "f") {
|
3401
|
-
var scope = self.scope;
|
3402
|
-
do {
|
3403
|
-
if (scope instanceof AST_Defun || is_func_expr(scope)) {
|
3404
|
-
set_flag(scope, INLINED);
|
3405
|
-
}
|
3406
|
-
} while (scope = scope.parent_scope);
|
3407
|
-
}
|
3408
|
-
}
|
3409
|
-
}
|
3410
|
-
|
3411
|
-
if (single_use && fixed instanceof AST_Lambda) {
|
3412
|
-
single_use =
|
3413
|
-
def.scope === self.scope
|
3414
|
-
&& !scope_encloses_variables_in_this_scope(nearest_scope, fixed)
|
3415
|
-
|| parent instanceof AST_Call
|
3416
|
-
&& parent.expression === self
|
3417
|
-
&& !scope_encloses_variables_in_this_scope(nearest_scope, fixed)
|
3418
|
-
&& !(fixed.name && fixed.name.definition().recursive_refs > 0);
|
3419
|
-
}
|
3420
|
-
|
3421
|
-
if (single_use && fixed) {
|
3422
|
-
if (fixed instanceof AST_DefClass) {
|
3423
|
-
set_flag(fixed, SQUEEZED);
|
3424
|
-
fixed = make_node(AST_ClassExpression, fixed, fixed);
|
3425
|
-
}
|
3426
|
-
if (fixed instanceof AST_Defun) {
|
3427
|
-
set_flag(fixed, SQUEEZED);
|
3428
|
-
fixed = make_node(AST_Function, fixed, fixed);
|
3429
|
-
}
|
3430
|
-
if (def.recursive_refs > 0 && fixed.name instanceof AST_SymbolDefun) {
|
3431
|
-
const defun_def = fixed.name.definition();
|
3432
|
-
let lambda_def = fixed.variables.get(fixed.name.name);
|
3433
|
-
let name = lambda_def && lambda_def.orig[0];
|
3434
|
-
if (!(name instanceof AST_SymbolLambda)) {
|
3435
|
-
name = make_node(AST_SymbolLambda, fixed.name, fixed.name);
|
3436
|
-
name.scope = fixed;
|
3437
|
-
fixed.name = name;
|
3438
|
-
lambda_def = fixed.def_function(name);
|
3439
|
-
}
|
3440
|
-
walk(fixed, node => {
|
3441
|
-
if (node instanceof AST_SymbolRef && node.definition() === defun_def) {
|
3442
|
-
node.thedef = lambda_def;
|
3443
|
-
lambda_def.references.push(node);
|
3444
|
-
}
|
3445
|
-
});
|
3446
|
-
}
|
3447
|
-
if (
|
3448
|
-
(fixed instanceof AST_Lambda || fixed instanceof AST_Class)
|
3449
|
-
&& fixed.parent_scope !== nearest_scope
|
3450
|
-
) {
|
3451
|
-
fixed = fixed.clone(true, compressor.get_toplevel());
|
3452
|
-
|
3453
|
-
nearest_scope.add_child_scope(fixed);
|
3454
|
-
}
|
3455
|
-
return fixed.optimize(compressor);
|
3456
|
-
}
|
3457
|
-
|
3458
|
-
// multiple uses
|
3459
|
-
if (fixed) {
|
3460
|
-
let replace;
|
3461
|
-
|
3462
|
-
if (fixed instanceof AST_This) {
|
3463
|
-
if (!(def.orig[0] instanceof AST_SymbolFunarg)
|
3464
|
-
&& def.references.every((ref) =>
|
3465
|
-
def.scope === ref.scope
|
3466
|
-
)) {
|
3467
|
-
replace = fixed;
|
3468
|
-
}
|
3469
|
-
} else {
|
3470
|
-
var ev = fixed.evaluate(compressor);
|
3471
|
-
if (
|
3472
|
-
ev !== fixed
|
3473
|
-
&& (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))
|
3474
|
-
) {
|
3475
|
-
replace = make_node_from_constant(ev, fixed);
|
3476
|
-
}
|
3477
|
-
}
|
3478
|
-
|
3479
|
-
if (replace) {
|
3480
|
-
const name_length = self.size(compressor);
|
3481
|
-
const replace_size = replace.size(compressor);
|
3482
|
-
|
3483
|
-
let overhead = 0;
|
3484
|
-
if (compressor.option("unused") && !compressor.exposed(def)) {
|
3485
|
-
overhead =
|
3486
|
-
(name_length + 2 + replace_size) /
|
3487
|
-
(def.references.length - def.assignments);
|
3488
|
-
}
|
3489
|
-
|
3490
|
-
if (replace_size <= name_length + overhead) {
|
3491
|
-
return replace;
|
3492
|
-
}
|
3493
|
-
}
|
3494
|
-
}
|
3007
|
+
return inline_into_symbolref(self, compressor);
|
3008
|
+
} else {
|
3009
|
+
return self;
|
3495
3010
|
}
|
3496
|
-
|
3497
|
-
return self;
|
3498
3011
|
});
|
3499
3012
|
|
3500
|
-
function scope_encloses_variables_in_this_scope(scope, pulled_scope) {
|
3501
|
-
for (const enclosed of pulled_scope.enclosed) {
|
3502
|
-
if (pulled_scope.variables.has(enclosed.name)) {
|
3503
|
-
continue;
|
3504
|
-
}
|
3505
|
-
const looked_up = scope.find_variable(enclosed.name);
|
3506
|
-
if (looked_up) {
|
3507
|
-
if (looked_up === enclosed) continue;
|
3508
|
-
return true;
|
3509
|
-
}
|
3510
|
-
}
|
3511
|
-
return false;
|
3512
|
-
}
|
3513
|
-
|
3514
3013
|
function is_atomic(lhs, self) {
|
3515
3014
|
return lhs instanceof AST_SymbolRef || lhs.TYPE === self.TYPE;
|
3516
3015
|
}
|
@@ -3576,26 +3075,6 @@ def_optimize(AST_NaN, function(self, compressor) {
|
|
3576
3075
|
return self;
|
3577
3076
|
});
|
3578
3077
|
|
3579
|
-
function is_reachable(self, defs) {
|
3580
|
-
const find_ref = node => {
|
3581
|
-
if (node instanceof AST_SymbolRef && member(node.definition(), defs)) {
|
3582
|
-
return walk_abort;
|
3583
|
-
}
|
3584
|
-
};
|
3585
|
-
|
3586
|
-
return walk_parent(self, (node, info) => {
|
3587
|
-
if (node instanceof AST_Scope && node !== self) {
|
3588
|
-
var parent = info.parent();
|
3589
|
-
|
3590
|
-
if (parent instanceof AST_Call && parent.expression === node) return;
|
3591
|
-
|
3592
|
-
if (walk(node, find_ref)) return walk_abort;
|
3593
|
-
|
3594
|
-
return true;
|
3595
|
-
}
|
3596
|
-
});
|
3597
|
-
}
|
3598
|
-
|
3599
3078
|
const ASSIGN_OPS = makePredicate("+ - / * % >> << >>> | ^ &");
|
3600
3079
|
const ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &");
|
3601
3080
|
def_optimize(AST_Assign, function(self, compressor) {
|
@@ -3604,6 +3083,17 @@ def_optimize(AST_Assign, function(self, compressor) {
|
|
3604
3083
|
}
|
3605
3084
|
|
3606
3085
|
var def;
|
3086
|
+
// x = x ---> x
|
3087
|
+
if (
|
3088
|
+
self.operator === "="
|
3089
|
+
&& self.left instanceof AST_SymbolRef
|
3090
|
+
&& self.left.name !== "arguments"
|
3091
|
+
&& !(def = self.left.definition()).undeclared
|
3092
|
+
&& self.right.equivalent_to(self.left)
|
3093
|
+
) {
|
3094
|
+
return self.right;
|
3095
|
+
}
|
3096
|
+
|
3607
3097
|
if (compressor.option("dead_code")
|
3608
3098
|
&& self.left instanceof AST_SymbolRef
|
3609
3099
|
&& (def = self.left.definition()).scope === compressor.find_parent(AST_Lambda)) {
|
@@ -3626,6 +3116,7 @@ def_optimize(AST_Assign, function(self, compressor) {
|
|
3626
3116
|
|| parent instanceof AST_Sequence && parent.tail_node() === node);
|
3627
3117
|
}
|
3628
3118
|
self = self.lift_sequences(compressor);
|
3119
|
+
|
3629
3120
|
if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
|
3630
3121
|
// x = expr1 OP expr2
|
3631
3122
|
if (self.right.left instanceof AST_SymbolRef
|
@@ -4239,7 +3730,16 @@ def_optimize(AST_Sub, function(self, compressor) {
|
|
4239
3730
|
});
|
4240
3731
|
|
4241
3732
|
def_optimize(AST_Chain, function (self, compressor) {
|
4242
|
-
if (is_nullish(self.expression, compressor))
|
3733
|
+
if (is_nullish(self.expression, compressor)) {
|
3734
|
+
let parent = compressor.parent();
|
3735
|
+
// It's valid to delete a nullish optional chain, but if we optimized
|
3736
|
+
// this to `delete undefined` then it would appear to be a syntax error
|
3737
|
+
// when we try to optimize the delete. Thankfully, `delete 0` is fine.
|
3738
|
+
if (parent instanceof AST_UnaryPrefix && parent.operator === "delete") {
|
3739
|
+
return make_node_from_constant(0, self);
|
3740
|
+
}
|
3741
|
+
return make_node(AST_Undefined, self);
|
3742
|
+
}
|
4243
3743
|
return self;
|
4244
3744
|
});
|
4245
3745
|
|
@@ -4301,6 +3801,12 @@ def_optimize(AST_Dot, function(self, compressor) {
|
|
4301
3801
|
const sub = self.flatten_object(self.property, compressor);
|
4302
3802
|
if (sub) return sub.optimize(compressor);
|
4303
3803
|
}
|
3804
|
+
|
3805
|
+
if (self.expression instanceof AST_PropAccess
|
3806
|
+
&& parent instanceof AST_PropAccess) {
|
3807
|
+
return self;
|
3808
|
+
}
|
3809
|
+
|
4304
3810
|
let ev = self.evaluate(compressor);
|
4305
3811
|
if (ev !== self) {
|
4306
3812
|
ev = make_node_from_constant(ev, self).optimize(compressor);
|