Skip to content
New issue

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

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

Already on GitHub? # to your account

[duplicate] 'let' causing variables to disappear in for-loop #485

Closed
michaelathatch opened this issue Mar 31, 2017 · 6 comments
Closed

[duplicate] 'let' causing variables to disappear in for-loop #485

michaelathatch opened this issue Mar 31, 2017 · 6 comments
Labels
bug Confirmed bug with es2015 The bug is caused when used with es2015-block-scoping plugin

Comments

@michaelathatch
Copy link

This only occurs in conjunction with the es2015 plugin.
The following code

function getSum(data) {
  let total = 0;
  for (let i = 0; i < data.length; i++) {
    total += data[i];
  }
  return total;
}

gets transpiled to

"use strict";function getSum(a){for(var c=0;c<a.length;c++)a[c];return b}

which returns the undefined variable b. Seemingly total has been removed.

The same code using var i instead of let i produces the expected result

"use strict";function getSum(a){for(var c=0,b=0;b<a.length;b++)c+=a[b];return c}
@hzoo
Copy link
Member

hzoo commented Mar 31, 2017

Hey @michaelmcleodnz! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@boopathi boopathi added bug Confirmed bug with es2015 The bug is caused when used with es2015-block-scoping plugin labels Mar 31, 2017
@michaelathatch
Copy link
Author

If it helps, this seems to be because vars declared before a for-loop are pulled inside the for-loop declaration, which can't be done with lets because it changes their scope.
Babili knows not to pull the lets inside a for-loop without the es2015 plugin, so I'm not sure what the order of operations would be here that causes it to pull the variable inside the for-loop and then decide that it can be optimised away because it is let-scoped.

@michaelathatch
Copy link
Author

I've found that this issue has already been reported in #430 and #412.
Line 89 of merge-sibling-variables checks that the for-loop declaration is of type var, but presumably somewhere in minify-dead-code-elimination it is still believed to be of type let.

@michaelathatch michaelathatch changed the title 'let' causing variables to disappear in for-loop [duplicate] 'let' causing variables to disappear in for-loop Apr 2, 2017
boopathi pushed a commit that referenced this issue Nov 12, 2017
…) (#713)

* Fix(merge-sibling-var): force recalc ref when concat for-loop var (#485)

Fix #594
Fix #430
Fix #412

* Add test cases of es2015 scoping issue (#713)

* Use babel path API instead of mutating node
@boopathi
Copy link
Member

Fixed in #713

@armujahid
Copy link

armujahid commented Jan 7, 2019

This issue is occurring again on latest version of babel-preset-minify v0.5.0. For and while loops are eating cont/let declarations immediately before these.

Demo of issue

Should I open new issue or should this issue be reopened?

Versions:
@babel/core: 7.2.2
@babel/preset-env: 7.2.3
babel-preset-minify: 0.5.0
gulp-babel: 8.0.0

@armujahid
Copy link

armujahid commented Jan 7, 2019

As a workaround, I am now using below minify config options and everything appears to be working.

// Below configs are copied from https://github.com/Polymer/tools/blob/master/packages/build/src/js-transform.ts
// Disable the minify-constant-folding plugin because it has a bug relating
// to invalid substitution of constant values into export specifiers:
// https://github.com/babel/minify/issues/820
evaluate: false,

// TODO(aomarks) Find out why we disabled this plugin.
simplifyComparisons: false,

// Prevent removal of things that babel thinks are unreachable, but sometimes
// gets wrong: https://github.com/Polymer/tools/issues/724
deadcode: false,

// Disable the simplify plugin because it can eat some statements preceeding
// loops. https://github.com/babel/minify/issues/824
simplify: false,

// This is breaking ES6 output. https://github.com/Polymer/tools/issues/261
mangle: false,

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Confirmed bug with es2015 The bug is caused when used with es2015-block-scoping plugin
Projects
None yet
Development

No branches or pull requests

4 participants