Tool to guide manual de-minification of variables and functions?

132 Views Asked by At

I have a minified javascript file. I can send it through a variety of tools to insert newlines and indentation. What I then want is to fix the variable names. I know that no tool can do this automatically. What I need is a tool that will augment my attempt to do so manually. It needs to be aware of scope rules so that when I rename c to length and d to height and j() to move(), those changes will be made everywhere that the same c and d and j are used, but not in other scopes where different variables and functions with those same names exist.

Is there a tool like this, specifically designed for reversing minification? If there isn't a tool for this specific job, is there a smart IDE that can at least handle renaming variables or methods following scope rules?

1

There are 1 best solutions below

0
On

I found an in-browser/downloadable node.js library tool that does rename refactoring very well. Esprima handles the following ES6 code (slightly modified from the example) such that when I change the name of any of the global scope hi, only the hi names surrounded by a comment block are changed (I couldn't think of a better way to call out code since markdown doesn't show in code blocks, sorry).

// Array shuffling code from Underscore.js, code modified from base example on http://esprima.org/demo/rename.html
var shuffled;

var /*hi*/ = 'hi'; // initial declaration

function /*hi*/() { // modifies var above
    this.shuffle = 'x';
}

_.shuffle = function(obj) {
    function hi() {
        return 'hello';
    }
    var shuffled = [], rand;
    each(obj, function(value, index, list) {
        rand = Math.floor(Math.random() * (index + 1));
        shuffled[index] = shuffled[rand];
        shuffled[rand] = value;
    });
    hi(); // calls function defined above, name not changed by change to global scope 'hi'
    console.log(hello); // considered to be the same as the let statement below even though this is a syntax error since let doesn't get hoisted like var
    return shuffled;
};

let hello = 'hello';

function hiNotInScope() {
    var hi = 'something else'; // not in global scope, so change to global hi doesn't change this
    console.log(hi); // changed if the hi in this scope is changed
}

// hi (not changed since it's in a comment)

/*hi*/(); // calls global hi function.

It seems to respect scoping rules as long as there are no code errors (for example, a let declaration above a var declaration of the same name that gets hoisted above the let will be considered in-scope, but this is a non-issue since it's a syntax error).