Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated names in var statements are considered as different variables #685

Closed
RubenVerborgh opened this issue Sep 8, 2017 · 0 comments
Labels
bug Confirmed bug has PR Has an open PR that fixes this issue
Milestone

Comments

@RubenVerborgh
Copy link

RubenVerborgh commented Sep 8, 2017

1. Minimal code to reproduce the bug

Input Code

function f() {
  var a = 1;
  var b = a;
  var a = 2;
  console.log(a, b);
}
f();

or

function f() {
  var a = 1, b = a, a = 2;
  console.log(a, b);
}
f();

Live example on babeljs.io

Actual Output

function f(){var c=1,c=2;console.log(c,c)}f();

Expected Output

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:

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.

RubenVerborgh added a commit to linkeddata/rdflib.js that referenced this issue Sep 8, 2017
A bug in the minifier breaks the N3 parser
(babel/minify#685).
RubenVerborgh added a commit to SolidOS/mashlib that referenced this issue Sep 8, 2017
A bug in the minifier breaks rdflib's N3 parser
(babel/minify#685).
@vigneshshanmugam vigneshshanmugam added babel This is an issue in the upstream project - Babel has PR Has an open PR that fixes this issue labels Sep 9, 2017
@boopathi boopathi added bug Confirmed bug and removed has PR Has an open PR that fixes this issue labels Oct 25, 2017
@boopathi boopathi added this to the 1.0 milestone Nov 18, 2017
dduran1967 pushed a commit to Yodata/rdflib.js that referenced this issue Dec 9, 2017
A bug in the minifier breaks the N3 parser
(babel/minify#685).
boopathi added a commit that referenced this issue Dec 11, 2017
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
@boopathi boopathi added has PR Has an open PR that fixes this issue and removed babel This is an issue in the upstream project - Babel labels Dec 11, 2017
boopathi added a commit that referenced this issue Dec 11, 2017
* 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

* Fix binding check and checks for not Identifier

* Add testcase for non-identifier replacementPaths
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug has PR Has an open PR that fixes this issue
Projects
None yet
Development

No branches or pull requests

3 participants