From f97e107c09cb56e242614c9d7c629a4ae7e4a313 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 13 Nov 2021 14:18:56 +0000 Subject: [PATCH] enhance `reduce_vars` (#5171) --- lib/compress.js | 4 +++- test/compress/drop-unused.js | 30 +++++++++++++++++++++++++++--- test/compress/hoist_vars.js | 29 ++++++++++++++++++++++++++--- test/compress/reduce_vars.js | 18 ++++++++++++++++++ 4 files changed, 74 insertions(+), 7 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 351ec2f9..ab3d9dfe 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -613,12 +613,14 @@ merge(Compressor.prototype, { if (def.single_use == "m") return false; var safe = tw.safe_ids[def.id]; if (safe) { - if (!HOP(tw.safe_ids, def.id)) safe.read = safe.read && safe.read !== tw.safe_ids ? true : tw.safe_ids; + var in_order = HOP(tw.safe_ids, def.id); + if (!in_order) safe.read = safe.read && safe.read !== tw.safe_ids ? true : tw.safe_ids; if (def.fixed == null) { if (is_arguments(def)) return false; if (def.global && def.name == "arguments") return false; tw.loop_ids[def.id] = null; def.fixed = make_node(AST_Undefined, def.orig[0]); + if (in_order) delete def.safe_ids; return true; } return !safe.assign || safe.assign === tw.safe_ids; diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index dc934c9b..eae53cdb 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -3062,7 +3062,7 @@ issue_4184: { expect_stdout: "42" } -issue_4235: { +issue_4235_1: { options = { inline: true, reduce_vars: true, @@ -3081,13 +3081,37 @@ issue_4235: { } expect: { void function() { - var f; - console.log(f); + var f = console.log(f); }(); } expect_stdout: "undefined" } +issue_4235_2: { + options = { + inline: true, + passes: 2, + reduce_vars: true, + side_effects: true, + unused: true, + varify: true, + } + input: { + (function() { + { + const f = 0; + } + (function f() { + var f = console.log(f); + })(); + })(); + } + expect: { + console.log(void 0); + } + expect_stdout: "undefined" +} + issue_4404: { options = { pure_getters: "strict", diff --git a/test/compress/hoist_vars.js b/test/compress/hoist_vars.js index 39a27275..983b47ed 100644 --- a/test/compress/hoist_vars.js +++ b/test/compress/hoist_vars.js @@ -135,7 +135,7 @@ issue_2295: { } } -issue_4487: { +issue_4487_1: { options = { functions: true, hoist_vars: true, @@ -152,14 +152,37 @@ issue_4487: { } expect: { function a() { - var f; - console.log(typeof f); + var f = console.log(typeof f); } a(); } expect_stdout: "undefined" } +issue_4487_2: { + options = { + functions: true, + hoist_vars: true, + keep_fnames: true, + passes: 2, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var a = function f() { + var f = console.log(typeof f); + }; + var b = a(); + } + expect: { + (function a() { + console.log(typeof void 0); + })(); + } + expect_stdout: "undefined" +} + issue_4489: { options = { collapse_vars: true, diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 43e1ff1a..f98f88e6 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -7340,6 +7340,24 @@ local_assignment_modified: { expect_stdout: "42" } +local_declaration: { + options = { + evaluate: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + } + input: { + var a; + a || console.log(a = "PASS"); + } + expect: { + var a; + console.log("PASS"); + } + expect_stdout: "PASS" +} + local_definition_modified: { options = { evaluate: true,