Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

$ being mangled even when reserved #242

Closed
BradleyStaples opened this Issue Jul 17, 2013 · 3 comments

Comments

Projects
None yet
3 participants

I'm using a jQuery plugin for tagging (https://github.com/daniel-zahariev/jquery-textntags/blob/master/jquery-textntags.js) and uglify is still overriding the $ as a variable even when told not to with the -r flag. Noticed from a build script, but manually running the following command duplicates the issue for me.

uglifyjs jquery-textntags.js -c "warnings=false" -d IS_SERVER=false -m -r '$,require,exports'

Specifically, the editorSelectionLength variable is the one this is happening to (currently line 87). I noticed the issue of this same variable being reassigned within a switch statement around line 286 as well.

Contributor

colorhook commented Dec 13, 2013

+1

(function($){
     var a = $("a");a.a;
     var aa = $("a");aa.a;
 })($, $$);

minified to:

!function($){var a=$("a");a.a;var $=$("a");$.$}($,$$);

@colorhook colorhook added a commit to colorhook/UglifyJS2 that referenced this issue Dec 16, 2013

@colorhook colorhook bugfix #242 fa3300f
Contributor

colorhook commented Dec 16, 2013

I have found a hack method:

Before mangling the source code, find all the custom reserved keywork defined by except option, and set the mangled_name to itself.

var except = ['$', 'exports', 'define', 'require'];

var toplevel = UglifyJS.parse(filecontent, {filename: "?", toplevel: null});

//mangle
var $next_mangled = UglifyJS.AST_Scope.prototype.next_mangled;
UglifyJS.AST_Scope.prototype.next_mangled = function(options){
   var m = $next_mangled.call(this, options);
   while(except.indexOf(m) != -1){
      m = $next_mangled.call(this, options);
   }
   return m;
}

toplevel.figure_out_scope();
toplevel.compute_char_frequency();
toplevel.mangle_names({except: except});

Also created a pull request to fix it:

AST_Scope.DEFMETHOD("next_mangled", function(options){
    var ext = this.enclosed;
    out: while (true) {
        var m = base54(++this.cname);
        if (!is_identifier(m)) continue; // skip over "do"
        // we must ensure that the mangled name does not shadow a name
        // from some parent scope that is referenced in this or in
        // inner scopes.
        if(options.except.indexOf(m) !== -1){
            continue;
        }
        for (var i = ext.length; --i >= 0;) {
            var sym = ext[i];
            var name = sym.mangled_name || (sym.unmangleable(options) && sym.name);
            if (m == name) continue out;
        }
        console.log(m);
        return m;
    }
});

@mishoo mishoo added a commit that referenced this issue Dec 16, 2013

@mishoo mishoo Merge pull request #371 from colorhook/master
bugfix #242
b634018
Owner

mishoo commented Dec 16, 2013

Closed with PR #371

@mishoo mishoo closed this Dec 16, 2013

@rvanvelzen rvanvelzen added a commit to rvanvelzen/UglifyJS2 that referenced this issue Dec 23, 2013

@colorhook @rvanvelzen colorhook + rvanvelzen bugfix #242 01882d1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment