aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2018-05-21 15:53:51 +0800
committerGitHub <noreply@github.com>2018-05-21 15:53:51 +0800
commitb5af8a1914b311e763ba108eaa234c882244f9c6 (patch)
tree9305c6332d966675d36ab3317ee92bef03005bfb
parentc14d09ba84605d27238aef2b3f251674d7e0394c (diff)
downloadtracifyjs-b5af8a1914b311e763ba108eaa234c882244f9c6.tar.gz
tracifyjs-b5af8a1914b311e763ba108eaa234c882244f9c6.zip
fix corner case in `reduce_vars` (#3141)
fixes #3140
-rw-r--r--lib/compress.js13
-rw-r--r--test/compress/reduce_vars.js189
2 files changed, 201 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js
index efc7c47f..498729d0 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -380,6 +380,16 @@ merge(Compressor.prototype, {
mark(tw, def, true);
}
});
+ scope.may_call_this = function() {
+ scope.may_call_this = noop;
+ if (!scope.contains_this()) return;
+ scope.functions.each(function(def) {
+ if (def.init instanceof AST_Defun && !(def.id in tw.defun_ids)) {
+ tw.defun_ids[def.id] = false;
+ }
+ });
+ return true;
+ };
}
function mark_defun(tw, def) {
@@ -556,6 +566,7 @@ merge(Compressor.prototype, {
return true;
});
def(AST_Call, function(tw, descend) {
+ if (tw.find_parent(AST_Scope).may_call_this()) return;
var exp = this.expression;
if (!(exp instanceof AST_SymbolRef)) return;
var def = exp.definition();
@@ -6278,7 +6289,7 @@ merge(Compressor.prototype, {
return self;
});
- AST_Lambda.DEFMETHOD("contains_this", function() {
+ AST_Scope.DEFMETHOD("contains_this", function() {
var result;
var self = this;
self.walk(new TreeWalker(function(node) {
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index ac55f81d..626893f4 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -6178,3 +6178,192 @@ issue_3125: {
}
expect_stdout: "7"
}
+
+issue_3140_1: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ (function() {
+ var a;
+ function f() {
+ }
+ f.g = function g() {
+ function h() {
+ console.log(a ? "PASS" : "FAIL");
+ }
+ a = true;
+ this();
+ a = false;
+ h.g = g;
+ return h;
+ };
+ return f;
+ })().g().g();
+ }
+ expect: {
+ (function() {
+ var a;
+ function f() {
+ }
+ f.g = function g() {
+ function h() {
+ console.log(a ? "PASS" : "FAIL");
+ }
+ a = true;
+ this();
+ a = false;
+ h.g = g;
+ return h;
+ };
+ return f;
+ })().g().g();
+ }
+ expect_stdout: "PASS"
+}
+
+issue_3140_2: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ (function() {
+ var a;
+ function f() {
+ }
+ f.g = function g() {
+ var self = this;
+ function h() {
+ console.log(a ? "PASS" : "FAIL");
+ }
+ a = true;
+ self();
+ a = false;
+ h.g = g;
+ return h;
+ };
+ return f;
+ })().g().g();
+ }
+ expect: {
+ (function() {
+ var a;
+ function f() {
+ }
+ f.g = function g() {
+ function h() {
+ console.log(a ? "PASS" : "FAIL");
+ }
+ a = true;
+ this();
+ a = false;
+ h.g = g;
+ return h;
+ };
+ return f;
+ })().g().g();
+ }
+ expect_stdout: "PASS"
+}
+
+issue_3140_3: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ (function() {
+ var a;
+ function f() {
+ }
+ f.g = function g() {
+ var self = this;
+ function h() {
+ console.log(a ? "PASS" : "FAIL");
+ }
+ a = true;
+ (function() {
+ return self;
+ })()();
+ a = false;
+ h.g = g;
+ return h;
+ };
+ return f;
+ })().g().g();
+ }
+ expect: {
+ (function() {
+ var a;
+ function f() {
+ }
+ f.g = function g() {
+ var self = this;
+ function h() {
+ console.log(a ? "PASS" : "FAIL");
+ }
+ a = true;
+ (function() {
+ return self;
+ })()();
+ a = false;
+ h.g = g;
+ return h;
+ };
+ return f;
+ })().g().g();
+ }
+ expect_stdout: "PASS"
+}
+
+issue_3140_4: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ (function() {
+ var a;
+ function f() {
+ }
+ f.g = function g() {
+ var o = {
+ p: this
+ };
+ function h() {
+ console.log(a ? "PASS" : "FAIL");
+ }
+ a = true;
+ o.p();
+ a = false;
+ h.g = g;
+ return h;
+ };
+ return f;
+ })().g().g();
+ }
+ expect: {
+ (function() {
+ var a;
+ function f() {
+ }
+ f.g = function g() {
+ var o = {
+ p: this
+ };
+ function h() {
+ console.log(a ? "PASS" : "FAIL");
+ }
+ a = true;
+ o.p();
+ a = false;
+ h.g = g;
+ return h;
+ };
+ return f;
+ })().g().g();
+ }
+ expect_stdout: "PASS"
+}