aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-12-01 02:31:04 +0800
committerGitHub <noreply@github.com>2019-12-01 02:31:04 +0800
commit0593892d6efeee15b54bc91bdd0ef1224a466541 (patch)
treeafbe83297dea0911c52524b6da93c2f767bd587c /lib/compress.js
parentb866a23671713cb1f39c68084dd6376e000e72e4 (diff)
downloadtracifyjs-0593892d6efeee15b54bc91bdd0ef1224a466541.tar.gz
tracifyjs-0593892d6efeee15b54bc91bdd0ef1224a466541.zip
enhance `collapse_vars` (#3616)
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js28
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 34700d4a..6a76fc32 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1147,7 +1147,7 @@ merge(Compressor.prototype, {
&& (scan_lhs && lhs.equivalent_to(node)
|| scan_rhs && (hit_rhs = scan_rhs(node, this)))) {
if (stop_if_hit && (hit_rhs || !lhs_local || !replace_all)) {
- abort = true;
+ if (!hit_rhs || !value_def) abort = true;
return node;
}
if (is_lhs(node, parent)) {
@@ -1541,14 +1541,27 @@ merge(Compressor.prototype, {
function find_stop(node, level) {
var parent = scanner.parent(level);
if (parent instanceof AST_Array) return value_def ? find_stop(parent, level + 1) : node;
- if (parent instanceof AST_Assign) return node;
+ if (parent instanceof AST_Assign) {
+ if (!value_def) return node;
+ if (lhs.equivalent_to(parent.left)) return node;
+ if (get_rvalue(candidate).equivalent_to(parent.left)) return node;
+ return find_stop(parent, level + 1);
+ }
if (parent instanceof AST_Binary) {
- if (!value_def || parent.left !== node) return node;
+ if (!value_def) return node;
+ if (lazy_op[parent.operator] && parent.left !== node) {
+ var grandparent = scanner.parent(level + 1);
+ if (!(grandparent instanceof AST_Binary)) return node;
+ if (grandparent.operator != parent.operator) return node;
+ }
return find_stop(parent, level + 1);
}
- if (parent instanceof AST_Call) return node;
+ if (parent instanceof AST_Call) return value_def ? parent : node;
if (parent instanceof AST_Case) return node;
- if (parent instanceof AST_Conditional) return node;
+ if (parent instanceof AST_Conditional) {
+ if (!value_def || parent.condition !== node) return node;
+ return find_stop(parent, level + 1);
+ }
if (parent instanceof AST_Definitions) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Exit) return node;
if (parent instanceof AST_If) return node;
@@ -1562,7 +1575,10 @@ merge(Compressor.prototype, {
}
if (parent instanceof AST_SimpleStatement) return find_stop_unused(parent, level + 1);
if (parent instanceof AST_Switch) return node;
- if (parent instanceof AST_Unary) return node;
+ if (parent instanceof AST_Unary) {
+ if (parent.operator == "delete") return node;
+ return value_def ? find_stop(parent, level + 1) : node;
+ }
if (parent instanceof AST_VarDef) return node;
return null;
}