From 7edd10e5e508df42da8586afc994537598c83f25 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 10 Oct 2022 14:45:57 +0100 Subject: [PATCH] fix corner case in `unused` (#5708) fixes #5707 --- lib/compress.js | 7 +++++-- test/compress/side_effects.js | 4 +++- test/compress/yields.js | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 58f08322..90355858 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -7251,16 +7251,19 @@ Compressor.prototype.compress = function(node) { node.name = null; } if (node instanceof AST_Lambda) { - descend_scope(); if (drop_funcs && node !== self && node instanceof AST_LambdaDefinition) { var def = node.name.definition(); if (!(def.id in in_use_ids)) { log(node.name, "Dropping unused function {name}"); def.eliminated++; - if (parent instanceof AST_ExportDefault) return to_func_expr(node, true); + if (parent instanceof AST_ExportDefault) { + descend_scope(); + return to_func_expr(node, true); + } return in_list ? List.skip : make_node(AST_EmptyStatement, node); } } + descend_scope(); if (node instanceof AST_LambdaExpression && node.name && drop_fn_name(node.name.definition())) { node.name = null; } diff --git a/test/compress/side_effects.js b/test/compress/side_effects.js index 3c09448d..090a6566 100644 --- a/test/compress/side_effects.js +++ b/test/compress/side_effects.js @@ -584,7 +584,9 @@ issue_4668: { } expect: { console.log(function f() { - (function g() {})(); + (function g() { + 0; + })(); }()); } expect_stdout: "undefined" diff --git a/test/compress/yields.js b/test/compress/yields.js index 8478d541..c49f7f39 100644 --- a/test/compress/yields.js +++ b/test/compress/yields.js @@ -2018,3 +2018,24 @@ issue_5684: { expect_stdout: "PASS" node_version: ">=10" } + +issue_5707: { + options = { + hoist_props: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + yields: true, + } + input: { + var a, b; + function* f(c = (b = 42, console.log("PASS"))) {} + b = f(); + } + expect: { + console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=6" +}