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

Var renamed but original name [incorrectly] used in code === 'not defined' error #825

Closed
frudman opened this issue Apr 29, 2018 · 2 comments · Fixed by #826
Closed

Var renamed but original name [incorrectly] used in code === 'not defined' error #825

frudman opened this issue Apr 29, 2018 · 2 comments · Fixed by #826
Labels
bug Confirmed bug has PR Has an open PR that fixes this issue

Comments

@frudman
Copy link

frudman commented Apr 29, 2018

When running code through minify (BABEL_ENV=prod npx babel --no-comments --out-file dst src), a local variable gets "reduced" (i.e. from navx to c) as expected but a later reference (within a string expression) uses the original name (i.e. navx) resulting in a 'not defined' error upon execution.

Input Code

(function() {

    const totItems = 71; // REQUIRED for error: if commented out, NO ERROR!!!

    var start = 1, navx = '';
    while (start < 71) {
        navx += 'a';
        start += 10;
    }

    const pages = 'b' + navx;

    console.log(pages);
})();

.babelrc Setup:

{
  "presets": ["env"],
  "env": {
    "prod": {
      "presets": ["minify"]
    }
  }
}

Minified code

Using BABEL_ENV=prod npx babel --out-file dst.js src.js to build it:

'use strict';(function(){for(var b=1,c='';71>b;)c+='a',b+=10;var a='search result pages: '+navx;console.log(a)})();

Actual Output

Using node dst to test run this code:

/home/freddy/enerx/dst.js:1
(function (exports, require, module, __filename, __dirname) { 'use strict';(function(){for(var b=1,c='';71>b;)c+='a',b+=10;var a='search result pages: '+navx;console.log(a)})();
                                                                                                                                                         ^

ReferenceError: navx is not defined
    at /home/freddy/enerx/dst.js:1:154
    at Object.<anonymous> (/home/freddy/enerx/dst.js:1:175)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
    at startup (internal/bootstrap/node.js:228:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3)

Expected Output

Using node src to run:

search result pages: baaaaaaa

Details

Something interesting (?): if I remove the unused totItems declaration (definition? :-)), the code builds and runs properly.

Is this a hoisting issue? (not a clue: just said that to sound smart! :-))

MORE IMPORTANTLY: if I remove the enclosing IIFE (i.e. (function(){})()), the code also runs properly.

@frudman
Copy link
Author

frudman commented Apr 30, 2018

more observations:

  • if I change the const totItems = 71; statement to var totItems = 71; everything works
  • if I change it to let totItems = 71; it does NOT work

(the value is not important, just a leftover from cutting down real code to minimally needed to reproduce problem)

@vigneshshanmugam vigneshshanmugam added the has PR Has an open PR that fixes this issue label Apr 30, 2018
@frudman
Copy link
Author

frudman commented Apr 30, 2018

looking for a workaround (for our real code), I noticed that the babel REPL is working properly on the same test code. It seems to use Babel 6.26.0, same as us, but Env 1.6.2 (we have babel-preset-env v1.6.1). Not sure which or which version of minify they use on that repl site.

Included a capture of test:
screen shot 2018-04-30 at 12 41 03 pm

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

Successfully merging a pull request may close this issue.

2 participants