question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unminified variable name used unexpectedly

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
davidbonnetcommented, Jun 24, 2018

This comment relates to previous issues with let and for loops: https://github.com/babel/minify/issues/485#issuecomment-291024057

1reaction
Cypcommented, 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}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Are JavaScript variable names changed to random letters ...
The answer to your question in most cases of minification is: Yes, most minifiers will randomly rename variable/function/etc. names, starting ...
Read more >
SyntaxError: missing variable name - JavaScript | MDN
The JavaScript exception "missing variable name" is a common error. It is usually caused by omitting a variable name or a typographic error....
Read more >
unminifies JS with unique words for variable names - Reddit
Having a tool that creates unique var names with scoping taken into account seems somewhat more useful as you'd be able to properly...
Read more >
SyntaxError: missing variable name - JavaScript
Or maybe a comma is wrong. Check for typos! Message. SyntaxError: missing variable name (Firefox) SyntaxError: Unexpected token = (Chrome). Error type.
Read more >
Understanding Spam Scripts | [H]ard|Forum
... call removing all the whitespace and shortening all the variable names. What you need to do is use a script to "unminify"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found