Unminified variable name used unexpectedly
See original GitHub issueminify
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:
- Created 5 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This comment relates to previous issues with
let
andfor
loops: https://github.com/babel/minify/issues/485#issuecomment-291024057Reduced testcase Doesn’t trigger with
var
instead oflet
, and doesn’t trigger without env or simplify.Actual
Expected