fix corner case in conditionals (#5233)

fixes #5232
This commit is contained in:
Alex Lam S.L
2021-12-23 14:25:02 +00:00
committed by GitHub
parent 29a1e71705
commit bab416465f
3 changed files with 137 additions and 10 deletions

View File

@@ -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, {
@@ -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);