diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-04-19 23:37:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-20 06:37:46 +0800 |
commit | 88504ab86995b33b1c497e44ef314ab54fd57355 (patch) | |
tree | 9de9c280e40dbfa910497d904ddfd685b53896b1 /lib/compress.js | |
parent | e38754e8021c46b08595a4c71f26d20b2d538409 (diff) | |
download | tracifyjs-88504ab86995b33b1c497e44ef314ab54fd57355.tar.gz tracifyjs-88504ab86995b33b1c497e44ef314ab54fd57355.zip |
enhance `join_vars` (#3804)
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/lib/compress.js b/lib/compress.js index 6742aef3..e40e1d2e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2447,6 +2447,7 @@ merge(Compressor.prototype, { statements[++j] = stat; defs = stat; } + continue; } else if (stat instanceof AST_Exit) { stat.value = join_assigns_expr(stat.value); } else if (stat instanceof AST_For) { @@ -2454,21 +2455,20 @@ merge(Compressor.prototype, { if (exprs) { CHANGED = true; stat.init = exprs.length ? make_sequence(stat.init, exprs) : null; - statements[++j] = stat; } else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) { if (stat.init) { prev.definitions = prev.definitions.concat(stat.init.definitions); } - stat.init = prev; - statements[j] = stat; + defs = stat.init = prev; + statements[j] = merge_defns(stat); CHANGED = true; + continue; } else if (defs && stat.init && defs.TYPE == stat.init.TYPE && declarations_only(stat.init)) { defs.definitions = defs.definitions.concat(stat.init.definitions); stat.init = null; - statements[++j] = stat; CHANGED = true; - } else { - statements[++j] = stat; + } else if (stat.init instanceof AST_Definitions) { + defs = stat.init; } } else if (stat instanceof AST_ForIn) { stat.object = join_assigns_expr(stat.object); @@ -2481,19 +2481,16 @@ merge(Compressor.prototype, { if (!exprs.length) continue; stat.body = make_sequence(stat.body, exprs); } - statements[++j] = stat; } else if (stat instanceof AST_Switch) { stat.expression = join_assigns_expr(stat.expression); } else if (stat instanceof AST_With) { stat.expression = join_assigns_expr(stat.expression); - } else { - statements[++j] = stat; } + statements[++j] = defs ? merge_defns(stat) : stat; } statements.length = j + 1; function join_assigns_expr(value) { - statements[++j] = stat; var exprs = join_assigns(prev, value, 1); if (!exprs) return value; CHANGED = true; @@ -2501,6 +2498,23 @@ merge(Compressor.prototype, { if (exprs[exprs.length - 1] !== tail) exprs.push(tail.left); return make_sequence(value, exprs); } + + function merge_defns(stat) { + return stat.transform(new TreeTransformer(function(node, descend, in_list) { + if (node instanceof AST_Definitions) { + if (defs === node) return node; + if (defs.TYPE != node.TYPE) return node; + var parent = this.parent(); + if (parent instanceof AST_ForIn && parent.init === node) return node; + if (!declarations_only(node)) return node; + defs.definitions = defs.definitions.concat(node.definitions); + CHANGED = true; + return in_list ? List.skip : make_node(AST_EmptyStatement, node); + } + if (node instanceof AST_Scope) return node; + if (!(node instanceof AST_Statement)) return node; + })); + } } } |