Skip to content

Commit

Permalink
fix corner case in conditionals (#5233)
Browse files Browse the repository at this point in the history
fixes #5232
  • Loading branch information
alexlamsl committed Dec 23, 2021
1 parent 29a1e71 commit bab4164
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 10 deletions.
62 changes: 53 additions & 9 deletions lib/compress.js
Expand Up @@ -8792,17 +8792,58 @@ Compressor.prototype.compress = function(node) {
self.body = self.alternative || make_node(AST_EmptyStatement, self);
self.alternative = tmp;
}
var body = [], var_defs = [], refs = [];
var body_exprs = sequencesize(self.body, body, var_defs, refs);
var alt_exprs = sequencesize(self.alternative, body, var_defs, refs);
var body_defuns = [];
var body_var_defs = [];
var body_refs = [];
var body_exprs = sequencesize(self.body, body_defuns, body_var_defs, body_refs);
var alt_defuns = [];
var alt_var_defs = [];
var alt_refs = [];
var alt_exprs = sequencesize(self.alternative, alt_defuns, alt_var_defs, alt_refs);
if (body_exprs instanceof AST_BlockStatement || alt_exprs instanceof AST_BlockStatement) {
var body = [], var_defs = [];
if (body_exprs) {
[].push.apply(body, body_defuns);
[].push.apply(var_defs, body_var_defs);
if (body_exprs instanceof AST_BlockStatement) {
self.body = body_exprs;
} else if (body_exprs.length == 0) {
self.body = make_node(AST_EmptyStatement, self.body);
} else {
self.body = make_node(AST_SimpleStatement, self.body, {
body: make_sequence(self.body, body_exprs),
});
}
body_refs.forEach(function(ref) {
ref.definition().references.push(ref);
});
}
if (alt_exprs) {
[].push.apply(body, alt_defuns);
[].push.apply(var_defs, alt_var_defs);
if (alt_exprs instanceof AST_BlockStatement) {
self.alternative = alt_exprs;
} else if (alt_exprs.length == 0) {
self.alternative = null;
} else {
self.alternative = make_node(AST_SimpleStatement, self.alternative, {
body: make_sequence(self.alternative, alt_exprs),
});
}
alt_refs.forEach(function(ref) {
ref.definition().references.push(ref);
});
}
if (var_defs.length > 0) body.push(make_node(AST_Var, self, { definitions: var_defs }));
body.push(self);
if (body_exprs instanceof AST_BlockStatement) self.body = body_exprs;
if (alt_exprs instanceof AST_BlockStatement) self.alternative = alt_exprs;
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
if (body.length > 0) {
body.push(self);
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
}
} else if (body_exprs && alt_exprs) {
if (var_defs.length > 0) body.push(make_node(AST_Var, self, { definitions: var_defs }));
var body = body_defuns.concat(alt_defuns);
if (body_var_defs.length > 0 || alt_var_defs.length > 0) body.push(make_node(AST_Var, self, {
definitions: body_var_defs.concat(alt_var_defs),
}));
if (body_exprs.length == 0) {
body.push(make_node(AST_SimpleStatement, self.condition, {
body: alt_exprs.length > 0 ? make_node(AST_Binary, self, {
Expand Down Expand Up @@ -8835,7 +8876,10 @@ Compressor.prototype.compress = function(node) {
}),
}).optimize(compressor));
}
refs.forEach(function(ref) {
body_refs.forEach(function(ref) {
ref.definition().references.push(ref);
});
alt_refs.forEach(function(ref) {
ref.definition().references.push(ref);
});
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
Expand Down
83 changes: 83 additions & 0 deletions test/compress/conditionals.js
Expand Up @@ -1924,3 +1924,86 @@ object_super: {
expect_stdout: "PASS"
node_version: ">=4"
}

issue_5232_1: {
options = {
conditionals: true,
}
input: {
(function() {
if (Math) {
function f() {}
for (var a in [ 42 ])
console.log(typeof f);
} else {
var b = null;
return true;
}
})();
}
expect: {
(function() {
var b;
if (!Math)
return b = null, true;
function f() {}
for (var a in [ 42 ]) console.log(typeof f);
})();
}
expect_stdout: "function"
}

issue_5232_2: {
options = {
conditionals: true,
}
input: {
console.log(function() {
if (!Math);
else {
var b = null;
return "PASS";
}
}());
}
expect: {
console.log(function() {
var b;
if (Math)
return b = null, "PASS";
}());
}
expect_stdout: "PASS"
}

issue_5232_3: {
options = {
conditionals: true,
}
input: {
console.log(function() {
return function() {
if (console)
console.log("PASS");
else {
var a = null;
return "FAIL";
}
};
}()());
}
expect: {
console.log(function() {
return function() {
var a;
if (!console)
return a = null, "FAIL";
console.log("PASS");
};
}()());
}
expect_stdout: [
"PASS",
"undefined",
]
}
2 changes: 1 addition & 1 deletion test/reduce.js
Expand Up @@ -548,7 +548,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
}));
var before_iterations = testcase;
for (var c = 0; c < max_iterations; ++c) {
if (verbose && pass == 1 && c % 25 == 0) {
if (verbose && c % (pass == 1 ? 25 : 100) == 0) {
log("// reduce test pass " + pass + ", iteration " + c + ": " + testcase.length + " bytes");
}
var CHANGED = false;
Expand Down

0 comments on commit bab4164

Please sign in to comment.