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

View File

@@ -327,6 +327,13 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
self.uses_eval = true; self.uses_eval = true;
} }
} }
if (sym.init instanceof AST_LambdaDefinition && sym.scope !== sym.init.name.scope) {
var scope = node.scope;
do {
if (scope === sym.init.name.scope) break;
} while (scope = scope.parent_scope);
if (!scope) sym.init = undefined;
}
node.thedef = sym; node.thedef = sym;
node.reference(options); node.reference(options);
return true; return true;

View File

@@ -1454,3 +1454,47 @@ issue_4689: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=4" node_version: ">=4"
} }
issue_4691: {
options = {
if_return: true,
toplevel: true,
}
input: {
function A() {}
A.prototype.f = function() {
if (!this)
return;
const a = "PA";
function g(b) {
h(a + b);
}
[ "SS" ].forEach(function(c) {
g(c);
});
};
function h(d) {
console.log(d);
}
new A().f();
}
expect: {
function A() {}
A.prototype.f = function() {
if (this) {
const a = "PA";
[ "SS" ].forEach(function(c) {
g(c);
});
function g(b) {
h(a + b);
}
}
};
function h(d) {
console.log(d);
}
new A().f();
}
expect_stdout: "PASS"
}

View File

@@ -53,8 +53,10 @@ dead_code_2_should_warn: {
g(); g();
x = 10; x = 10;
throw new Error("foo"); throw new Error("foo");
var x; {
function g(){}; var x;
function g(){};
}
} }
f(); f();
} }
@@ -62,7 +64,6 @@ dead_code_2_should_warn: {
expect_warnings: [ expect_warnings: [
"WARN: Dropping unreachable code [test/compress/dead-code.js:8,12]", "WARN: Dropping unreachable code [test/compress/dead-code.js:8,12]",
] ]
node_version: "<=4"
} }
dead_code_constant_boolean_should_warn_more: { dead_code_constant_boolean_should_warn_more: {
@@ -88,8 +89,10 @@ dead_code_constant_boolean_should_warn_more: {
bar(); bar();
} }
expect: { expect: {
var foo; {
function bar() {} var foo;
function bar() {}
}
// nothing for the while // nothing for the while
// as for the for, it should keep: // as for the for, it should keep:
var x = 10, y; var x = 10, y;

View File

@@ -5018,9 +5018,12 @@ catch_no_argname: {
try { try {
throw a; throw a;
} catch { } catch {
console.log(a, a, a); function g() {
return a;
}
console.log(a, a, g());
} }
console.log(a, a, a); console.log(a, a, g());
} }
expect_stdout: [ expect_stdout: [
"PASS PASS PASS", "PASS PASS PASS",
@@ -5558,3 +5561,140 @@ issue_4659_3: {
} }
expect_stdout: "1" expect_stdout: "1"
} }
block_scope_1: {
input: {
console.log(typeof f);
function f() {}
}
expect: {
console.log(typeof f);
function f() {}
}
expect_stdout: "function"
}
block_scope_1_compress: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
typeofs: true,
unused: true,
}
input: {
console.log(typeof f);
function f() {}
}
expect: {
console.log("function");
}
expect_stdout: "function"
}
block_scope_2: {
input: {
{
console.log(typeof f);
}
function f() {}
}
expect: {
console.log(typeof f);
function f() {}
}
expect_stdout: "function"
}
block_scope_2_compress: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
typeofs: true,
unused: true,
}
input: {
{
console.log(typeof f);
}
function f() {}
}
expect: {
console.log("function");
}
expect_stdout: "function"
}
block_scope_3: {
input: {
console.log(typeof f);
{
function f() {}
}
}
expect: {
console.log(typeof f);
{
function f() {}
}
}
expect_stdout: true
}
block_scope_3_compress: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
typeofs: true,
unused: true,
}
input: {
console.log(typeof f);
{
function f() {}
}
}
expect: {
console.log(typeof f);
{
function f() {}
}
}
expect_stdout: true
}
block_scope_4: {
input: {
{
console.log(typeof f);
function f() {}
}
}
expect: {
console.log(typeof f);
function f() {}
}
expect_stdout: "function"
}
block_scope_4_compress: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
typeofs: true,
unused: true,
}
input: {
{
console.log(typeof f);
function f() {}
}
}
expect: {
console.log("function");
}
expect_stdout: "function"
}

View File

@@ -1357,3 +1357,50 @@ issue_4689: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=4" node_version: ">=4"
} }
issue_4691: {
options = {
if_return: true,
toplevel: true,
}
input: {
"use strict";
function A() {}
A.prototype.f = function() {
if (!this)
return;
let a = "PA";
function g(b) {
h(a + b);
}
[ "SS" ].forEach(function(c) {
g(c);
});
};
function h(d) {
console.log(d);
}
new A().f();
}
expect: {
"use strict";
function A() {}
A.prototype.f = function() {
if (this) {
let a = "PA";
[ "SS" ].forEach(function(c) {
g(c);
});
function g(b) {
h(a + b);
}
}
};
function h(d) {
console.log(d);
}
new A().f();
}
expect_stdout: "PASS"
node_version: ">=4"
}