terinjokes / gulp-uglify

Minify files with UglifyJS
MIT License
1.23k stars 154 forks source link

Wrong minified code produced with conditional variable declaration #350

Closed shaan1337 closed 5 years ago

shaan1337 commented 5 years ago

Version: Latest (3.0.1)

The following code:

return t?(e={},e.whatever='hello'):(e={},e.whatever='hello');

is minified to:

return e.whatever=(e={},"hello")};

which throws an e is undefined error (e is being used before initialization to {})

Reproduction steps:

  1. Download and unpack uglify-bug.zip
  2. yarn install (or npm install)
  3. gulp minify

Run original file, there are no errors: node file.js

Run minified file: node dist/file.js

Error is thrown:

/home/shaan/.../file.js:1
(function (exports, require, module, __filename, __dirname) { test=function(t){var e;return e.whatever=(e={},"hello")},test(!0);
                                                                                                      ^

TypeError: Cannot set property 'whatever' of undefined
    at test (/home/shaan/.../uglify-bug/dist/file.js:1:103)
    at Object.<anonymous> (/home/shaan/.../uglify-bug/dist/file.js:1:120)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
terinjokes commented 5 years ago

It looks like this bug is a regression from previous version of UglifyJS. It previously minified to code that works correctly:

test=function(t){return{whatever:"hello"}},test(!0);

As you note, it does not compress properly in the current version, 3.4.9. This can be seen by running it directly on the file you provided.

I recommend opening an issue on the UglifyJS project, so they can look at your issue.