From fa3250199af84865363fc80a280ff035c7e495c6 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 30 Mar 2018 16:23:09 +0900 Subject: mangle unused nested `AST_SymbolCatch` correctly (#3038) fixes #3035 --- lib/scope.js | 13 +- test/compress/ie8.js | 540 +++++++++++++++++++++++++++++++++++++++++++++ test/compress/screw-ie8.js | 466 -------------------------------------- 3 files changed, 548 insertions(+), 471 deletions(-) create mode 100644 test/compress/ie8.js delete mode 100644 test/compress/screw-ie8.js diff --git a/lib/scope.js b/lib/scope.js index 7cbd82c1..a9431524 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -434,16 +434,19 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){ var redef = def.redefined(); if (redef) { redefined.push(def); - def.references.forEach(function(ref) { - ref.thedef = redef; - ref.reference(options); - ref.thedef = def; - }); + reference(node.argname); + def.references.forEach(reference); } descend(); if (!redef) mangle(def); return true; } + + function reference(sym) { + sym.thedef = redef; + sym.reference(options); + sym.thedef = def; + } }); this.walk(tw); redefined.forEach(mangle); diff --git a/test/compress/ie8.js b/test/compress/ie8.js new file mode 100644 index 00000000..b21b8411 --- /dev/null +++ b/test/compress/ie8.js @@ -0,0 +1,540 @@ +do_screw: { + options = { + ie8: false, + } + beautify = { + ie8: false, + ascii_only: true, + } + input: { + f("\v"); + } + expect_exact: 'f("\\v");' +} + +dont_screw: { + options = { + ie8: true, + } + beautify = { + ie8: true, + ascii_only: true, + } + input: { + f("\v"); + } + expect_exact: 'f("\\x0B");' +} + +do_screw_constants: { + options = { + ie8: false, + } + input: { + f(undefined, Infinity); + } + expect_exact: "f(void 0,1/0);" +} + +dont_screw_constants: { + options = { + ie8: true, + } + input: { + f(undefined, Infinity); + } + expect_exact: "f(undefined,Infinity);" +} + +do_screw_try_catch: { + options = { + ie8: false, + } + mangle = { + ie8: false, + } + beautify = { + ie8: false, + } + input: { + good = function(e){ + return function(error){ + try{ + e() + } catch(e) { + error(e) + } + } + }; + } + expect: { + good = function(n){ + return function(t){ + try{ + n() + } catch(n) { + t(n) + } + } + }; + } +} + +dont_screw_try_catch: { + options = { + ie8: true, + } + mangle = { + ie8: true, + } + beautify = { + ie8: true, + } + input: { + bad = function(e){ + return function(error){ + try{ + e() + } catch(e) { + error(e) + } + } + }; + } + expect: { + bad = function(t){ + return function(n){ + try{ + t() + } catch(t) { + n(t) + } + } + }; + } +} + +do_screw_try_catch_undefined: { + options = { + ie8: false, + } + mangle = { + ie8: false, + } + beautify = { + ie8: false, + } + input: { + function a(b){ + try { + throw 'Stuff'; + } catch (undefined) { + console.log('caught: ' + undefined); + } + console.log('undefined is ' + undefined); + return b === undefined; + }; + } + expect: { + function a(o){ + try{ + throw "Stuff" + } catch (o) { + console.log("caught: "+o) + } + console.log("undefined is " + void 0); + return void 0===o + } + } + expect_stdout: true +} + +dont_screw_try_catch_undefined: { + options = { + ie8: true, + } + mangle = { + ie8: true, + } + beautify = { + ie8: true, + } + input: { + function a(b){ + try { + throw 'Stuff'; + } catch (undefined) { + console.log('caught: ' + undefined); + } + console.log('undefined is ' + undefined); + return b === undefined; + }; + } + expect: { + function a(n){ + try{ + throw "Stuff" + } catch (undefined) { + console.log("caught: " + undefined) + } + console.log("undefined is " + undefined); + return n === undefined + } + } + expect_stdout: true +} + +reduce_vars: { + options = { + evaluate: true, + reduce_funcs: true, + reduce_vars: true, + ie8: true, + unused: true, + } + mangle = { + ie8: true, + } + input: { + function f() { + var a; + try { + x(); + } catch (a) { + y(); + } + alert(a); + } + } + expect: { + function f() { + var t; + try { + x(); + } catch (t) { + y(); + } + alert(t); + } + } +} + +issue_1586_1: { + options = { + ie8: true, + } + mangle = { + ie8: true, + } + input: { + function f() { + try { + x(); + } catch (err) { + console.log(err.message); + } + } + } + expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}" +} + +issue_1586_2: { + options = { + ie8: false, + } + mangle = { + ie8: false, + } + input: { + function f() { + try { + x(); + } catch (err) { + console.log(err.message); + } + } + } + expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}" +} + +issue_2120_1: { + mangle = { + ie8: false, + } + input: { + "aaaaaaaa"; + var a = 1, b = "FAIL"; + try { + throw 1; + } catch (c) { + try { + throw 0; + } catch (a) { + if (c) b = "PASS"; + } + } + console.log(b); + } + expect: { + "aaaaaaaa"; + var a = 1, b = "FAIL"; + try { + throw 1; + } catch (t) { + try { + throw 0; + } catch (a) { + if (t) b = "PASS"; + } + } + console.log(b); + } + expect_stdout: "PASS" +} + +issue_2120_2: { + mangle = { + ie8: true, + } + input: { + "aaaaaaaa"; + var a = 1, b = "FAIL"; + try { + throw 1; + } catch (c) { + try { + throw 0; + } catch (a) { + if (c) b = "PASS"; + } + } + console.log(b); + } + expect: { + "aaaaaaaa"; + var a = 1, b = "FAIL"; + try { + throw 1; + } catch (c) { + try { + throw 0; + } catch (a) { + if (c) b = "PASS"; + } + } + console.log(b); + } + expect_stdout: "PASS" +} + +issue_2254_1: { + mangle = { + ie8: false, + } + input: { + "eeeeee"; + try { + console.log(f("PASS")); + } catch (e) {} + function f(s) { + try { + throw "FAIL"; + } catch (e) { + return s; + } + } + } + expect: { + "eeeeee"; + try { + console.log(f("PASS")); + } catch (e) {} + function f(t) { + try { + throw "FAIL"; + } catch (e) { + return t; + } + } + } + expect_stdout: "PASS" +} + +issue_2254_2: { + mangle = { + ie8: true, + } + input: { + "eeeeee"; + try { + console.log(f("PASS")); + } catch (e) {} + function f(s) { + try { + throw "FAIL"; + } catch (e) { + return s; + } + } + } + expect: { + "eeeeee"; + try { + console.log(f("PASS")); + } catch (e) {} + function f(t) { + try { + throw "FAIL"; + } catch (e) { + return t; + } + } + } + expect_stdout: "PASS" +} + +issue_24_1: { + mangle = { + ie8: false, + } + input: { + (function(a) { + console.log(typeof function f(){} === typeof a ? "FAIL" : "PASS"); + })(); + } + expect: { + (function(o) { + console.log(typeof function o(){} === typeof o ? "FAIL" : "PASS"); + })(); + } + expect_stdout: "PASS" +} + +issue_24_2: { + mangle = { + ie8: true, + } + input: { + (function(a) { + console.log(typeof function f(){} === typeof a ? "FAIL" : "PASS"); + })(); + } + expect: { + (function(n) { + console.log(typeof function o(){} === typeof n ? "FAIL" : "PASS"); + })(); + } + expect_stdout: "PASS" +} + +issue_2976_1: { + mangle = { + ie8: false, + } + input: { + console.log(function f() { + var a; + return a === f ? "FAIL" : "PASS"; + }()); + } + expect: { + console.log(function n() { + var o; + return o === n ? "FAIL" : "PASS"; + }()); + } + expect_stdout: "PASS" +} + +issue_2976_2: { + mangle = { + ie8: true, + } + input: { + console.log(function f() { + var a; + return a === f ? "FAIL" : "PASS"; + }()); + } + expect: { + console.log(function n() { + var o; + return o === n ? "FAIL" : "PASS"; + }()); + } + expect_stdout: "PASS" +} + +issue_3035: { + mangle = { + ie8: false, + } + input: { + var c = "FAIL"; + (function(a) { + try { + throw 1; + } catch (b) { + try { + throw 0; + } catch (a) { + b && (c = "PASS"); + } + } + })(); + console.log(c); + } + expect: { + var c = "FAIL"; + (function(o) { + try { + throw 1; + } catch (t) { + try { + throw 0; + } catch (o) { + t && (c = "PASS"); + } + } + })(); + console.log(c); + } + expect_stdout: "PASS" +} + +issue_3035_ie8: { + mangle = { + ie8: true, + } + input: { + var c = "FAIL"; + (function(a) { + try { + throw 1; + } catch (b) { + try { + throw 0; + } catch (a) { + b && (c = "PASS"); + } + } + })(); + console.log(c); + } + expect: { + var c = "FAIL"; + (function(t) { + try { + throw 1; + } catch (o) { + try { + throw 0; + } catch (t) { + o && (c = "PASS"); + } + } + })(); + console.log(c); + } + expect_stdout: "PASS" +} diff --git a/test/compress/screw-ie8.js b/test/compress/screw-ie8.js deleted file mode 100644 index 3a95eb6b..00000000 --- a/test/compress/screw-ie8.js +++ /dev/null @@ -1,466 +0,0 @@ -do_screw: { - options = { - ie8: false, - } - beautify = { - ie8: false, - ascii_only: true, - } - input: { - f("\v"); - } - expect_exact: 'f("\\v");' -} - -dont_screw: { - options = { - ie8: true, - } - beautify = { - ie8: true, - ascii_only: true, - } - input: { - f("\v"); - } - expect_exact: 'f("\\x0B");' -} - -do_screw_constants: { - options = { - ie8: false, - } - input: { - f(undefined, Infinity); - } - expect_exact: "f(void 0,1/0);" -} - -dont_screw_constants: { - options = { - ie8: true, - } - input: { - f(undefined, Infinity); - } - expect_exact: "f(undefined,Infinity);" -} - -do_screw_try_catch: { - options = { - ie8: false, - } - mangle = { - ie8: false, - } - beautify = { - ie8: false, - } - input: { - good = function(e){ - return function(error){ - try{ - e() - } catch(e) { - error(e) - } - } - }; - } - expect: { - good = function(n){ - return function(t){ - try{ - n() - } catch(n) { - t(n) - } - } - }; - } -} - -dont_screw_try_catch: { - options = { - ie8: true, - } - mangle = { - ie8: true, - } - beautify = { - ie8: true, - } - input: { - bad = function(e){ - return function(error){ - try{ - e() - } catch(e) { - error(e) - } - } - }; - } - expect: { - bad = function(t){ - return function(n){ - try{ - t() - } catch(t) { - n(t) - } - } - }; - } -} - -do_screw_try_catch_undefined: { - options = { - ie8: false, - } - mangle = { - ie8: false, - } - beautify = { - ie8: false, - } - input: { - function a(b){ - try { - throw 'Stuff'; - } catch (undefined) { - console.log('caught: ' + undefined); - } - console.log('undefined is ' + undefined); - return b === undefined; - }; - } - expect: { - function a(o){ - try{ - throw "Stuff" - } catch (o) { - console.log("caught: "+o) - } - console.log("undefined is " + void 0); - return void 0===o - } - } - expect_stdout: true -} - -dont_screw_try_catch_undefined: { - options = { - ie8: true, - } - mangle = { - ie8: true, - } - beautify = { - ie8: true, - } - input: { - function a(b){ - try { - throw 'Stuff'; - } catch (undefined) { - console.log('caught: ' + undefined); - } - console.log('undefined is ' + undefined); - return b === undefined; - }; - } - expect: { - function a(n){ - try{ - throw "Stuff" - } catch (undefined) { - console.log("caught: " + undefined) - } - console.log("undefined is " + undefined); - return n === undefined - } - } - expect_stdout: true -} - -reduce_vars: { - options = { - evaluate: true, - reduce_funcs: true, - reduce_vars: true, - ie8: true, - unused: true, - } - mangle = { - ie8: true, - } - input: { - function f() { - var a; - try { - x(); - } catch (a) { - y(); - } - alert(a); - } - } - expect: { - function f() { - var t; - try { - x(); - } catch (t) { - y(); - } - alert(t); - } - } -} - -issue_1586_1: { - options = { - ie8: true, - } - mangle = { - ie8: true, - } - input: { - function f() { - try { - x(); - } catch (err) { - console.log(err.message); - } - } - } - expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}" -} - -issue_1586_2: { - options = { - ie8: false, - } - mangle = { - ie8: false, - } - input: { - function f() { - try { - x(); - } catch (err) { - console.log(err.message); - } - } - } - expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}" -} - -issue_2120_1: { - mangle = { - ie8: false, - } - input: { - "aaaaaaaa"; - var a = 1, b = "FAIL"; - try { - throw 1; - } catch (c) { - try { - throw 0; - } catch (a) { - if (c) b = "PASS"; - } - } - console.log(b); - } - expect: { - "aaaaaaaa"; - var a = 1, b = "FAIL"; - try { - throw 1; - } catch (t) { - try { - throw 0; - } catch (a) { - if (t) b = "PASS"; - } - } - console.log(b); - } - expect_stdout: "PASS" -} - -issue_2120_2: { - mangle = { - ie8: true, - } - input: { - "aaaaaaaa"; - var a = 1, b = "FAIL"; - try { - throw 1; - } catch (c) { - try { - throw 0; - } catch (a) { - if (c) b = "PASS"; - } - } - console.log(b); - } - expect: { - "aaaaaaaa"; - var a = 1, b = "FAIL"; - try { - throw 1; - } catch (c) { - try { - throw 0; - } catch (a) { - if (c) b = "PASS"; - } - } - console.log(b); - } - expect_stdout: "PASS" -} - -issue_2254_1: { - mangle = { - ie8: false, - } - input: { - "eeeeee"; - try { - console.log(f("PASS")); - } catch (e) {} - function f(s) { - try { - throw "FAIL"; - } catch (e) { - return s; - } - } - } - expect: { - "eeeeee"; - try { - console.log(f("PASS")); - } catch (e) {} - function f(t) { - try { - throw "FAIL"; - } catch (e) { - return t; - } - } - } - expect_stdout: "PASS" -} - -issue_2254_2: { - mangle = { - ie8: true, - } - input: { - "eeeeee"; - try { - console.log(f("PASS")); - } catch (e) {} - function f(s) { - try { - throw "FAIL"; - } catch (e) { - return s; - } - } - } - expect: { - "eeeeee"; - try { - console.log(f("PASS")); - } catch (e) {} - function f(t) { - try { - throw "FAIL"; - } catch (e) { - return t; - } - } - } - expect_stdout: "PASS" -} - -issue_24_1: { - mangle = { - ie8: false, - } - input: { - (function(a) { - console.log(typeof function f(){} === typeof a ? "FAIL" : "PASS"); - })(); - } - expect: { - (function(o) { - console.log(typeof function o(){} === typeof o ? "FAIL" : "PASS"); - })(); - } - expect_stdout: "PASS" -} - -issue_24_2: { - mangle = { - ie8: true, - } - input: { - (function(a) { - console.log(typeof function f(){} === typeof a ? "FAIL" : "PASS"); - })(); - } - expect: { - (function(n) { - console.log(typeof function o(){} === typeof n ? "FAIL" : "PASS"); - })(); - } - expect_stdout: "PASS" -} - -issue_2976_1: { - mangle = { - ie8: false, - } - input: { - console.log(function f() { - var a; - return a === f ? "FAIL" : "PASS"; - }()); - } - expect: { - console.log(function n() { - var o; - return o === n ? "FAIL" : "PASS"; - }()); - } - expect_stdout: "PASS" -} - -issue_2976_2: { - mangle = { - ie8: true, - } - input: { - console.log(function f() { - var a; - return a === f ? "FAIL" : "PASS"; - }()); - } - expect: { - console.log(function n() { - var o; - return o === n ? "FAIL" : "PASS"; - }()); - } - expect_stdout: "PASS" -} -- cgit v1.2.3