aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-10-23 06:46:05 +0800
committerGitHub <noreply@github.com>2019-10-23 06:46:05 +0800
commit4240fba9b831c6d78fde5aac6855410bca34c30e (patch)
treec17ac09db6b332f9bad867c6cf90e67d28244339
parent267bc70d3358170794938ab915c01092c9cf59e2 (diff)
downloadtracifyjs-4240fba9b831c6d78fde5aac6855410bca34c30e.tar.gz
tracifyjs-4240fba9b831c6d78fde5aac6855410bca34c30e.zip
fix corner cases in `unused` (#3519)
-rw-r--r--lib/compress.js3
-rw-r--r--test/compress/drop-unused.js61
2 files changed, 62 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js
index f7fedeb2..7202219a 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3738,7 +3738,7 @@ merge(Compressor.prototype, {
head.push(def);
} else {
var value = def.value
- && (sym.references.length != 1 || !sym.replaced)
+ && !def.value.single_use
&& def.value.drop_side_effect_free(compressor);
if (value) {
AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name));
@@ -6158,6 +6158,7 @@ merge(Compressor.prototype, {
if (single_use && fixed) {
def.single_use = false;
fixed._squeezed = true;
+ fixed.single_use = true;
if (fixed instanceof AST_Defun) {
fixed = make_node(AST_Function, fixed, fixed);
fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name);
diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js
index 833b8c61..dfe33cec 100644
--- a/test/compress/drop-unused.js
+++ b/test/compress/drop-unused.js
@@ -2103,7 +2103,7 @@ issue_3497: {
expect_stdout: "undefined"
}
-issue_3515: {
+issue_3515_1: {
options = {
collapse_vars: true,
reduce_vars: true,
@@ -2127,3 +2127,62 @@ issue_3515: {
}
expect_stdout: "1"
}
+
+issue_3515_2: {
+ options = {
+ side_effects: true,
+ toplevel: true,
+ unused: true,
+ }
+ input: {
+ var a = "FAIL";
+ function f() {
+ typeof b === "number";
+ delete a;
+ }
+ var b = f(a = "PASS");
+ console.log(a);
+ }
+ expect: {
+ var a = "FAIL";
+ function f() {
+ delete a;
+ }
+ f(a = "PASS");
+ console.log(a);
+ }
+ expect_stdout: "PASS"
+}
+
+issue_3515_3: {
+ options = {
+ collapse_vars: true,
+ unused: true,
+ }
+ input: {
+ var c = "FAIL";
+ (function() {
+ function f() {
+ c = "PASS";
+ }
+ var a = f();
+ var a = function g(b) {
+ b && (b.p = this);
+ }(a);
+ })();
+ console.log(c);
+ }
+ expect: {
+ var c = "FAIL";
+ (function() {
+ function f() {
+ c = "PASS";
+ }
+ (function(b) {
+ b && (b.p = this);
+ })(f());
+ })();
+ console.log(c);
+ }
+ expect_stdout: "PASS"
+}