fix corner case in evaluate (#3729)

This commit is contained in:
Alex Lam S.L
2020-02-19 00:41:10 +00:00
committed by GitHub
parent 7052ce5aef
commit 6092bf23de
5 changed files with 59 additions and 7 deletions

View File

@@ -3416,6 +3416,8 @@ merge(Compressor.prototype, {
line: this.start.line, line: this.start.line,
col: this.start.col col: this.start.col
}); });
} finally {
if (val instanceof RegExp) val.lastIndex = 0;
} }
} }
return this; return this;
@@ -6866,7 +6868,7 @@ merge(Compressor.prototype, {
if (node.truthy) return true; if (node.truthy) return true;
if (node.falsy) return false; if (node.falsy) return false;
if (node.is_truthy()) return true; if (node.is_truthy()) return true;
return node.evaluate(compressor); return node.evaluate(compressor, true);
} }
function is_indexFn(node) { function is_indexFn(node) {

View File

@@ -1240,11 +1240,11 @@ issue_2535_1: {
expect: { expect: {
y(); y();
x() && y(); x() && y();
(x(), 1) && y(); x(), y();
x() && y(); x() && y();
x() && y(); x() && y();
x() && y(); x() && y();
(x(), 0) && y(); x();
} }
} }

View File

@@ -16,7 +16,6 @@ wrongly_optimized: {
function func() { function func() {
foo(); foo();
} }
// TODO: optimize to `func(), bar()` func(), 1, bar();
(func(), 1) && bar();
} }
} }

View File

@@ -84,6 +84,7 @@ wrongly_optimized: {
options = { options = {
booleans: true, booleans: true,
conditionals: true, conditionals: true,
dead_code: true,
evaluate: true, evaluate: true,
expression: true, expression: true,
} }
@@ -99,8 +100,8 @@ wrongly_optimized: {
function func() { function func() {
foo(); foo();
} }
// TODO: optimize to `func(), bar()` func(), 1;
if (func(), 1) bar(); bar();
} }
} }

View File

@@ -423,3 +423,53 @@ var_test_global: {
"PASS", "PASS",
] ]
} }
lazy_boolean: {
options = {
evaluate: true,
passes: 2,
side_effects: true,
unsafe: true,
}
input: {
/b/.exec({}) && console.log("PASS");
/b/.test({}) && console.log("PASS");
/b/g.exec({}) && console.log("PASS");
/b/g.test({}) && console.log("PASS");
}
expect: {
console.log("PASS");
console.log("PASS");
console.log("PASS");
console.log("PASS");
}
expect_stdout: [
"PASS",
"PASS",
"PASS",
"PASS",
]
}
reset_state_between_evaluate: {
options = {
evaluate: true,
passes: 2,
unsafe: true,
}
input: {
console.log(function() {
for (var a in /[abc4]/g.exec("a"))
return "PASS";
return "FAIL";
}());
}
expect: {
console.log(function() {
for (var a in /[abc4]/g.exec("a"))
return "PASS";
return "FAIL";
}());
}
expect_stdout: "PASS"
}