Skip to content

Commit

Permalink
fix corner case in varify (#5698)
Browse files Browse the repository at this point in the history
fixes #5697
  • Loading branch information
alexlamsl committed Oct 4, 2022
1 parent 58d997a commit be8ccc3
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/compress.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
162 changes: 162 additions & 0 deletions test/compress/varify.js
Expand Up @@ -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"
}

0 comments on commit be8ccc3

Please sign in to comment.