aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-04-18 22:04:21 +0100
committerGitHub <noreply@github.com>2020-04-19 05:04:21 +0800
commiteb6f32bfc3c9af65f417e2e4440115cec21ac075 (patch)
treedad9cc96442a75542fe88b07137676d8d6854bab /lib
parentf110601fb4bfa8f7b362bcc4a7a8a2f850e5132e (diff)
downloadtracifyjs-eb6f32bfc3c9af65f417e2e4440115cec21ac075.tar.gz
tracifyjs-eb6f32bfc3c9af65f417e2e4440115cec21ac075.zip
enhance `collapse_vars` (#3801)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js16
1 files changed, 10 insertions, 6 deletions
diff --git a/lib/compress.js b/lib/compress.js
index b7a91ea5..b0e25dde 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1122,7 +1122,10 @@ merge(Compressor.prototype, {
function collapse(statements, compressor) {
if (scope.pinned()) return statements;
var args;
+ var assignments = Object.create(null);
var candidates = [];
+ var declare_only = Object.create(null);
+ var force_single;
var stat_index = statements.length;
var scanner = new TreeTransformer(function(node, descend) {
if (abort) return node;
@@ -1255,7 +1258,6 @@ merge(Compressor.prototype, {
// Skip (non-executed) functions and (leading) default case in switch statements
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
}, patch_sequence);
- var force_single;
while (--stat_index >= 0) {
// Treat parameters as collapsible in IIFE, i.e.
// function(a, b){ ... }(x());
@@ -1264,7 +1266,6 @@ merge(Compressor.prototype, {
if (stat_index == 0 && compressor.option("unused")) extract_args();
// Find collapsible assignments
var hit_stack = [];
- var declare_only = Object.create(null);
extract_candidates(statements[stat_index]);
while (candidates.length > 0) {
hit_stack = candidates.pop();
@@ -1501,6 +1502,9 @@ merge(Compressor.prototype, {
candidates.push(hit_stack.slice());
extract_candidates(expr.left);
extract_candidates(expr.right);
+ if (expr.left instanceof AST_SymbolRef) {
+ assignments[expr.left.name] = (assignments[expr.left.name] || 0) + 1;
+ }
} else if (expr instanceof AST_Binary) {
extract_candidates(expr.left);
extract_candidates(expr.right);
@@ -1740,14 +1744,13 @@ merge(Compressor.prototype, {
if (expr instanceof AST_VarDef) {
var def = expr.name.definition();
if (!member(expr.name, def.orig)) return;
- var referenced = def.references.length - def.replaced;
- var declared = def.orig.length - def.eliminated;
- declared -= declare_only[def.name] || 0;
+ var declared = def.orig.length - def.eliminated - (declare_only[def.name] || 0);
+ var referenced = def.references.length - def.replaced - (assignments[def.name] || 0);
if (declared > 1 && !(expr.name instanceof AST_SymbolFunarg)) {
mangleable_var(expr.value);
return make_node(AST_SymbolRef, expr.name, expr.name);
}
- if (referenced > 1 ? mangleable_var(expr.value) : !compressor.exposed(def)) {
+ if (mangleable_var(expr.value) || referenced == 1 && !compressor.exposed(def)) {
return make_node(AST_SymbolRef, expr.name, expr.name);
}
} else if (expr instanceof AST_Assign) {
@@ -1871,6 +1874,7 @@ merge(Compressor.prototype, {
found = true;
if (node instanceof AST_VarDef) {
node.value = null;
+ declare_only[node.name.name] = (declare_only[node.name.name] || 0) + 1;
if (value_def) value_def.replaced++;
return node;
}