From 569757d14dbbe04f66af65d7bf0600b3a75efd96 Mon Sep 17 00:00:00 2001 From: kzc Date: Thu, 8 Feb 2018 15:28:35 -0500 Subject: [PATCH] fix `collapse_vars` regression in destructuring (#2897) fixes #2896 --- lib/compress.js | 1 + test/compress/destructuring.js | 33 +++++++++++++++++++++++++++++++++ test/run-tests.js | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 3744e552..886a4b5b 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1512,6 +1512,7 @@ merge(Compressor.prototype, { } function may_modify(sym) { + if (!sym.definition) return true; // AST_Destructuring var def = sym.definition(); if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false; if (def.scope.get_defun_scope() !== scope) return true; diff --git a/test/compress/destructuring.js b/test/compress/destructuring.js index cdab6648..07e93222 100644 --- a/test/compress/destructuring.js +++ b/test/compress/destructuring.js @@ -439,6 +439,39 @@ mangle_destructuring_decl: { node_version: ">=6" } +mangle_destructuring_decl_collapse_vars: { + options = { + collapse_vars: true, + 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: l = 9, o: a, r: c} = e; + console.log(n, o, s, l, a, c); + } + 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, diff --git a/test/run-tests.js b/test/run-tests.js index 76b3013a..deb1f954 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -208,7 +208,7 @@ function run_compress_tests() { }); return false; } - if (0 && test.reminify && !reminify(test.options, input_code, input_formatted, test.expect_stdout)) { + if (test.reminify && !reminify(test.options, input_code, input_formatted, test.expect_stdout)) { return false; } }