account for side-effects in comparisons of null & undefined

This commit is contained in:
alexlamsl 2018-02-01 17:51:38 +08:00
parent fad6766a90
commit f41141750d
2 changed files with 134 additions and 80 deletions

View File

@ -4800,6 +4800,7 @@ merge(Compressor.prototype, {
&& lhs.operator == self.right.operator
&& (is_undefined(lhs.left, compressor) && self.right.left instanceof AST_Null
|| lhs.left instanceof AST_Null && is_undefined(self.right.left, compressor))
&& !lhs.right.has_side_effects(compressor)
&& lhs.right.equivalent_to(self.right.right)) {
var combined = make_node(AST_Binary, self, {
operator: lhs.operator.slice(0, -1),

View File

@ -118,6 +118,7 @@ issue_2857_1: {
comparisons: true,
}
input: {
function f(a) {
a === undefined || a === null;
a === undefined || a !== null;
a !== undefined || a === null;
@ -127,7 +128,9 @@ issue_2857_1: {
a !== undefined && a === null;
a !== undefined && a !== null;
}
}
expect: {
function f(a) {
null == a;
void 0 === a || null !== a;
void 0 !== a || null === a;
@ -138,12 +141,14 @@ issue_2857_1: {
null != a;
}
}
}
issue_2857_2: {
options = {
comparisons: true,
}
input: {
function f(a, p) {
a === undefined || a === null || p;
a === undefined || a !== null || p;
a !== undefined || a === null || p;
@ -153,7 +158,9 @@ issue_2857_2: {
a !== undefined && a === null || p;
a !== undefined && a !== null || p;
}
}
expect: {
function f(a, p) {
null == a || p;
void 0 === a || null !== a || p;
void 0 !== a || null === a || p;
@ -164,12 +171,14 @@ issue_2857_2: {
null != a || p;
}
}
}
issue_2857_3: {
options = {
comparisons: true,
}
input: {
function f(a, p) {
a === undefined || a === null && p;
a === undefined || a !== null && p;
a !== undefined || a === null && p;
@ -179,7 +188,9 @@ issue_2857_3: {
a !== undefined && a === null && p;
a !== undefined && a !== null && p;
}
}
expect: {
function f(a, p) {
void 0 === a || null === a && p;
void 0 === a || null !== a && p;
void 0 !== a || null === a && p;
@ -190,12 +201,14 @@ issue_2857_3: {
null != a && p;
}
}
}
issue_2857_4: {
options = {
comparisons: true,
}
input: {
function f(a, p) {
p || a === undefined || a === null;
p || a === undefined || a !== null;
p || a !== undefined || a === null;
@ -205,7 +218,9 @@ issue_2857_4: {
p || a !== undefined && a === null;
p || a !== undefined && a !== null;
}
}
expect: {
function f(a, p) {
p || null == a;
p || void 0 === a || null !== a;
p || void 0 !== a || null === a;
@ -216,12 +231,14 @@ issue_2857_4: {
p || null != a;
}
}
}
issue_2857_5: {
options = {
comparisons: true,
}
input: {
function f(a, p) {
p && a === undefined || a === null;
p && a === undefined || a !== null;
p && a !== undefined || a === null;
@ -231,7 +248,9 @@ issue_2857_5: {
p && a !== undefined && a === null;
p && a !== undefined && a !== null;
}
}
expect: {
function f(a, p) {
p && void 0 === a || null === a;
p && void 0 === a || null !== a;
p && void 0 !== a || null === a;
@ -242,3 +261,37 @@ issue_2857_5: {
p && null != a;
}
}
}
issue_2857_6: {
options = {
comparisons: true,
pure_getters: "strict",
reduce_vars: true,
}
input: {
function f(a) {
if (({}).b === undefined || {}.b === null)
return a.b !== undefined && a.b !== null;
}
console.log(f({
a: [ null ],
get b() {
return this.a.shift();
}
}));
}
expect: {
function f(a) {
if (null == {}.b)
return void 0 !== a.b && null !== a.b;
}
console.log(f({
a: [ null ],
get b() {
return this.a.shift();
}
}));
}
expect_stdout: "true"
}