2017-03-07 38 views
0

我有一個縮小的JavaScript文件。我可以通過各種工具發送它來插入換行符和縮進。然後我想要修復變量名稱。我知道沒有工具可以自動執行此操作。我需要的是一個能夠增強我手動嘗試的工具。它需要這樣,當我在其他範圍內,其中重命名clengthdheightj()move(),這些變化將無處不提出,用於相同cdj,但不知道的範圍規則存在具有相同名稱的不同變量和函數。指導手動減小變量和函數的工具?

是否有像這樣的工具,專門用於扭轉縮小?如果這個特定的工作沒有工具,那麼是否有一個智能的IDE能夠至少處理遵循範圍規則的重命名變量或方法?

回答

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聲明高於var聲明獲取let上方懸掛的同名將在-考慮範圍之內,但這是一個非問題,因爲它是一個語法錯誤)。