enhance collapse_vars (#5312)

This commit is contained in:
Alex Lam S.L
2022-01-25 20:18:58 +00:00
committed by GitHub
parent 6de708af37
commit 5c863b74d7
4 changed files with 88 additions and 23 deletions

View File

@@ -2058,6 +2058,11 @@ Compressor.prototype.compress = function(node) {
assign.right = rvalue;
return assign;
}
// Stop signals related to AST_SymbolRef
if (should_stop_ref(node, parent)) {
abort = true;
return node;
}
// These node types have child nodes that execute sequentially,
// but are otherwise not safe to scan into or beyond them.
if (is_last_node(node, parent) || may_throw(node)) {
@@ -2184,7 +2189,8 @@ Compressor.prototype.compress = function(node) {
var stop_if_hit = null;
var lhs = get_lhs(candidate);
var side_effects = lhs && lhs.has_side_effects(compressor);
var scan_lhs = lhs && !side_effects && !is_lhs_read_only(lhs, compressor);
var scan_lhs = lhs && (!side_effects || lhs instanceof AST_SymbolRef)
&& !is_lhs_read_only(lhs, compressor);
var scan_rhs = foldable(candidate);
if (!scan_lhs && !scan_rhs) continue;
var compound = candidate instanceof AST_Assign && candidate.operator.slice(0, -1);
@@ -2318,7 +2324,13 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_DestructuredKeyVal) return node.key instanceof AST_Node;
if (node instanceof AST_DWLoop) return true;
if (node instanceof AST_LoopControl) return true;
if (node instanceof AST_SymbolRef) {
if (node instanceof AST_Try) return true;
if (node instanceof AST_With) return true;
return false;
}
function should_stop_ref(node, parent) {
if (!(node instanceof AST_SymbolRef)) return false;
if (node.is_declared(compressor)) {
if (node.fixed_value()) return false;
if (can_drop_symbol(node)) {
@@ -2332,10 +2344,6 @@ Compressor.prototype.compress = function(node) {
scan_rhs = false;
return false;
}
if (node instanceof AST_Try) return true;
if (node instanceof AST_With) return true;
return false;
}
function in_conditional(node, parent) {
if (parent instanceof AST_Assign) return parent.left !== node && lazy_op[parent.operator.slice(0, -1)];

View File

@@ -673,8 +673,7 @@ issue_4827_1: {
c &&= b = a, console.log(b);
}
expect: {
A = "FAIL";
var a = A, b = "PASS", c;
var a = A = "FAIL", b = "PASS", c;
c &&= b = a, console.log(b);
}
expect_stdout: "PASS"

View File

@@ -2579,7 +2579,7 @@ side_effects_property: {
expect_stdout: true
}
undeclared: {
undeclared_1: {
options = {
collapse_vars: true,
unused: true,
@@ -2594,8 +2594,68 @@ undeclared: {
}
expect: {
function f(x, y) {
return (b = y) + x;
}
}
}
undeclared_2: {
options = {
collapse_vars: true,
unused: true,
}
input: {
function f(x, y) {
var a;
a = x;
b = y;
return b + x;
return a + b;
}
}
expect: {
function f(x, y) {
return x + (b = y);
}
}
}
undeclared_3: {
options = {
collapse_vars: true,
unused: true,
}
input: {
function f(x, y) {
var a;
a = x;
b = y;
return b + a();
}
}
expect: {
function f(x, y) {
return (b = y) + x();
}
}
}
undeclared_4: {
options = {
collapse_vars: true,
unused: true,
}
input: {
function f(x, y) {
var a;
a = x;
b = y;
return a() + b;
}
}
expect: {
function f(x, y) {
b = y;
return x() + b;
}
}
}
@@ -2987,9 +3047,8 @@ compound_assignment_4: {
console.log(a);
}
expect: {
A = "PASS";
var a = "";
(a += (a = "FAIL", A)).p;
(a += (a = "FAIL", A = "PASS")).p;
console.log(a);
}
expect_stdout: "PASS"

View File

@@ -250,8 +250,7 @@ issue_4517: {
expect: {
console.log(function() {
var a = 2;
A = a;
return A + typeof !1;
return (A = a) + typeof !1;
}());
}
expect_stdout: "2boolean"