Fix (typeof side_effect()) in boolean context
Fixes #1289 with suggestion by @rvanvelzen
This commit is contained in:
@@ -2314,6 +2314,12 @@ merge(Compressor.prototype, {
|
|||||||
// typeof always returns a non-empty string, thus it's
|
// typeof always returns a non-empty string, thus it's
|
||||||
// always true in booleans
|
// always true in booleans
|
||||||
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
|
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
|
||||||
|
if (self.expression.has_side_effects(compressor)) {
|
||||||
|
return make_node(AST_Seq, self, {
|
||||||
|
car: self.expression,
|
||||||
|
cdr: make_node(AST_True, self)
|
||||||
|
});
|
||||||
|
}
|
||||||
return make_node(AST_True, self);
|
return make_node(AST_True, self);
|
||||||
}
|
}
|
||||||
if (e instanceof AST_Binary && self.operator == "!") {
|
if (e instanceof AST_Binary && self.operator == "!") {
|
||||||
|
|||||||
@@ -23,3 +23,25 @@ typeof_evaluation: {
|
|||||||
h='undefined';
|
h='undefined';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typeof_in_boolean_context: {
|
||||||
|
options = {
|
||||||
|
booleans : true,
|
||||||
|
evaluate : true,
|
||||||
|
conditionals : true,
|
||||||
|
};
|
||||||
|
input: {
|
||||||
|
function f1(x) { return typeof x ? "yes" : "no"; }
|
||||||
|
function f2() { return typeof g()? "Yes" : "No"; }
|
||||||
|
typeof 0 ? foo() : bar();
|
||||||
|
!typeof console.log(1);
|
||||||
|
var a = !typeof console.log(2);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f1(x) { return "yes"; }
|
||||||
|
function f2() { return g(), "Yes"; }
|
||||||
|
foo();
|
||||||
|
!(console.log(1), !0);
|
||||||
|
var a = !(console.log(2), !0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user