Skip to content

Commit

Permalink
fix corner case in conditionals (#5335)
Browse files Browse the repository at this point in the history
fixes #5334
  • Loading branch information
alexlamsl committed Feb 3, 2022
1 parent 8ceb4b0 commit fa30960
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
28 changes: 13 additions & 15 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,12 @@ Compressor.prototype.compress = function(node) {
AST_SymbolDeclaration.DEFMETHOD("convert_symbol", convert_symbol);
AST_SymbolRef.DEFMETHOD("convert_symbol", convert_symbol);

function process_to_assign(ref) {
var def = ref.definition();
def.assignments++;
def.references.push(ref);
}

function mark_destructured(process, tw) {
var marker = new TreeWalker(function(node) {
if (node instanceof AST_DefaultValue) {
Expand Down Expand Up @@ -3539,9 +3545,7 @@ Compressor.prototype.compress = function(node) {
if (!var_def.value) return;
exprs.push(make_node(AST_Assign, var_def, {
operator: "=",
left: var_def.name.convert_symbol(AST_SymbolRef, function(ref) {
ref.definition().references.push(ref);
}),
left: var_def.name.convert_symbol(AST_SymbolRef, process_to_assign),
right: var_def.value,
}));
});
Expand Down Expand Up @@ -9084,9 +9088,7 @@ Compressor.prototype.compress = function(node) {
body: make_sequence(self.body, body_exprs),
});
}
body_refs.forEach(function(ref) {
ref.definition().references.push(ref);
});
body_refs.forEach(process_to_assign);
}
if (alt_exprs) {
[].push.apply(body, alt_defuns);
Expand All @@ -9100,9 +9102,7 @@ Compressor.prototype.compress = function(node) {
body: make_sequence(self.alternative, alt_exprs),
});
}
alt_refs.forEach(function(ref) {
ref.definition().references.push(ref);
});
alt_refs.forEach(process_to_assign);
}
if (var_defs.length > 0) body.push(make_node(AST_Var, self, { definitions: var_defs }));
if (body.length > 0) {
Expand Down Expand Up @@ -9146,12 +9146,8 @@ Compressor.prototype.compress = function(node) {
}),
}).optimize(compressor));
}
body_refs.forEach(function(ref) {
ref.definition().references.push(ref);
});
alt_refs.forEach(function(ref) {
ref.definition().references.push(ref);
});
body_refs.forEach(process_to_assign);
alt_refs.forEach(process_to_assign);
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
}
if (is_empty(self.body)) self = make_node(AST_If, self, {
Expand Down Expand Up @@ -10450,6 +10446,7 @@ Compressor.prototype.compress = function(node) {
scope.enclosed.push(def);
if (!value) return;
var sym = make_node(AST_SymbolRef, name, name);
def.assignments++;
def.references.push(sym);
expressions.push(make_node(AST_Assign, self, {
operator: "=",
Expand Down Expand Up @@ -10523,6 +10520,7 @@ Compressor.prototype.compress = function(node) {

function process(ref, name) {
var def = name.definition();
def.assignments++;
def.references.push(ref);
var symbol = make_node(AST_SymbolVar, name, name);
def.orig.push(symbol);
Expand Down
55 changes: 55 additions & 0 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2007,3 +2007,58 @@ issue_5232_3: {
"undefined",
]
}

issue_5334_1: {
options = {
conditionals: true,
hoist_props: true,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function f() {
if (console.log("PASS"))
var o = true, o = {
p: o += console.log("FAIL"),
};
}
f();
}
expect: {
(function() {
var o;
console.log("PASS") && (o = true, o = {
p: o += console.log("FAIL"),
});
})();
}
expect_stdout: "PASS"
}

issue_5334_2: {
options = {
conditionals: true,
hoist_props: true,
inline: true,
passes: 3,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function f() {
if (console.log("PASS"))
var o = true, o = {
p: o += console.log("FAIL"),
};
}
f();
}
expect: {
console.log("PASS") && console.log("FAIL");
}
expect_stdout: "PASS"
}

0 comments on commit fa30960

Please sign in to comment.