我發現了一個瀏覽器內/下載的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
上方懸掛的同名將在-考慮範圍之內,但這是一個非問題,因爲它是一個語法錯誤)。