非死book JS 工具包:jscodeshift

xg48 9年前發布 | 15K 次閱讀 JavaScript開發工具包 jscodeshift

jscodeshift 是一個用于在多 JS 文件運行 codemod 的工具包,它提供:

  • 一個 runner,它可在每個傳遞給它的文件之間提供轉換,還能輸出轉換文件的數量。

  • recast 的包裝,提供不同的 API。Recast 是一個 AST-to-AST 轉換工具,并且會盡可能地保護代碼的原有風格。

示例代碼:

// Adding a method to all Identifiers
jscodeshift.registerMethods({    
   logNames: function() {        
      return this.forEach(function(path) {            
        console.log(path.node.name);
        });
    }
}, jscodeshift.Identifier);
// Adding a method to all collectionsjscodeshift.registerMethods({    
      findIdentifiers: function() {        
         return this.find(jscodeshift.Identifier);
    }
});

jscodeshift(ast).findIdentifiers().logNames();
jscodeshift(ast).logNames(); // error, unless `ast` only consists of Identifier nodes

項目主頁:http://www.baiduhome.net/lib/view/home/1428676580468

 本文由用戶 xg48 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!