Skip to content

Commit

Permalink
detect boolean context across IIFEs (#3461)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Oct 10, 2019
1 parent b18f717 commit 33c94d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ast.js
Expand Up @@ -979,6 +979,14 @@ TreeWalker.prototype = {
|| p instanceof AST_Conditional
|| p.tail_node() === self) {
self = p;
} else if (p instanceof AST_Return) {
var fn;
do {
fn = this.parent(++i);
if (!fn) return false;
} while (!(fn instanceof AST_Lambda));
self = this.parent(++i);
if (!self || self.TYPE != "Call" || self.expression !== fn) return false;
} else {
return false;
}
Expand Down
31 changes: 31 additions & 0 deletions test/compress/evaluate.js
Expand Up @@ -1757,3 +1757,34 @@ issue_3387_2: {
}
expect_stdout: "NaN"
}

iife_boolean_context: {
options = {
booleans: true,
evaluate: true,
}
input: {
console.log(function() {
return Object(1) || false;
}() ? "PASS" : "FAIL");
console.log(function() {
return [].length || true;
}() ? "PASS" : "FAIL");
}
expect: {
console.log(function() {
return Object(1);
}() ? "PASS" : "FAIL");
console.log(function() {
return [].length, 1;
}() ? "PASS" : "FAIL");
}
expect_stdout: [
"PASS",
"PASS",
]
expect_warnings: [
"WARN: Dropping side-effect-free || [test/compress/evaluate.js:2,19]",
"WARN: Boolean || always true [test/compress/evaluate.js:5,19]",
]
}

0 comments on commit 33c94d3

Please sign in to comment.