Repository: babel/minify (js)

Issue:
Duplicated names in var statements are considered as different variables
### 1. Minimal code to reproduce the bug

#### Input Code
```js
function f() {
  var a = 1;
  var b = a;
  var a = 2;
  console.log(a, b);
}
f();
```
or
```js
function f() {
  var a = 1, b = a, a = 2;
  console.log(a, b);
}
f();
```
[Live example on babeljs.io](http://babeljs.io/repl/#?babili=true&browsers=&build=&builtIns=false&code_lz=GYVwdgxgLglg9mABMAFASkQbwFCMQNwEMAnRQxAXkQEYBuXAkxAI0rPr3KoCYPEIEAZzgAbAKYA6EXADmKQgBoWaegF9sqFUA&debug=false&circleciRepo=&evaluate=false&lineWrap=false&presets=react%2Cstage-2%2Cbabili&prettier=false&targets=&version=6.26.0)

#### Actual Output
```js
function f(){var c=1,c=2;console.log(c,c)}f();
```

#### Expected Output

```js
function f(){var c=1,d=c;c=2,console.log(c,d)}f();
```

#### Details
Note the incorrect constant propagation in the `console.log` statement: it has `console.log(c,c)`, even though the two arguments in the original are different. This happens because the variable `a` is declared multiple times as `var`, yet is not recognized as the same variable.

In the correct interpretation `a=2` is recognized as a modification to the same variable, so constant propagation is not allowed to happen. Changing the second `var a` to simply `a` makes the problem disappear.

Slightly more complex example:

```js
function loop() {
  var end = 0;
  var start = end;
  while (end < 10) {
    console.log(start, end);
    var end = end + 1;
  }
}
```

### 2. Stack Trace if there is an Error thrown during minification

None.

### 3. Configuration - babel-minify configuration or babelrc

Default.

The problem is with the `deadcode` option (`babel-plugin-minify-dead-code-elimination`), which is switched on by default.

### 4. Whether you used it with other presets/plugins - like es2015 or env

None.

### 5. Environment: gulp, rollup, webpack, babel-cli, etc...?

All.

Pull request description:
fix(dce): bail on non-constant replacement path
In one use variable replacement, if the right hand side contains
constant violations, the left-hand side is supposed to be preserved, so
we detect this and bail out for this case

+ Fix #685

Patch:
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
index 4ead0e1..026fc05 100644
--- a/CODE_OF_CONDUCT.md
+++ b/CODE_OF_CONDUCT.md
@@ -1,13 +1,78 @@
-# Contributor Code of Conduct
+# Contributor Covenant Code of Conduct
 
-As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
+## Our Pledge
 
-We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
+In the interest of fostering an open and welcoming environment, we as
+contributors and maintainers pledge to making participation in our project and
+our community a harassment-free experience for everyone, regardless of age, body
+size, disability, ethnicity, gender identity and expression, level of experience,
+nationality, personal appearance, race, religion, or sexual identity and
+orientation.
 
-Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
+## Our Standards
 
-Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
+Examples of behavior that contributes to creating a positive environment
+include:
 
-Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
+* Using welcoming and inclusive language
+* Being respectful of differing viewpoints and experiences
+* Gracefully accepting constructive criticism
+* Focusing on what is best for the community
+* Showing empathy towards other community members
 
-This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
+Examples of unacceptable behavior by participants include:
+
+* The use of sexualized language or imagery and unwelcome sexual attention or
+  advances
+* Trolling, insulting/derogatory comments, and personal or political attacks
+* Public or private harassment
+* Publishing others' private information, such as a physical or electronic
+  address, without explicit permission
+* Other conduct which could reasonably be considered inappropriate in a
+  professional setting
+
+## Our Responsibilities
+
+Project maintainers are responsible for clarifying the standards of acceptable
+behavior and are expected to take appropriate and fair corrective action in
+response to any instances of unacceptable behavior.
+
+Project maintainers have the right and responsibility to remove, edit, or
+reject comments, commits, code, wiki edits, issues, and other contributions
+that are not aligned to this Code of Conduct, or to ban temporarily or
+permanently any contributor for other behaviors that they deem inappropriate,
+threatening, offensive, or harmful.
+
+## Scope
+
+This Code of Conduct applies both within project spaces and in public spaces
+when an individual is representing the project or its community. Examples of
+representing a project or community include using an official project e-mail
+address, posting via an official social media account, or acting as an appointed
+representative at an online or offline event. Representation of a project may be
+further defined and clarified by project maintainers.
+
+## Enforcement
+
+Instances of abusive, harassing, or otherwise unacceptable behavior may be
+reported by contacting the following people in our
+[Slack](https://babeljs.slack.com/)([request access](https://slack.babeljs.io/))  -
+[boopathi](https://babeljs.slack.com/messages/boopathi),
+[vigneshh](https://babeljs.slack.com/messages/vigneshh),
+[hzoo](https://babeljs.slack.com/messages/hzoo) or
+[loganfsmyth](https://babeljs.slack.com/messages/loganfsmyth). All complaints
+will be reviewed and investigated and will result in a response that is deemed
+necessary and appropriate to the circumstances. The project team is obligated to
+maintain confidentiality with regard to the reporter of an incident. Further
+details of specific enforcement policies may be posted separately.
+
+Project maintainers who do not follow or enforce the Code of Conduct in good
+faith may face temporary or permanent repercussions as determined by other
+members of the project's leadership.
+
+## Attribution
+
+This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
+available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
+
+[homepage]: https://www.contributor-covenant.org
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 037fcce..448fa7d 100644
--- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js
+++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js
@@ -349,9 +349,16 @@ module.exports = ({ types: t, traverse }) => {
               let bail = false;
 
               if (replacementPath.isIdentifier()) {
-                bail =
-                  refPath.scope.getBinding(replacement.name) !==
-                  scope.getBinding(replacement.name);
+                const binding = scope.getBinding(replacement.name);
+                // the reference should be in the same scope
+                // and the replacement should be a constant - this is to
+                // ensure that the duplication of replacement is not affected
+                // https://github.com/babel/minify/issues/685
+                bail = !(
+                  binding &&
+                  refPath.scope.getBinding(replacement.name) === binding &&
+                  binding.constantViolations.length === 0
+                );
               } else {
                 replacementPath.traverse({
                   Function(path) {
@@ -362,9 +369,13 @@ module.exports = ({ types: t, traverse }) => {
                     if (bail) {
                       return;
                     }
-                    bail =
-                      refPath.scope.getBinding(node.name) !==
-                      scope.getBinding(node.name);
+                    const binding = scope.getBinding(node.name);
+                    if (
+                      binding &&
+                      refPath.scope.getBinding(node.name) === binding
+                    ) {
+                      bail = binding.constantViolations.length > 0;
+                    }
                   }
                 });
               }


Test patch:
diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-685/actual.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-685/actual.js
new file mode 100644
index 0000000..c39141a
--- /dev/null
+++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-685/actual.js
@@ -0,0 +1,16 @@
+function loop() {
+  var end = 0;
+  var start = end;
+  while (end < 10) {
+    console.log(start, end);
+    var end = end + 1;
+  }
+}
+loop();
+
+function bar() {
+  var x = 1;
+  var y = x + 2;
+  var x = 3;
+  return x + y;
+}
diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-685/expected.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-685/expected.js
new file mode 100644
index 0000000..62b3623
--- /dev/null
+++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/fixtures/issue-685/expected.js
@@ -0,0 +1,16 @@
+function loop() {
+  var end = 0;
+  var start = end;
+  while (end < 10) {
+    console.log(start, end);
+    var end = end + 1;
+  }
+}
+loop();
+
+function bar() {
+  var x = 1;
+  var y = x + 2;
+  var x = 3;
+  return x + y;
+}
\ No newline at end of file