fix corner cases with block-scoped functions (#4695)

This commit is contained in:
Alex Lam S.L
2021-02-26 20:16:14 +00:00
committed by GitHub
parent ea52339502
commit ac26993b5a
6 changed files with 271 additions and 20 deletions

View File

@@ -1509,11 +1509,22 @@ merge(Compressor.prototype, {
return stat instanceof AST_Const || stat instanceof AST_DefClass || stat instanceof AST_Let;
}
function safe_to_trim(stat) {
if (stat instanceof AST_LambdaDefinition) {
var def = stat.name.definition();
return def.scope === stat.name.scope || all(def.references, function(ref) {
var scope = ref.scope;
do {
if (scope === stat.name.scope) return true;
} while (scope = scope.parent_scope);
});
}
return !is_lexical_definition(stat);
}
function as_statement_array(thing) {
if (thing === null) return [];
if (thing instanceof AST_BlockStatement) return all(thing.body, function(stat) {
return !is_lexical_definition(stat);
}) ? thing.body : [ thing ];
if (thing instanceof AST_BlockStatement) return all(thing.body, safe_to_trim) ? thing.body : [ thing ];
if (thing instanceof AST_EmptyStatement) return [];
if (is_statement(thing)) return [ thing ];
throw new Error("Can't convert thing to statement array");
@@ -2732,9 +2743,7 @@ merge(Compressor.prototype, {
for (var i = 0; i < statements.length;) {
var stat = statements[i];
if (stat instanceof AST_BlockStatement) {
if (all(stat.body, function(stat) {
return !is_lexical_definition(stat);
})) {
if (all(stat.body, safe_to_trim)) {
CHANGED = true;
eliminate_spurious_blocks(stat.body);
[].splice.apply(statements, [i, 1].concat(stat.body));
@@ -2972,15 +2981,16 @@ merge(Compressor.prototype, {
}
function extract_functions() {
var tail = statements.slice(i + 1);
statements.length = i + 1;
return tail.filter(function(stat) {
if (stat instanceof AST_Defun) {
statements.push(stat);
var defuns = [];
var tail = statements.splice(i + 1).filter(function(stat) {
if (stat instanceof AST_LambdaDefinition) {
defuns.push(stat);
return false;
}
return true;
});
[].push.apply(all(tail, safe_to_trim) ? statements : tail, defuns);
return tail;
}
function as_statement_array_with_return(node, ab) {
@@ -3439,7 +3449,7 @@ merge(Compressor.prototype, {
function push(node) {
if (block) {
block.push(node);
if (is_lexical_definition(node)) block.required = true;
if (!safe_to_trim(node)) block.required = true;
} else {
target.push(node);
}
@@ -5118,7 +5128,7 @@ merge(Compressor.prototype, {
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
case 1:
var stat = node.body[0];
if (is_lexical_definition(stat)) return node;
if (!safe_to_trim(stat)) return node;
if (parent instanceof AST_IterationStatement && stat instanceof AST_LambdaDefinition) return node;
return stat;
}