Repository: babel/minify (js)

Issue:
var assignment destructuring minifies to wrong syntax
<!---
Thanks for filing an issue 😄! Before you submit, please read the following:

Search open/closed issues before submitting since someone might have asked the same thing before!

If you have a support request or question please submit them to the #minify channel on babeljs slack: https://babeljs.slack.com/messages/minify

Also have a look at the Debugging guidelines: https://github.com/babel/minify/blob/master/CONTRIBUTING.md#debugging

Things required:

For a bug:

1. Minimal code to reproduce the bug
2. Stack Trace if there is an Error thrown during minification
3. Configuration - babel-minify configuration or babelrc
4. Whether you used it with other presets/plugins - like es2015 or env
5. Environment: gulp, rollup, webpack, babel-cli, etc...?

For an enhancement:

1. Description
2. Code Examples with transformations

-->

#### Input Code

```js
if (false) {
  var {x, y} = foo();
}
```

#### Actual Output
<!-- If an error is thrown, please provide the stack trace here -->

```js
var{x,y};
```

#### Expected Output

```js
var x,y;
```

#### Details
<!---
Anything else at all
-->
This bug is annoying especially when you disable scoped variables due to Safari bugs related to for statements with scoped variables.


Pull request description:
fix(dce): extract only binding identifiers during block removal
+ Fix #710

Patch:
diff --git a/packages/babel-plugin-minify-dead-code-elimination/src/index.js b/packages/babel-plugin-minify-dead-code-elimination/src/index.js
index 90e7fc9..25fe626 100644
--- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js
+++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js
@@ -948,7 +948,11 @@ module.exports = ({ types: t, traverse }) => {
 
     if (path.isVariableDeclaration({ kind: "var" })) {
       for (const decl of path.node.declarations) {
-        declarators.push(t.variableDeclarator(decl.id));
+        const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id));
+
+        declarators.push(
+          ...bindingIds.map(name => t.variableDeclarator(t.identifier(name)))
+        );
       }
     } else {
       path.traverse({
@@ -957,7 +961,12 @@ module.exports = ({ types: t, traverse }) => {
           if (!isSameFunctionScope(varPath, path)) return;
 
           for (const decl of varPath.node.declarations) {
-            declarators.push(t.variableDeclarator(decl.id));
+            const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id));
+            declarators.push(
+              ...bindingIds.map(name =>
+                t.variableDeclarator(t.identifier(name))
+              )
+            );
           }
         }
       });


Test patch:
diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js
index 3c4519e..4c622c3 100644
--- a/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js
+++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js
@@ -2516,4 +2516,17 @@ describe("dce-plugin", () => {
       }
     `
   );
+
+  thePlugin(
+    "extractVars should extract only identifiers & ignore ObjectPattern/ArrayPattern",
+    `
+      if (false) {
+        var {x, y} = foo();
+        var [a, b] = bar();
+      }
+    `,
+    `
+      var x, y, a, b;
+    `
+  );
 });