From be8ccc3ab5c34c4d4861e58bb1634d6dbc4a53f0 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 4 Oct 2022 09:03:24 +0100 Subject: [PATCH] fix corner case in `varify` (#5698) fixes #5697 --- lib/compress.js | 7 +- test/compress/varify.js | 162 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 7e5f7e07e01..71d3654319a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -9347,7 +9347,7 @@ Compressor.prototype.compress = function(node) { return !same_scope(def) || may_overlap(compressor, def); } }, true)) { - self.init = to_var(self.init); + self.init = to_var(self.init, self.resolve()); } } return self; @@ -10161,14 +10161,13 @@ Compressor.prototype.compress = function(node) { } } - function to_var(stat) { + function to_var(stat, scope) { return make_node(AST_Var, stat, { definitions: stat.definitions.map(function(defn) { return make_node(AST_VarDef, defn, { name: defn.name.convert_symbol(AST_SymbolVar, function(name, node) { var def = name.definition(); def.orig[def.orig.indexOf(node)] = name; - var scope = def.scope.resolve(); if (def.scope === scope) return; def.scope = scope; scope.variables.set(def.name, def); @@ -10194,7 +10193,7 @@ Compressor.prototype.compress = function(node) { return !defn.name.match_symbol(function(node) { if (node instanceof AST_SymbolDeclaration) return !can_varify(compressor, node); }, true); - }) ? to_var(self) : self; + }) ? to_var(self, compressor.find_parent(AST_Scope)) : self; } OPT(AST_Const, varify); diff --git a/test/compress/varify.js b/test/compress/varify.js index 19e2ce8a949..a7d983a69ef 100644 --- a/test/compress/varify.js +++ b/test/compress/varify.js @@ -682,3 +682,165 @@ issue_5516: { expect_stdout: "function" node_version: ">=4" } + +issue_5697_1: { + options = { + if_return: true, + inline: true, + reduce_vars: true, + unused: true, + varify: true, + } + input: { + console.log(function() { + f(); + return typeof a; + function f() { + (function() { + for (var k in { foo: 42 }) { + const a = k; + console.log(a); + } + })(); + } + }()); + } + expect: { + console.log(function() { + (function() { + for (var k in { foo: 42 }) { + var a = k; + console.log(a); + } + })(); + return typeof a; + }()); + } + expect_stdout: [ + "foo", + "undefined", + ] +} + +issue_5697_2: { + options = { + if_return: true, + inline: true, + reduce_vars: true, + unused: true, + varify: true, + } + input: { + "use strict"; + console.log(function() { + f(); + return typeof a; + function f() { + (function() { + for (var k in { foo: 42 }) { + let a = k; + console.log(a); + } + })(); + } + }()); + } + expect: { + "use strict"; + console.log(function() { + (function() { + for (var k in { foo: 42 }) { + var a = k; + console.log(a); + } + })(); + return typeof a; + }()); + } + expect_stdout: [ + "foo", + "undefined", + ] + node_version: ">=4" +} + +issue_5697_3: { + options = { + inline: true, + reduce_vars: true, + side_effects: true, + unused: true, + varify: true, + } + input: { + console.log(function() { + f(); + return typeof a; + function f() { + (function() { + for (var k in { foo: 42 }) { + const a = k; + console.log(a); + } + })(); + } + }()); + } + expect: { + console.log(function() { + (function() { + for (var k in { foo: 42 }) { + var a = k; + console.log(a); + } + })(); + return typeof a; + }()); + } + expect_stdout: [ + "foo", + "undefined", + ] +} + +issue_5697_4: { + options = { + inline: true, + reduce_vars: true, + side_effects: true, + unused: true, + varify: true, + } + input: { + "use strict"; + console.log(function() { + f(); + return typeof a; + function f() { + (function() { + for (var k in { foo: 42 }) { + let a = k; + console.log(a); + } + })(); + } + }()); + } + expect: { + "use strict"; + console.log(function() { + (function() { + for (var k in { foo: 42 }) { + var a = k; + console.log(a); + } + })(); + return typeof a; + }()); + } + expect_stdout: [ + "foo", + "undefined", + ] + node_version: ">=4" +}