From eba3a37bb510320ba95f27021c35c9fb83375b08 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 12 Oct 2019 03:42:57 +0800 Subject: [PATCH] fix boolean context detection (#3466) fixes #3465 --- lib/ast.js | 1 + test/compress/booleans.js | 88 +++++++++++++++++++++++++++++++++++++++ test/compress/evaluate.js | 31 -------------- 3 files changed, 89 insertions(+), 31 deletions(-) create mode 100644 test/compress/booleans.js diff --git a/lib/ast.js b/lib/ast.js index 5e1fbbb9..b1704915 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -985,6 +985,7 @@ TreeWalker.prototype = { fn = this.parent(++i); if (!fn) return false; } while (!(fn instanceof AST_Lambda)); + if (fn.name) return false; self = this.parent(++i); if (!self || self.TYPE != "Call" || self.expression !== fn) return false; } else { diff --git a/test/compress/booleans.js b/test/compress/booleans.js new file mode 100644 index 00000000..9b8c1664 --- /dev/null +++ b/test/compress/booleans.js @@ -0,0 +1,88 @@ +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/booleans.js:2,19]", + "WARN: Boolean || always true [test/compress/booleans.js:5,19]", + ] +} + +issue_3465_1: { + options = { + booleans: true, + } + input: { + console.log(function(a) { + return typeof a; + }() ? "PASS" : "FAIL"); + } + expect: { + console.log(function(a) { + return 1; + }() ? "PASS" : "FAIL"); + } + expect_stdout: "PASS" +} + +issue_3465_2: { + options = { + booleans: true, + } + input: { + console.log(function f(a) { + if (!a) console.log(f(42)); + return typeof a; + }() ? "PASS" : "FAIL"); + } + expect: { + console.log(function f(a) { + if (!a) console.log(f(42)); + return typeof a; + }() ? "PASS" : "FAIL"); + } + expect_stdout: [ + "number", + "PASS", + ] +} + +issue_3465_3: { + options = { + booleans: true, + passes: 2, + unused: true, + } + input: { + console.log(function f(a) { + return typeof a; + }() ? "PASS" : "FAIL"); + } + expect: { + console.log(function(a) { + return 1; + }() ? "PASS" : "FAIL"); + } + expect_stdout: "PASS" +} diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index 22c87b70..0ac117fe 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -1757,34 +1757,3 @@ 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]", - ] -} -- 2.34.1