improve handling of declaration statements (#4980)

This commit is contained in:
Alex Lam S.L
2021-05-30 09:32:48 +01:00
committed by GitHub
parent 55a230daa8
commit 06e3dbc089
3 changed files with 127 additions and 8 deletions

View File

@@ -325,9 +325,12 @@ merge(Compressor.prototype, {
} else if (insert === "awaits" && node instanceof AST_Try) {
if (node.bfinally) return node;
}
var index = node.body.length - 1;
if (index >= 0) {
node.body[index] = node.body[index].transform(tt);
for (var index = node.body.length; --index >= 0;) {
var stat = node.body[index];
if (!is_declaration(stat, true)) {
node.body[index] = stat.transform(tt);
break;
}
}
} else if (node instanceof AST_If) {
node.body = node.body.transform(tt);
@@ -1695,8 +1698,16 @@ merge(Compressor.prototype, {
});
}
function is_declaration(stat) {
return stat instanceof AST_Defun || stat instanceof AST_Var && declarations_only(stat);
function is_declaration(stat, lexical) {
if (stat instanceof AST_DefClass) return lexical && !stat.extends && all(stat.properties, function(prop) {
if (prop.key instanceof AST_Node) return false;
if (prop instanceof AST_ClassField && prop.static && prop.value) return false;
return true;
});
if (stat instanceof AST_Definitions) return (lexical || stat instanceof AST_Var) && declarations_only(stat);
if (stat instanceof AST_ExportDeclaration) return is_declaration(stat.body, lexical);
if (stat instanceof AST_ExportDefault) return is_declaration(stat.body, lexical);
return stat instanceof AST_LambdaDefinition;
}
function tighten_body(statements, compressor) {
@@ -3157,7 +3168,7 @@ merge(Compressor.prototype, {
var index = body.lastIndexOf(stat);
if (index < 0) return false;
while (++index < body.length) {
if (!is_declaration(body[index])) return false;
if (!is_declaration(body[index], true)) return false;
}
return true;
}
@@ -7970,7 +7981,7 @@ merge(Compressor.prototype, {
self.condition,
]);
body.splice(i, 1);
} else if (!is_declaration(stat)) {
} else if (!is_declaration(stat, true)) {
break;
}
}