Repository: babel/minify (js)

Issue:
when WhileStatement replaced by ForStatement, the Identifier in ForStatement.test is conflict with mangled vars
babel-core: 6.26.0
babel-preset-minify: 0.4.0
default config

#### Input Code

here is the code fragment from D3.js
```js
function d3_svg_line(projection) {
  var x = d3_geom_pointX, y = d3_geom_pointY, defined = d3_true, interpolate = d3_svg_lineLinear, interpolateKey = interpolate.key, tension = .7;
  function line(data) {
    var segments = [], points = [], i = -1, n = data.length, d, fx = d3_functor(x);
    function segment() {
      segments.push("M", interpolate(projection(points), tension));
    }
    while (++i < n) {
      if (defined.call(this, d = data[i], i)) {
        points.push([ +fx.call(this, d, i)]);
      } else if (points.length) {
        segment();
        points = [];
      }
    }
    if (points.length) segment();
    return segments.length ? segments.join("") : null;
  }
  return line;
}
```

#### Actual Output

for human reading， i'v beautify the minified code and add comment it

```js
function a(a) {
  var b = d3_geom_pointX,
    c = d3_geom_pointY,
    e = d3_true,
    d = d3_svg_lineLinear,
    f = d.key;
  return function(c) {
    function f() {
      /** BUG: the Identifier d is defined at the top function scope **/
      h.push("M", d(a(j), .7))
    }
   /**
     * BUG:  but the Identifier d is re-defined at the ForStatment.test,  conflict
     */
    for (var g, h = [], j = [], d = -1, i = c.length, k = d3_functor(b); ++d < i;) e.call(this, g = c[d], d) ? j.push([+k.call(this, g, d)]) : j.length && (f(), j = []);
    return j.length && f(), h.length ? h.join("") : null
  }
}
```
#### Details

it seems #713 miss this case.

when rollback to babel-preset-minify:0.2.0 or 0.3.0,  everything goes fine.

when i disable mangle, the output seems work fine

does it possible to force mangle after simplify?






Pull request description:
fix: add constant violations to references in mangler collect phase
fix #822 

~~Obviously, this doesn't seem to work and introduces more regression. The Circle CI tests fail. Working on an update.~~

Update: fixed

Patch:
diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js
index 49d908a..1b20fc8 100644
--- a/packages/babel-plugin-minify-mangle-names/src/index.js
+++ b/packages/babel-plugin-minify-mangle-names/src/index.js
@@ -131,6 +131,11 @@ module.exports = babel => {
           // Collect bindings defined in the scope
           Object.keys(scope.bindings).forEach(name => {
             scopeTracker.addBinding(scope.bindings[name]);
+
+            // add all constant violations as references
+            scope.bindings[name].constantViolations.forEach(() => {
+              scopeTracker.addReference(scope, scope.bindings[name], name);
+            });
           });
         },
 


Test patch:
diff --git a/packages/babel-plugin-minify-mangle-names/__tests__/fixtures/issue-822/actual.js b/packages/babel-plugin-minify-mangle-names/__tests__/fixtures/issue-822/actual.js
new file mode 100644
index 0000000..c50da61
--- /dev/null
+++ b/packages/babel-plugin-minify-mangle-names/__tests__/fixtures/issue-822/actual.js
@@ -0,0 +1,32 @@
+function d3_svg_line(projection) {
+  var x = d3_geom_pointX,
+    y = d3_geom_pointY,
+    defined = d3_true,
+    interpolate = d3_svg_lineLinear,
+    interpolateKey = interpolate.key,
+    tension = 0.7;
+
+  function line(data) {
+    var segments = [],
+      points = [],
+      i = -1,
+      n = data.length,
+      d,
+      fx = d3_functor(x);
+    function segment() {
+      segments.push("M", interpolate(projection(points), tension));
+    }
+    while (++i < n) {
+      if (defined.call(this, (d = data[i]), i)) {
+        points.push([+fx.call(this, d, i)]);
+      } else if (points.length) {
+        segment();
+        points = [];
+      }
+    }
+    if (points.length) segment();
+    return segments.length ? segments.join("") : null;
+  }
+
+  return line;
+}
diff --git a/packages/babel-plugin-minify-mangle-names/__tests__/fixtures/issue-822/expected.js b/packages/babel-plugin-minify-mangle-names/__tests__/fixtures/issue-822/expected.js
new file mode 100644
index 0000000..d47113d
--- /dev/null
+++ b/packages/babel-plugin-minify-mangle-names/__tests__/fixtures/issue-822/expected.js
@@ -0,0 +1,35 @@
+function d3_svg_line(a) {
+  var c = d3_geom_pointX,
+      d = d3_geom_pointY,
+      e = d3_true,
+      f = d3_svg_lineLinear,
+      g = f.key,
+      h = 0.7;
+
+  function b(b) {
+    var j = [],
+        k = [],
+        l = -1,
+        m = b.length,
+        n,
+        o = d3_functor(c);
+
+    function g() {
+      j.push("M", f(a(k), h));
+    }
+
+    while (++l < m) {
+      if (e.call(this, n = b[l], l)) {
+        k.push([+o.call(this, n, l)]);
+      } else if (k.length) {
+        g();
+        k = [];
+      }
+    }
+
+    if (k.length) g();
+    return j.length ? j.join("") : null;
+  }
+
+  return b;
+}
\ No newline at end of file