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

Unminified variable name used unexpectedly #866

Open
Juribiyan opened this issue May 23, 2018 · 6 comments
Open

Unminified variable name used unexpectedly #866

Juribiyan opened this issue May 23, 2018 · 6 comments
Labels
bug Confirmed bug

Comments

@Juribiyan
Copy link

Juribiyan commented May 23, 2018

minify unless you turn mangle off replaces the original variable names with one-letter names. But under some curcumstances it forgets to use the minified variable name and uses the unmodified variable name instead which leads to a ReferenceError since it's not defined.

To Reproduce

See configuration below. This does not occur in REPL.

  • Source code
String.prototype.toBlob = function() {
  try {
    let arr = this.split(',')
    , mime = arr[0].match(/:(.*?);/)[1]
    , bstr = atob(arr[1])
    , n = bstr.length
    , u8arr = new Uint8Array(n);
    while(n--){
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], {type:mime});
  }
  catch (e) {
    console.error(e)
    return false
  }
}

Actual Output

String.prototype.toBlob = function() {
  try {
    for (var a = this.split(','), b = a[0].match(/:(.*?);/)[1], c = atob(a[1]), d = c.length, e = new Uint8Array(d); d--;) 
      e[d] = c.charCodeAt(d);
    return new Blob([u8arr], { type: mime }) 
  } 
  catch (a) {
    return console.error(a), !1 } 
}

This obviously produces a ReferenceError: u8arr is not defined.

Expected Output

Here's an actual ouput produced long time ago by "babel-cli": "6.18.0", "babel-preset-latest": "6.16.0" and "babel-preset-babili": "0.0.9"

String.prototype.toBlob = function() {
  try {
    for (var a = this.split(','), b = a[0].match(/:(.*?);/)[1], d = atob(a[1]), g = d.length, h = new Uint8Array(g); g--;) 
      h[g] = d.charCodeAt(g);
    return new Blob([h], {
      type: b
    })
  } catch (a) {
    return console.error(a), !1
  }
};

Configuration

  • package.json (excerpt):
{
  "scripts": {
    "build": "cross-env BABEL_ENV=production babel src --out-dir build --source-maps"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-minify": "^0.4.3",
    "cross-env": "^5.1.6"
  }
}
  • .babelrc:
{
  "presets": [["env", {"modules": false, "comments": false}]],
  "env": {
    "production": {
      "presets": ["minify"]
    }
  }
}

To build, run npm run build.

@Juribiyan
Copy link
Author

Updating to "babel-cli": "^7.0.0-beta.3" and "babel-preset-env": "^2.0.0-alpha.20" does not seem to help.

@vigneshshanmugam vigneshshanmugam added the bug Confirmed bug label May 24, 2018
@Cyp
Copy link

Cyp commented May 25, 2018

Reduced testcase
Doesn't trigger with var instead of let, and doesn't trigger without env or simplify.

function baz() {
    let foo = 42;
    while (bar) {
    }
    return foo;
}

Actual

"use strict";function baz(){for(;bar;);return foo}

Expected

"use strict";function baz(){for(;bar;);return 42}

@nyk0r
Copy link

nyk0r commented Jun 6, 2018

Got a similar issue with the while loop in a method.

Source

_findContainer () {
    let view = this.view;
    while (view) {
        if (view.model && view.model.isSettingsContainer) {
            break;
        }
        view = view.parent;
    }
    return view;
}

Result

_findContainer: function _findContainer() {
    for (var a = this.view; a && !(a.model && a.model.isSettingsContainer); )
        a = a.parent;
    return view
}

Expected

_findContainer: function _findContainer() {
    for (var a = this.view; a && !(a.model && a.model.isSettingsContainer); )
        a = a.parent;
    return a
}

Environment

"@babel/core": "^7.0.0-beta.49",
"@babel/preset-env": "^7.0.0-beta.49",
"babel-preset-minify": "^0.4.3",

@pmdartus
Copy link
Contributor

The issue appears to be fixed on the latest master.

@Cyp
Copy link

Cyp commented Jun 12, 2018

I can still reproduce the problem with my reduced testcase with latest master of babel and babel/minify, although the non-reduced testcases seem to be correct, now.

@davidbonnet
Copy link

This comment relates to previous issues with let and for loops: #485 (comment)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

6 participants