enhance conditionals (#5371)

This commit is contained in:
Alex Lam S.L
2022-03-02 03:00:37 +00:00
committed by GitHub
parent f8edf05c3c
commit fdbbef2991
3 changed files with 13 additions and 23 deletions

View File

@@ -9157,14 +9157,16 @@ Compressor.prototype.compress = function(node) {
var cond = fuzzy_eval(compressor, self.condition); var cond = fuzzy_eval(compressor, self.condition);
if (!cond) { if (!cond) {
AST_Node.warn("Condition always false [{file}:{line},{col}]", self.condition.start); 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); extract_declarations_from_unreachable_code(compressor, self.body, body);
if (self.alternative) body.push(self.alternative); if (self.alternative) body.push(self.alternative);
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor); return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
} else if (!(cond instanceof AST_Node)) { } else if (!(cond instanceof AST_Node)) {
AST_Node.warn("Condition always true [{file}:{line},{col}]", self.condition.start); AST_Node.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
var body = [ var body = [
make_node(AST_SimpleStatement, self.condition, { body: self.condition }), make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
self.body, self.body,
]; ];
if (self.alternative) extract_declarations_from_unreachable_code(compressor, self.alternative, 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, left: self.expression,
right: exp, right: exp,
}), }),
body: make_node(AST_BlockStatement, self, { body: make_node(AST_BlockStatement, self, { body: statements }),
body: statements,
}),
alternative: null, alternative: null,
}).optimize(compressor); }).optimize(compressor);
if (exp) statements.unshift(make_node(AST_SimpleStatement, exp, { if (exp) statements.unshift(make_node(AST_SimpleStatement, exp, { body: exp }));
body: exp, statements.unshift(make_node(AST_SimpleStatement, self.expression, { body: self.expression }));
})); return make_node(AST_BlockStatement, self, { body: statements }).optimize(compressor);
statements.unshift(make_node(AST_SimpleStatement, self.expression, {
body:self.expression,
}));
return make_node(AST_BlockStatement, self, {
body: statements,
}).optimize(compressor);
case 2: case 2:
if (!member(default_branch, body) || !no_break(body[1])) break; if (!member(default_branch, body) || !no_break(body[1])) break;
var statements = body[0].body.slice(); var statements = body[0].body.slice();
@@ -9524,14 +9518,10 @@ Compressor.prototype.compress = function(node) {
left: self.expression, left: self.expression,
right: body[0].expression, right: body[0].expression,
}), }),
body: make_node(AST_BlockStatement, body[0], { body: make_node(AST_BlockStatement, body[0], { body: statements }),
body: statements,
}),
alternative: exclusive && alternative || null, alternative: exclusive && alternative || null,
}); });
if (!exclusive && alternative) node = make_node(AST_BlockStatement, self, { if (!exclusive && alternative) node = make_node(AST_BlockStatement, self, { body: [ node, alternative ] });
body: [ node, alternative ],
});
return node.optimize(compressor); return node.optimize(compressor);
} }
return self; return self;

View File

@@ -82,14 +82,14 @@ ifs_3_should_warn: {
"WARN: Boolean && always false [test/compress/conditionals.js:3,12]", "WARN: Boolean && always false [test/compress/conditionals.js:3,12]",
"WARN: Condition left of && 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: 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: Dropping unreachable code [test/compress/conditionals.js:3,34]",
"WARN: + in boolean context always true [test/compress/conditionals.js:10,19]", "WARN: + in boolean context always true [test/compress/conditionals.js:10,19]",
"WARN: Boolean || always true [test/compress/conditionals.js:10,12]", "WARN: Boolean || always true [test/compress/conditionals.js:10,12]",
"WARN: Condition left of || 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: 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 side-effect-free statement [test/compress/conditionals.js:10,12]",
"WARN: Dropping unreachable code [test/compress/conditionals.js:12,15]",
] ]
} }

View File

@@ -1329,7 +1329,7 @@ issue_3490_2: {
expect: { expect: {
var b = 42, c = "FAIL"; var b = 42, c = "FAIL";
var a; var a;
for (c = "PASS", b; "" == typeof d;); for (c = "PASS"; "" == typeof d;);
console.log(c, b); console.log(c, b);
} }
expect_stdout: "PASS 42" expect_stdout: "PASS 42"