我发现了一个浏览器内/可下载的 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
声明之上的声明将被视为在范围内,但这不是问题,因为它是一种语法错误)。var
let