fix corner case in reduce_vars (#5408)

fixes #5407
This commit is contained in:
Alex Lam S.L
2022-04-05 03:09:25 +01:00
committed by GitHub
parent d75a946707
commit ccd77d70db
2 changed files with 47 additions and 8 deletions

View File

@@ -763,6 +763,22 @@ Compressor.prototype.compress = function(node) {
}; };
} }
function make_fixed_default(compressor, node, save) {
var prev_save, prev_seq;
return function() {
var current = save();
var ev;
if (!is_undefined(current, compressor) && (ev = fuzzy_eval(compressor, current, true)) !== undefined) {
return ev instanceof AST_Node ? node : current;
}
if (prev_save !== current) {
prev_save = current;
prev_seq = make_sequence(node, [ current, node.value ]);
}
return prev_seq;
};
}
function scan_declaration(tw, compressor, lhs, fixed, visit) { function scan_declaration(tw, compressor, lhs, fixed, visit) {
var scanner = new TreeWalker(function(node) { var scanner = new TreeWalker(function(node) {
if (node instanceof AST_DefaultValue) { if (node instanceof AST_DefaultValue) {
@@ -771,14 +787,7 @@ Compressor.prototype.compress = function(node) {
node.value.walk(tw); node.value.walk(tw);
pop(tw); pop(tw);
var save = fixed; var save = fixed;
if (save) fixed = make_fixed(save, function(value) { if (save) fixed = make_fixed_default(compressor, node, save);
var ev;
if (is_undefined(value, compressor)
|| (ev = fuzzy_eval(compressor, value, true)) === undefined) {
return make_sequence(node, [ value, node.value ]);
}
return ev instanceof AST_Node ? node : value;
});
node.name.walk(scanner); node.name.walk(scanner);
fixed = save; fixed = save;
return true; return true;

View File

@@ -2209,3 +2209,33 @@ issue_5340_3: {
expect_stdout: "undefined" expect_stdout: "undefined"
node_version: ">=6" node_version: ">=6"
} }
issue_5407: {
options = {
evaluate: true,
reduce_vars: true,
}
input: {
(function(a) {
for (var i = 0; i < 2; i++)
(function(b = 4) {
console.log(b);
a = 2;
})(a);
})();
}
expect: {
(function(a) {
for (var i = 0; i < 2; i++)
(function(b = 4) {
console.log(b);
a = 2;
})(a);
})();
}
expect_stdout: [
"4",
"2",
]
node_version: ">=6"
}