非死book JS 工具包:jscodeshift

xg48 9年前

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.open-open.com/lib/view/home/1428676580468