From 2cb6454b1f8739e27708a29c5a97610afa2c554e Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 8 Jun 2024 20:15:37 +0300 Subject: [PATCH] fix corner case in `merge_vars` (#5830) fixes #5829 --- lib/compress.js | 4 +++- test/compress/nullish.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 664be40da2b..d0babad62b5 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6719,9 +6719,11 @@ Compressor.prototype.compress = function(node) { segments.consequent = scan_branches(level + 1, condition.left, condition.right).consequent; break; case "||": - case "??": segments.alternative = scan_branches(level + 1, condition.left, null, condition.right).alternative; break; + case "??": + segments.alternative = scan_branches(level + 1, condition.left, condition.right, condition.right).alternative; + break; default: condition.walk(tw); break; diff --git a/test/compress/nullish.js b/test/compress/nullish.js index 2f438d45aea..2ef46bcece3 100644 --- a/test/compress/nullish.js +++ b/test/compress/nullish.js @@ -344,3 +344,43 @@ issue_5266: { ] node_version: ">=14" } + +issue_5829_1: { + options = { + merge_vars: true, + } + input: { + (function f(a) { + var b; + (!a ?? (b = 0)) || console.log(b || "PASS"); + })("FAIL"); + } + expect: { + (function f(a) { + var b; + (!a ?? (b = 0)) || console.log(b || "PASS"); + })("FAIL"); + } + expect_stdout: "PASS" + node_version: ">=14" +} + +issue_5829_2: { + options = { + merge_vars: true, + } + input: { + (function f(a) { + var b; + (a ?? (b = 0)) && console.log(b || "PASS"); + })("FAIL"); + } + expect: { + (function f(a) { + var b; + (a ?? (b = 0)) && console.log(b || "PASS"); + })("FAIL"); + } + expect_stdout: "PASS" + node_version: ">=14" +}