Skip to content

Commit

Permalink
enhance conditionals (#5371)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Mar 2, 2022
1 parent f8edf05 commit fdbbef2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
30 changes: 10 additions & 20 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9157,14 +9157,16 @@ Compressor.prototype.compress = function(node) {
var cond = fuzzy_eval(compressor, self.condition);
if (!cond) {
AST_Node.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
var body = [ make_node(AST_SimpleStatement, self.condition, { body: self.condition }) ];
var body = [
make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
];
extract_declarations_from_unreachable_code(compressor, self.body, body);
if (self.alternative) body.push(self.alternative);
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
} else if (!(cond instanceof AST_Node)) {
AST_Node.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
var body = [
make_node(AST_SimpleStatement, self.condition, { body: self.condition }),
make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
self.body,
];
if (self.alternative) extract_declarations_from_unreachable_code(compressor, self.alternative, body);
Expand Down Expand Up @@ -9493,20 +9495,12 @@ Compressor.prototype.compress = function(node) {
left: self.expression,
right: exp,
}),
body: make_node(AST_BlockStatement, self, {
body: statements,
}),
body: make_node(AST_BlockStatement, self, { body: statements }),
alternative: null,
}).optimize(compressor);
if (exp) statements.unshift(make_node(AST_SimpleStatement, exp, {
body: exp,
}));
statements.unshift(make_node(AST_SimpleStatement, self.expression, {
body:self.expression,
}));
return make_node(AST_BlockStatement, self, {
body: statements,
}).optimize(compressor);
if (exp) statements.unshift(make_node(AST_SimpleStatement, exp, { body: exp }));
statements.unshift(make_node(AST_SimpleStatement, self.expression, { body: self.expression }));
return make_node(AST_BlockStatement, self, { body: statements }).optimize(compressor);
case 2:
if (!member(default_branch, body) || !no_break(body[1])) break;
var statements = body[0].body.slice();
Expand All @@ -9524,14 +9518,10 @@ Compressor.prototype.compress = function(node) {
left: self.expression,
right: body[0].expression,
}),
body: make_node(AST_BlockStatement, body[0], {
body: statements,
}),
body: make_node(AST_BlockStatement, body[0], { body: statements }),
alternative: exclusive && alternative || null,
});
if (!exclusive && alternative) node = make_node(AST_BlockStatement, self, {
body: [ node, alternative ],
});
if (!exclusive && alternative) node = make_node(AST_BlockStatement, self, { body: [ node, alternative ] });
return node.optimize(compressor);
}
return self;
Expand Down
4 changes: 2 additions & 2 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ ifs_3_should_warn: {
"WARN: Boolean && always false [test/compress/conditionals.js:3,12]",
"WARN: Condition left of && always false [test/compress/conditionals.js:3,12]",
"WARN: Condition always false [test/compress/conditionals.js:3,12]",
"WARN: Dropping side-effect-free statement [test/compress/conditionals.js:3,12]",
"WARN: Dropping unreachable code [test/compress/conditionals.js:3,34]",
"WARN: + in boolean context always true [test/compress/conditionals.js:10,19]",
"WARN: Boolean || always true [test/compress/conditionals.js:10,12]",
"WARN: Condition left of || always true [test/compress/conditionals.js:10,12]",
"WARN: Condition always true [test/compress/conditionals.js:10,12]",
"WARN: Dropping unreachable code [test/compress/conditionals.js:12,15]",
"WARN: Dropping side-effect-free statement [test/compress/conditionals.js:3,12]",
"WARN: Dropping side-effect-free statement [test/compress/conditionals.js:10,12]",
"WARN: Dropping unreachable code [test/compress/conditionals.js:12,15]",
]
}

Expand Down
2 changes: 1 addition & 1 deletion test/compress/sequences.js
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ issue_3490_2: {
expect: {
var b = 42, c = "FAIL";
var a;
for (c = "PASS", b; "" == typeof d;);
for (c = "PASS"; "" == typeof d;);
console.log(c, b);
}
expect_stdout: "PASS 42"
Expand Down

0 comments on commit fdbbef2

Please sign in to comment.