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

$ being mangled even when reserved #242

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

$ being mangled even when reserved #242

BradleyStaples opened this issue Jul 17, 2013 · 3 comments

Comments

@BradleyStaples
Copy link

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.

@colorhook
Copy link
Contributor

+1

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

minified to:

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

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

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 added a commit that referenced this issue Dec 16, 2013
@mishoo
Copy link
Owner

mishoo commented Dec 16, 2013

Closed with PR #371

@mishoo mishoo closed this as completed Dec 16, 2013
rvanvelzen pushed a commit to rvanvelzen/UglifyJS2 that referenced this issue Dec 23, 2013
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

No branches or pull requests

3 participants