enhance conditionals (#5371)
This commit is contained in:
@@ -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);
|
||||
@@ -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();
|
||||
@@ -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;
|
||||
|
||||
@@ -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]",
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user