0

我有一个缩小的 javascript 文件。我可以通过各种工具发送它来插入换行符和缩进。然后我想要的是修复变量名。我知道没有工具可以自动执行此操作。我需要的是一个工具,可以增加我手动执行此操作的尝试。它需要了解范围规则,以便当我重命名ctolengthdtoheightj()to 时,将在使用相同and和的move()任何地方进行这些更改,但不会在存在具有相同名称的不同变量和函数的其他范围内进行。 cdj

有没有这样的工具,专门用于反转缩小?如果没有针对此特定工作的工具,是否有一个智能 IDE 至少可以按照范围规则处理重命名变量或方法?

4

1 回答 1

1

我发现了一个浏览器内/可下载的 node.js 库工具,它可以很好地重命名重构。Esprima处理以下 ES6 代码(对示例稍作修改),这样当我更改任何全局范围的名称时hi,只会hi更改由注释块包围的名称(我想不出更好的方法来调用代码,因为降价没有显示在代码块中,抱歉)。

// 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.

只要没有代码错误,它似乎就遵守范围规则(例如,在同名let声明之上的声明将被视为在范围内,但这不是问题,因为它是一种语法错误)。varlet

于 2017-03-08T05:11:57.997 回答