diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-11-28 03:57:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-28 03:57:10 +0800 |
commit | 168ae747ad8c8c48a0318eaaffd25e084521fb60 (patch) | |
tree | 5462f8183e3079c1de8aee7d5c09376d9fa522e8 /lib | |
parent | d4b701067805f5041c3b27225742a7b36c3db90c (diff) | |
download | tracifyjs-168ae747ad8c8c48a0318eaaffd25e084521fb60.tar.gz tracifyjs-168ae747ad8c8c48a0318eaaffd25e084521fb60.zip |
enhance `collapse_vars` (#3611)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/lib/compress.js b/lib/compress.js index 19fba0b6..686a8d42 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1415,7 +1415,9 @@ merge(Compressor.prototype, { function extract_candidates(expr) { hit_stack.push(expr); - if (expr instanceof AST_Assign) { + if (expr instanceof AST_Array) { + expr.elements.forEach(extract_candidates); + } else if (expr instanceof AST_Assign) { candidates.push(hit_stack.slice()); extract_candidates(expr.left); extract_candidates(expr.right); @@ -1462,6 +1464,14 @@ merge(Compressor.prototype, { if (expr.alternative && !(expr.alternative instanceof AST_Block)) { extract_candidates(expr.alternative); } + } else if (expr instanceof AST_Object) { + expr.properties.forEach(function(prop) { + if (prop instanceof AST_ObjectKeyVal) { + hit_stack.push(prop); + extract_candidates(prop.value); + hit_stack.pop(); + } + }); } else if (expr instanceof AST_Sequence) { expr.expressions.forEach(extract_candidates); } else if (expr instanceof AST_SimpleStatement) { @@ -1492,6 +1502,7 @@ merge(Compressor.prototype, { function find_stop(node, level) { var parent = scanner.parent(level); + if (parent instanceof AST_Array) return node; if (parent instanceof AST_Assign) return node; if (parent instanceof AST_Binary) return node; if (parent instanceof AST_Call) return node; @@ -1501,6 +1512,7 @@ merge(Compressor.prototype, { if (parent instanceof AST_Exit) return node; if (parent instanceof AST_If) return node; if (parent instanceof AST_IterationStatement) return node; + if (parent instanceof AST_ObjectKeyVal) return node; if (parent instanceof AST_PropAccess) return node; if (parent instanceof AST_Sequence) { return (parent.tail_node() === node ? find_stop : find_stop_unused)(parent, level + 1); @@ -1516,6 +1528,7 @@ merge(Compressor.prototype, { var parent = scanner.parent(level); if (is_last_node(node, parent)) return node; if (in_conditional(node, parent)) return node; + if (parent instanceof AST_Array) return find_stop_unused(parent, level + 1); if (parent instanceof AST_Assign) return find_stop_unused(parent, level + 1); if (parent instanceof AST_Binary) return find_stop_unused(parent, level + 1); if (parent instanceof AST_Call) return find_stop_unused(parent, level + 1); @@ -1525,6 +1538,7 @@ merge(Compressor.prototype, { if (parent instanceof AST_Exit) return find_stop_unused(parent, level + 1); if (parent instanceof AST_If) return find_stop_unused(parent, level + 1); if (parent instanceof AST_IterationStatement) return node; + if (parent instanceof AST_ObjectKeyVal) return find_stop_unused(scanner.parent(level + 1), level + 2); if (parent instanceof AST_PropAccess) { var exp = parent.expression; if (exp === node) return find_stop_unused(parent, level + 1); @@ -1668,20 +1682,21 @@ merge(Compressor.prototype, { var found = false; return statements[stat_index].transform(new TreeTransformer(function(node, descend, in_list) { if (found) return node; + if (node instanceof AST_Scope) return node; if (node !== expr && node.body !== expr) return; + found = true; if (node instanceof AST_VarDef) { - found = true; node.value = null; return node; } - if (in_list) { - found = true; - return MAP.skip; - } - if (!this.parent()) { - found = true; - return null; - } + var parent = this.parent(); + if (!parent) return in_list ? MAP.skip : null; + if (parent instanceof AST_Sequence) return MAP.skip; + var value = expr; + do { + value = get_rvalue(value); + } while (value instanceof AST_Assign); + return value; }, function(node) { if (node instanceof AST_Sequence) switch (node.expressions.length) { case 0: return null; |