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

Mistakes when mangle top-level names #3301

Closed
gdh1995 opened this issue Nov 27, 2018 · 4 comments · Fixed by #3338
Closed

Mistakes when mangle top-level names #3301

gdh1995 opened this issue Nov 27, 2018 · 4 comments · Fixed by #3338

Comments

@gdh1995
Copy link

gdh1995 commented Nov 27, 2018

Bug report

Uglify version (uglifyjs -V)

uglify-js@3.4.9

JavaScript input

"use strict";
// test.js
var uglify = require('uglify-js');
var options = {
  nameCache: { vars: {}, props: {} },
  mangle: {
    toplevel: true
  }
};

var dist1 = uglify.minify({
  "1.js": "var extend = function() {}; function A() {};"
}, options);

var dist2 = uglify.minify({
  "2.js": "var B = (function (super) { function B() {}; extend(B, super); return B; })(A);"
}, options);

console.log(dist1);
console.log(dist2);

The uglifyjs CLI command executed or minify() options used.

$ npm install uglify-js
$ node test.js

JavaScript output

{ code: 'var n=function(){};function c(){}' }
{ code: 'var t=function(n){function t(){}return n(t,n),t}(c);' }

What is the mistake

  • The global variable extend is renamed to n
  • the name super in the second input is also renamed to n
  • uglifyJS replaces extend in the second input with n, which collides with super
  • if runs the test output, the real n(t, n) will call super(B, super)
    • then for more complicated code, it will throw.
@kzc
Copy link
Contributor

kzc commented Nov 28, 2018

Have you tried terser to see whether it exhibits the same toplevel name cache collision?

@gdh1995
Copy link
Author

gdh1995 commented Nov 29, 2018

Have you tried terser to see whether it exhibits the same toplevel name cache collision?

After renaming super into super1, the terser works well:

{ code: 'var n=function(){};function c(){}' }
{ code: 'var t=function(t){function e(){}return n(e,t),e}(c);' }

I also tested complex 2.js and terser did skip the global name n when renaming local variables.

@kzc
Copy link
Contributor

kzc commented Nov 29, 2018

FWIW, this mangle regression occurred in f40f5eb.
This commit was not merged to Terser.

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 14, 2019
alexlamsl added a commit that referenced this issue Mar 14, 2019
@gdh1995
Copy link
Author

gdh1995 commented Mar 14, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants