aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-03-04 09:13:54 +0000
committerGitHub <noreply@github.com>2021-03-04 17:13:54 +0800
commitcb50a2d192ef8be1aac7577d6ec9c06f27b71232 (patch)
tree88f4fbed080062cd6b9bbb184af4b6490fcacb0c
parent20be5209c08f95802198c7a5d3acd0767fcd4e70 (diff)
downloadtracifyjs-cb50a2d192ef8be1aac7577d6ec9c06f27b71232.tar.gz
tracifyjs-cb50a2d192ef8be1aac7577d6ec9c06f27b71232.zip
fix corner case in `collapse_vars` (#4733)
fixes #4732
-rw-r--r--lib/compress.js3
-rw-r--r--test/compress/collapse_vars.js48
2 files changed, 50 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js
index ed048593..6a59a9d5 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2675,9 +2675,10 @@ merge(Compressor.prototype, {
if (hit_index <= end) return handle_custom_scan_order(node, tt);
hit = true;
if (node instanceof AST_VarDef) {
- node.value = null;
declare_only[node.name.name] = (declare_only[node.name.name] || 0) + 1;
if (value_def) value_def.replaced++;
+ node = node.clone();
+ node.value = null;
return node;
}
return in_list ? List.skip : null;
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index c0a545c5..6e82e8b0 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -8756,3 +8756,51 @@ issue_4586_2: {
}
expect_stdout: "PASS"
}
+
+issue_4732_1: {
+ options = {
+ booleans: true,
+ collapse_vars: true,
+ evaluate: true,
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ var a = 0;
+ (function(b) {
+ var b = a++;
+ var c = b ? b && console.log("PASS") : 0;
+ })(a++);
+ }
+ expect: {
+ var a = 0;
+ (function(b) {
+ (b = a++) && (b && console.log("PASS"));
+ })(a++);
+ }
+ expect_stdout: "PASS"
+}
+
+issue_4732_2: {
+ options = {
+ collapse_vars: true,
+ conditionals: true,
+ evaluate: true,
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ var a = 0;
+ (function(b) {
+ var b = a++;
+ var c = b ? b && console.log("PASS") : 0;
+ })(a++);
+ }
+ expect: {
+ var a = 0;
+ (function(b) {
+ (b = a++) && b && console.log("PASS");
+ })(a++);
+ }
+ expect_stdout: "PASS"
+}