aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-04-16 22:31:33 +0100
committerGitHub <noreply@github.com>2020-04-17 05:31:33 +0800
commit0ce71bbec0c9f0d49ec86f8b959f12a7bf9df21d (patch)
treef02d7f7b8604eee716e86bd35b760c717212e758 /lib
parent46d142cbf6f14692063fe52b807955ee52704a7b (diff)
downloadtracifyjs-0ce71bbec0c9f0d49ec86f8b959f12a7bf9df21d.tar.gz
tracifyjs-0ce71bbec0c9f0d49ec86f8b959f12a7bf9df21d.zip
enhance `join_vars` (#3783)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js37
1 files changed, 32 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js
index e5b7bf09..18cbd05e 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2338,10 +2338,7 @@ merge(Compressor.prototype, {
exprs = body.expressions.slice();
}
if (!exprs) return;
- if (defn instanceof AST_Definitions) {
- var def = defn.definitions[defn.definitions.length - 1];
- if (trim_assigns(def.name, def.value, exprs)) return exprs;
- }
+ var trimmed = false;
for (var i = exprs.length - 1; --i >= 0;) {
var expr = exprs[i];
if (!(expr instanceof AST_Assign)) continue;
@@ -2349,8 +2346,38 @@ merge(Compressor.prototype, {
if (!(expr.left instanceof AST_SymbolRef)) continue;
var tail = exprs.slice(i + 1);
if (!trim_assigns(expr.left, expr.right, tail)) continue;
- return exprs.slice(0, i + 1).concat(tail);
+ trimmed = true;
+ exprs = exprs.slice(0, i + 1).concat(tail);
}
+ if (defn instanceof AST_Definitions) {
+ var def = defn.definitions[defn.definitions.length - 1];
+ if (trim_assigns(def.name, def.value, exprs)) trimmed = true;
+ if (join_var_assign(defn.definitions, exprs)) trimmed = true;
+ }
+ return trimmed && exprs;
+ }
+
+ function join_var_assign(definitions, exprs) {
+ var trimmed = false;
+ while (exprs.length) {
+ var expr = exprs[0];
+ if (!(expr instanceof AST_Assign)) break;
+ if (expr.operator != "=") break;
+ var lhs = expr.left;
+ if (!(lhs instanceof AST_SymbolRef)) break;
+ var def = lhs.definition();
+ if (def.scope !== scope) break;
+ var name = make_node(AST_SymbolVar, lhs, lhs);
+ definitions.push(make_node(AST_VarDef, expr, {
+ name: name,
+ value: expr.right
+ }));
+ def.orig.push(name);
+ def.replaced++;
+ exprs.shift();
+ trimmed = true;
+ }
+ return trimmed;
}
function trim_assigns(name, value, exprs) {