From 07f64d4050cd28cf2a837d30ce2938fa9628ff57 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 31 Mar 2018 15:03:46 +0900 Subject: [PATCH] fix escape analysis on `AST_New` (#3043) fixes #3042 --- lib/compress.js | 2 +- test/compress/reduce_vars.js | 72 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 52a23ee4..477fa307 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -428,7 +428,7 @@ merge(Compressor.prototype, { var parent = tw.parent(level); if (value && value.is_constant()) return; if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right - || parent instanceof AST_Call && node !== parent.expression + || parent instanceof AST_Call && (node !== parent.expression || parent instanceof AST_New) || parent instanceof AST_Exit && node === parent.value && node.scope !== d.scope || parent instanceof AST_VarDef && node === parent.value) { if (depth > 1 && !(value && value.is_constant_expression(scope))) depth = 1; diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 3d11ba42..59a99b9c 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -5582,3 +5582,75 @@ issue_2992: { } expect_stdout: "PASS" } + +issue_3042_1: { + options = { + reduce_funcs: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function f() {} + var a = [ 1, 2 ].map(function() { + return new f(); + }); + console.log(a[0].constructor === a[1].constructor); + } + expect: { + function f() {} + var a = [ 1, 2 ].map(function() { + return new f(); + }); + console.log(a[0].constructor === a[1].constructor); + } + expect_stdout: "true" +} + +issue_3042_2: { + options = { + reduce_funcs: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function Foo() { + this.isFoo = function(o) { + return o instanceof Foo; + }; + } + function FooCollection() { + this.foos = [1, 1].map(function() { + return new Foo(); + }); + } + var fooCollection = new FooCollection(); + console.log(fooCollection.foos[0].isFoo(fooCollection.foos[0])); + console.log(fooCollection.foos[0].isFoo(fooCollection.foos[1])); + console.log(fooCollection.foos[1].isFoo(fooCollection.foos[0])); + console.log(fooCollection.foos[1].isFoo(fooCollection.foos[1])); + } + expect: { + function Foo() { + this.isFoo = function(o) { + return o instanceof Foo; + }; + } + var fooCollection = new function() { + this.foos = [1, 1].map(function() { + return new Foo(); + }); + }(); + console.log(fooCollection.foos[0].isFoo(fooCollection.foos[0])); + console.log(fooCollection.foos[0].isFoo(fooCollection.foos[1])); + console.log(fooCollection.foos[1].isFoo(fooCollection.foos[0])); + console.log(fooCollection.foos[1].isFoo(fooCollection.foos[1])); + } + expect_stdout: [ + "true", + "true", + "true", + "true", + ] +} -- 2.34.1