fix corner cases in pure_getters & reduce_vars (#5859)

fixes #5856
fixes #5857
fixes #5858
This commit is contained in:
Alex Lam S.L
2024-06-20 17:43:55 +03:00
committed by GitHub
parent 8195a664fd
commit 95d3ede664
4 changed files with 82 additions and 17 deletions

View File

@@ -1261,8 +1261,9 @@ Compressor.prototype.compress = function(node) {
}); });
def(AST_Dot, function(tw, descend) { def(AST_Dot, function(tw, descend) {
descend(); descend();
var expr = this.expression; var node = this;
if (expr instanceof AST_SymbolRef) access(tw, expr.definition()); var expr = node.expression;
if (!node.optional && expr instanceof AST_SymbolRef) access(tw, expr.definition());
return true; return true;
}); });
def(AST_For, function(tw, descend, compressor) { def(AST_For, function(tw, descend, compressor) {
@@ -1362,15 +1363,18 @@ Compressor.prototype.compress = function(node) {
pop_scope(tw, fn); pop_scope(tw, fn);
return true; return true;
}); });
def(AST_Sub, function(tw) { def(AST_Sub, function(tw, descend) {
var node = this; var node = this;
if (!node.optional) return;
var expr = node.expression; var expr = node.expression;
if (node.optional) {
expr.walk(tw); expr.walk(tw);
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
push(tw, true); push(tw, true);
node.property.walk(tw); node.property.walk(tw);
pop(tw); pop(tw);
} else {
descend();
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
}
return true; return true;
}); });
def(AST_Switch, function(tw, descend, compressor) { def(AST_Switch, function(tw, descend, compressor) {
@@ -8020,8 +8024,14 @@ Compressor.prototype.compress = function(node) {
prop.walk(tw); prop.walk(tw);
}); });
if (node instanceof AST_Assign) { if (node instanceof AST_Assign) {
var right = get_rhs(node), shared = false; var fixed = sym.fixed_value();
if (init && node.write_only === true && !right.has_side_effects(compressor)) { var right = get_rhs(node);
var safe = fixed && fixed.is_constant();
var shared = false;
if (init
&& node.write_only === true
&& (safe || node.left === sym || right.equals(sym))
&& !right.has_side_effects(compressor)) {
initializations.add(node_def.id, right); initializations.add(node_def.id, right);
} else { } else {
right.walk(tw); right.walk(tw);
@@ -8031,13 +8041,10 @@ Compressor.prototype.compress = function(node) {
if (!node.write_only || shared) { if (!node.write_only || shared) {
verify_safe_usage(node_def, sym, value_modified[node_def.id]); verify_safe_usage(node_def, sym, value_modified[node_def.id]);
} }
} else { } else if (!safe) {
var fixed = sym.fixed_value();
if (!fixed || !fixed.is_constant()) {
verify_safe_usage(node_def, value_read[node_def.id], true); verify_safe_usage(node_def, value_read[node_def.id], true);
} }
} }
}
if (track_assigns(node_def, sym) && is_lhs(sym, node) !== sym) add_assigns(node_def, sym); if (track_assigns(node_def, sym) && is_lhs(sym, node) !== sym) add_assigns(node_def, sym);
unmark_lambda(node_def); unmark_lambda(node_def);
return true; return true;

View File

@@ -26,7 +26,7 @@ record_update: {
currying: { currying: {
options = { options = {
inline: true, inline: true,
passes: 2, passes: 3,
pure_getters: "strict", pure_getters: "strict",
reduce_funcs: true, reduce_funcs: true,
reduce_vars: true, reduce_vars: true,

View File

@@ -617,3 +617,33 @@ issue_5292_sub_pure_getters_strict: {
] ]
node_version: ">=14" node_version: ">=14"
} }
issue_5856: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
try {
var a;
a?.p;
a.q;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
try {
var a;
a?.p;
a.q;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
node_version: ">=14"
}

View File

@@ -1687,3 +1687,31 @@ issue_4939: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
issue_5856: {
options = {
pure_getters: true,
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
var a = [ "FAIL", "PASS" ];
(function(b) {
var c = b[0];
b[0] = b[1];
b[1] = c;
})(a);
console.log(a[0]);
}
expect: {
var a = [ "FAIL", "PASS" ];
(function(b) {
var c = b[0];
b[0] = b[1];
b[1] = c;
})(a);
console.log(a[0]);
}
expect_stdout: "PASS"
}