我有一个缩小的javascript文件。我可以通过各种工具发送它来插入换行符和缩进。然后我想要的是修复变量名。我知道没有任何工具能自动做到这一点。我需要的是一个工具,将加强我的尝试这样做手动。它需要知道作用域规则,以便当我将c重命名为length,将d重命名为height,将j()重命名为move()时,所有地方都会进行这些更改,即使用相同的 c和d和j,但在存在相同名称的不同变量和函数的其他作用域中则不使用。
是否有这样的工具,专门为逆转小型化而设计的?如果没有用于此特定作业的工具,那么是否有智能IDE至少可以处理按照范围规则重命名变量或方法?
发布于 2017-03-08 05:11:57
我找到了一个浏览器内/可下载的node.js库工具,它可以很好地重命名重构。埃斯皮尔马处理以下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的同名var声明之上的var声明将在范围内考虑,但这不是问题,因为这是语法错误)。
https://stackoverflow.com/questions/42660523
复制相似问题