Repository: babel/minify (js)

Issue:
Wrong code generated for while statement, edgecase
**Describe the bug**

The falsy while statement should not remove variable declarations.

**To Reproduce**

Minimal code to reproduce the bug

```js
function a() {
  ++i;
  while (false) {
    var i = meow();
  }
}
```

**Actual Output**

If there is no Error thrown,

```js
function a(){
  ++i;
}
```

**Expected Output**

```js
function a(){
  ++i;
  var i;
}
```

**Configuration**

How are you using babel-minify?

`babel-minify CLI`

babel-minify version: `0.5.0`

babel version : `7.0.0-beta.49`

babel-minify-config: default

**Possible solution**

Looks like path should not be removed but instead replaced with extracted variables.


Pull request description:
Fixes #922, extractVars instead of path removal
Discussion started here #898 

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 0eb1d74..8c26ed8 100644
--- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js
+++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js
@@ -707,7 +707,7 @@ module.exports = ({ types: t, traverse }) => {
       const test = path.get("test");
       const result = evaluate(test, { tdz: this.tdz });
       if (result.confident && test.isPure() && !result.value) {
-        path.remove();
+        path.replaceWithMultiple(extractVars(path.get("body")));
       }
     },
 


Test patch:
diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-922/actual.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-922/actual.js
new file mode 100644
index 0000000..e37e281
--- /dev/null
+++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-922/actual.js
@@ -0,0 +1,6 @@
+function a() {
+  ++i;
+  while (false) {
+    var i = meow();
+  }
+}
\ No newline at end of file
diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-922/expected.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-922/expected.js
new file mode 100644
index 0000000..64f8e39
--- /dev/null
+++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-922/expected.js
@@ -0,0 +1,4 @@
+function a() {
+  ++i;
+  var i;
+}
\ No newline at end of file
diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/loops/expected.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/loops/expected.js
index 4215535..981027f 100644
--- a/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/loops/expected.js
+++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/loops/expected.js
@@ -1,4 +1,3 @@
-// while
 while (true) {
   bar();
 }