From 6d9424231858e41c86ed3cb5be2eee4899e923a0 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 2 Nov 2021 11:32:26 +0800 Subject: [PATCH] enhance `reduce_vars` (#5163) --- lib/compress.js | 27 ++++++++++++++------------- test/compress/assignments.js | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index d68a045e..edc45b88 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -899,30 +899,22 @@ merge(Compressor.prototype, { case "&&=": case "||=": case "??=": - left.walk(tw); - push(tw); - if (scan) { - right.walk(tw); - walk_assign(); - } else { - mark_assignment_to_arguments(left); - right.walk(tw); - } - pop(tw); - return true; + var lazy = true; default: if (!scan) { mark_assignment_to_arguments(left); - return; + return walk_lazy(); } ld.assignments++; var fixed = ld.fixed; if (is_modified(compressor, tw, node, node, 0)) { ld.fixed = false; - return; + return walk_lazy(); } var safe = safe_to_read(tw, ld); + if (lazy) push(tw); right.walk(tw); + if (lazy) pop(tw); if (safe && !left.in_arg && safe_to_assign(tw, ld)) { push_ref(ld, left); mark(tw, ld); @@ -995,6 +987,15 @@ merge(Compressor.prototype, { } }); } + + function walk_lazy() { + if (!lazy) return; + left.walk(tw); + push(tw); + right.walk(tw); + pop(tw); + return true; + } }); def(AST_Binary, function(tw) { if (!lazy_op[this.operator]) return; diff --git a/test/compress/assignments.js b/test/compress/assignments.js index 84cc5d67..37c67411 100644 --- a/test/compress/assignments.js +++ b/test/compress/assignments.js @@ -550,6 +550,24 @@ logical_side_effects: { node_version: ">=15" } +evaluate_lazy_assignment: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var a = 42; + console.log(a &&= "PASS"); + } + expect: { + console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=15" +} + issue_4815_1: { options = { evaluate: true,