From 33a26d456be26341ada729056c2f02882eb9ae4c Mon Sep 17 00:00:00 2001 From: kzc Date: Sun, 5 Mar 2017 16:15:13 +0800 Subject: [PATCH] patch up #1543 for harmony fixes #1537 --- lib/compress.js | 2 + test/compress/collapse_vars.js | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/lib/compress.js b/lib/compress.js index dfc7e21f405..1442113ae17 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -549,6 +549,7 @@ merge(Compressor.prototype, { if (var_decl.value.is_constant()) { var ctt = new TreeTransformer(function(node) { if (node === ref + && !ctt.find_parent(AST_Destructuring) && !ctt.find_parent(AST_ForIn)) { return replace_var(node, ctt.parent(), true); } @@ -581,6 +582,7 @@ merge(Compressor.prototype, { if (unwind) return node; var parent = tt.parent(); if (node instanceof AST_Lambda + || node instanceof AST_Destructuring || node instanceof AST_Try || node instanceof AST_With || node instanceof AST_Case diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index c8fa046051b..6d9961ff463 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -1343,3 +1343,81 @@ issue_1537_for_of: { for (k of {prop: 'val'}); } } + +issue_1537_destructuring_1: { + options = { + collapse_vars: true, + } + input: { + var x = 1, y = 2; + [x] = [y]; + } + expect: { + var x = 1; + [x] = [2]; + } +} + +issue_1537_destructuring_2: { + options = { + collapse_vars: true, + } + input: { + var x = foo(); + [x] = [1]; + } + expect: { + var x = foo(); + [x] = [1]; + } +} + +issue_1537_destructuring_3: { + options = { + collapse_vars: true, + } + input: { + var x = Math.random(); + ({p: x = 9} = {v: 1}); + } + expect: { + var x = Math.random(); + ({p: x = 9} = {v: 1}); + } +} + +issue_1537_destructuring_for_in: { + options = { + collapse_vars: true, + } + input: { + var x = 1, y = 2; + (function() { + for ([[x], y] in a); + })(); + } + expect: { + var x = 1, y = 2; + (function() { + for ([[x], y] in a); + })(); + } +} + +issue_1537_destructuring_for_of: { + options = { + collapse_vars: true, + } + input: { + var x = 1, y = 2; + (function() { + for ([[x], y] of a); + })(); + } + expect: { + var x = 1, y = 2; + (function() { + for ([[x], y] of a); + })(); + } +}