Skip to content

Commit

Permalink
Fix (typeof side_effect()) in boolean context
Browse files Browse the repository at this point in the history
Fixes #1289 with suggestion by @rvanvelzen
  • Loading branch information
kzc authored and Richard van Velzen committed Oct 6, 2016
1 parent 4761d07 commit fc9804b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,12 @@ merge(Compressor.prototype, {
// typeof always returns a non-empty string, thus it's
// always true in booleans
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);
}
if (e instanceof AST_Binary && self.operator == "!") {
Expand Down
22 changes: 22 additions & 0 deletions test/compress/typeof.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,25 @@ typeof_evaluation: {
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);
}
}

0 comments on commit fc9804b

Please sign in to comment.