Skip to content

Commit

Permalink
fix corner case in assignments (#3407)
Browse files Browse the repository at this point in the history
fixes #3406
  • Loading branch information
alexlamsl committed May 11, 2019
1 parent e88c439 commit 54cb678
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 55 deletions.
29 changes: 14 additions & 15 deletions lib/compress.js
Expand Up @@ -5395,7 +5395,8 @@ merge(Compressor.prototype, {

OPT(AST_UnaryPrefix, function(self, compressor) {
var e = self.expression;
if (self.operator == "delete"
if (compressor.option("evaluate")
&& self.operator == "delete"
&& !(e instanceof AST_SymbolRef
|| e instanceof AST_PropAccess
|| is_identifier_atom(e))) {
Expand Down Expand Up @@ -6254,23 +6255,20 @@ merge(Compressor.prototype, {
if (compressor.option("dead_code")
&& self.left instanceof AST_SymbolRef
&& (def = self.left.definition()).scope === compressor.find_parent(AST_Lambda)) {
if (self.left.is_immutable()) return strip_assignment();
var level = 0, node, parent = self;
do {
node = parent;
parent = compressor.parent(level++);
if (parent instanceof AST_Exit) {
if (in_try(level, parent)) break;
if (is_reachable(def.scope, [ def ])) break;
if (self.operator == "=") return self.right.optimize(compressor);
def.fixed = false;
return make_node(AST_Binary, self, {
operator: self.operator.slice(0, -1),
left: self.left,
right: self.right
}).optimize(compressor);
return strip_assignment();
}
} while (parent instanceof AST_Binary && parent.right === node
|| parent instanceof AST_Sequence && parent.tail_node() === node);
|| parent instanceof AST_Sequence && parent.tail_node() === node
|| parent instanceof AST_UnaryPrefix);
}
self = self.lift_sequences(compressor);
if (!compressor.option("assignments")) return self;
Expand Down Expand Up @@ -6302,13 +6300,6 @@ merge(Compressor.prototype, {
expression: self.left
});
}
if (!compressor.option("ie8") && self.left instanceof AST_Symbol && self.left.is_immutable()) {
return (self.operator == "=" ? self.right : make_node(AST_Binary, self, {
operator: self.operator.slice(0, -1),
left: self.left,
right: self.right
})).optimize(compressor);
}
return self;

function in_try(level, node) {
Expand All @@ -6325,6 +6316,14 @@ merge(Compressor.prototype, {
}
}
}

function strip_assignment() {
return (self.operator != "=" ? make_node(AST_Binary, self, {
operator: self.operator.slice(0, -1),
left: self.left,
right: self.right
}) : maintain_this_binding(compressor, compressor.parent(), self, self.right)).optimize(compressor);
}
});

OPT(AST_Conditional, function(self, compressor) {
Expand Down
36 changes: 0 additions & 36 deletions test/compress/assignment.js
Expand Up @@ -311,39 +311,3 @@ issue_3375: {
}
expect_stdout: "string"
}

issue_3402: {
options = {
assignments: true,
evaluate: true,
functions: true,
passes: 2,
reduce_vars: true,
side_effects: true,
toplevel: true,
typeofs: true,
unused: true,
}
input: {
var f = function f() {
f = 42;
console.log(typeof f);
};
"function" == typeof f && f();
"function" == typeof f && f();
console.log(typeof f);
}
expect: {
function f() {
console.log(typeof f);
}
f();
f();
console.log(typeof f);
}
expect_stdout: [
"function",
"function",
"function",
]
}
53 changes: 53 additions & 0 deletions test/compress/dead-code.js
Expand Up @@ -960,3 +960,56 @@ unsafe_string_replace: {
}
expect_stdout: "PASS"
}

issue_3402: {
options = {
dead_code: true,
evaluate: true,
functions: true,
passes: 2,
reduce_vars: true,
side_effects: true,
toplevel: true,
typeofs: true,
unused: true,
}
input: {
var f = function f() {
f = 42;
console.log(typeof f);
};
"function" == typeof f && f();
"function" == typeof f && f();
console.log(typeof f);
}
expect: {
function f() {
console.log(typeof f);
}
f();
f();
console.log(typeof f);
}
expect_stdout: [
"function",
"function",
"function",
]
}

issue_3406: {
options = {
dead_code: true,
}
input: {
console.log(function f(a) {
return delete (f = a);
}());
}
expect: {
console.log(function f(a) {
return delete (0, a);
}());
}
expect_stdout: "true"
}
8 changes: 4 additions & 4 deletions test/compress/drop-unused.js
Expand Up @@ -1005,7 +1005,7 @@ issue_1715_4: {
delete_assign_1: {
options = {
booleans: true,
side_effects: true,
evaluate: true,
toplevel: true,
unused: true,
}
Expand All @@ -1024,16 +1024,16 @@ delete_assign_1: {
console.log((1 / 0, !0));
console.log((1 / 0, !0));
console.log((NaN, !0));
console.log((0 / 0, !0));
console.log((NaN, !0));
}
expect_stdout: true
}

delete_assign_2: {
options = {
booleans: true,
evaluate: true,
keep_infinity: true,
side_effects: true,
toplevel: true,
unused: true,
}
Expand All @@ -1052,7 +1052,7 @@ delete_assign_2: {
console.log((Infinity, !0));
console.log((1 / 0, !0));
console.log((NaN, !0));
console.log((0 / 0, !0));
console.log((NaN, !0));
}
expect_stdout: true
}
Expand Down
6 changes: 6 additions & 0 deletions test/compress/sequences.js
Expand Up @@ -490,6 +490,7 @@ issue_1758: {
delete_seq_1: {
options = {
booleans: true,
evaluate: true,
side_effects: true,
}
input: {
Expand All @@ -514,6 +515,7 @@ delete_seq_1: {
delete_seq_2: {
options = {
booleans: true,
evaluate: true,
side_effects: true,
}
input: {
Expand All @@ -538,6 +540,7 @@ delete_seq_2: {
delete_seq_3: {
options = {
booleans: true,
evaluate: true,
keep_infinity: true,
side_effects: true,
}
Expand All @@ -563,6 +566,7 @@ delete_seq_3: {
delete_seq_4: {
options = {
booleans: true,
evaluate: true,
sequences: true,
side_effects: true,
}
Expand Down Expand Up @@ -590,6 +594,7 @@ delete_seq_4: {
delete_seq_5: {
options = {
booleans: true,
evaluate: true,
keep_infinity: true,
sequences: true,
side_effects: true,
Expand Down Expand Up @@ -618,6 +623,7 @@ delete_seq_5: {
delete_seq_6: {
options = {
booleans: true,
evaluate: true,
side_effects: true,
}
input: {
Expand Down

0 comments on commit 54cb678

Please sign in to comment.