From 7bcb442e4ce548de2e6258792146aadb5bc24034 Mon Sep 17 00:00:00 2001 From: kzc Date: Mon, 22 May 2017 14:53:01 -0400 Subject: [PATCH] fix destructuring bugs in mangle and compress (#1992) - destructuring mangle - destructuring array default values fixes #1335 --- lib/compress.js | 12 ++- lib/scope.js | 1 - test/compress/destructuring.js | 138 +++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 5 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index dc55d4ee..c1f89bf7 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2097,9 +2097,6 @@ merge(Compressor.prototype, { })); } } - else if (node.names[i] instanceof AST_Hole) { - continue; - } else if (node.names[i] instanceof AST_ObjectKeyVal) { if (typeof node.names[i].key === "string") { initializations.add(node.names[i].key, destructuring_value); @@ -2107,7 +2104,14 @@ merge(Compressor.prototype, { } else if (node.names[i] instanceof AST_Symbol) { initializations.add(node.names[i].name, destructuring_value); - } else { + } + else if (node.names[i] instanceof AST_DefaultAssign) { + continue; + } + else if (node.names[i] instanceof AST_Hole) { + continue; + } + else { throw new Error(string_template("Unknown destructuring element of type: {type}", { type: Object.getPrototypeOf(node.names[i]).TYPE })); diff --git a/lib/scope.js b/lib/scope.js index afd6c1f2..ccb836e7 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -569,7 +569,6 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){ }); this.walk(tw); to_mangle.forEach(function(def){ - if (def.destructuring && !def.destructuring.is_array) return; def.mangle(options); }); diff --git a/test/compress/destructuring.js b/test/compress/destructuring.js index c4d927a3..b79d5734 100644 --- a/test/compress/destructuring.js +++ b/test/compress/destructuring.js @@ -405,3 +405,141 @@ destructuring_assign_of_computed_key: { expect_stdout: "42" node_version: ">=6" } + +mangle_destructuring_decl: { + options = { + evaluate: true, + unused: true, + } + mangle = { + } + input: { + function test(opts) { + let a = opts.a || { e: 7, n: 8 }; + let { t, e, n, s = 5 + 4, o, r } = a; + console.log(t, e, n, s, o, r); + } + test({a: { t: 1, e: 2, n: 3, s: 4, o: 5, r: 6 }}); + test({}); + } + expect: { + function test(t) { + let e = t.a || { e: 7, n: 8 }; + let {t: n, e: o, n: s, s: a = 9, o: c, r: l} = e; + console.log(n, o, s, a, c, l); + } + test({ a: { t: 1, e: 2, n: 3, s: 4, o: 5, r: 6 } }); + test({}); + } + expect_stdout: [ + "1 2 3 4 5 6", + "undefined 7 8 9 undefined undefined", + ] + node_version: ">=6" +} + +mangle_destructuring_assign_toplevel_true: { + options = { + toplevel: true, + evaluate: true, + unused: true, + } + mangle = { + toplevel: true, + } + beautify = { + ecma: 6 + } + input: { + function test(opts) { + let s, o, r; + let a = opts.a || { e: 7, n: 8 }; + ({ t, e, n, s = 5 + 4, o, r } = a); + console.log(t, e, n, s, o, r); + } + let t, e, n; + test({a: { t: 1, e: 2, n: 3, s: 4, o: 5, r: 6 }}); + test({}); + } + expect: { + function n(n) { + let t, a, c; + let l = n.a || { e: 7, n: 8 }; + ({t: o, e, n: s, s: t = 9, o: a, r: c} = l); + console.log(o, e, s, t, a, c); + } + let o, e, s; + n({ a: { t: 1, e: 2, n: 3, s: 4, o: 5, r: 6 } }); + n({}); + } + expect_stdout: [ + "1 2 3 4 5 6", + "undefined 7 8 9 undefined undefined", + ] + node_version: ">=6" +} + +mangle_destructuring_assign_toplevel_false: { + options = { + toplevel: false, + evaluate: true, + unused: true, + } + mangle = { + toplevel: false, + } + beautify = { + ecma: 6 + } + input: { + function test(opts) { + let s, o, r; + let a = opts.a || { e: 7, n: 8 }; + ({ t, e, n, s = 9, o, r } = a); + console.log(t, e, n, s, o, r); + } + let t, e, n; + test({a: { t: 1, e: 2, n: 3, s: 4, o: 5, r: 6 }}); + test({}); + } + expect: { + function test(o) { + let s, a, c; + let l = o.a || { e: 7, n: 8 }; + ({t, e, n, s = 9, o: a, r: c} = l); + console.log(t, e, n, s, a, c); + } + let t, e, n; + test({ a: { t: 1, e: 2, n: 3, s: 4, o: 5, r: 6 } }); + test({}); + } + expect_stdout: [ + "1 2 3 4 5 6", + "undefined 7 8 9 undefined undefined", + ] + node_version: ">=6" +} + +mangle_destructuring_decl_array: { + options = { + evaluate: true, + unused: true, + toplevel: true, + } + mangle = { + toplevel: true, + } + beautify = { + ecma: 6 + } + input: { + var [, t, e, n, s, o = 2, r = [ 1 + 2 ]] = [ 9, 8, 7, 6 ]; + console.log(t, e, n, s, o, r); + } + expect: { + var [, o, l, a, c, e = 2, g = [ 3 ]] = [ 9, 8, 7, 6 ]; + console.log(o, l, a, c, e, g); + } + expect_stdout: "8 7 6 undefined 2 [ 3 ]" + node_version: ">=6" +}