代码
代码重写 JavaScript 和 TypeScript 使用 Bail 插件.
安装
从 无数字 :
$ npm install @codemod/cli
这将在本地安装跑道作为 codemod。此软件包需要节点
v16级以上. 这个读取器假设你已经安装在本地, 但你也可以
全球安装 npm install -g @codemod/cli。 。 。 .
使用量
主界面作为命令行工具,通常运行类似:
$ npx codemod --plugin transform-module-name \
path/to/file.js \
another/file.js \
a/directory
这将重写文件 path/to/file.js, (中文(简体) ), another/file.js,以及发现于 a/directory 用 bail 插件转换它们 transform-module-name。可以指定多个插件,可以同时重写多个文件或目录.
注意TypeScript支持由bayl提供,因此可能不完全支持所有有效的TypeScript代码. 如果你遇到问题,请考虑在 标有标签的贝尔问题 area: typescript 在提交问题之前.
插件也可以从远程 URL 中装入, 包括保存 AST 浏览器 URL, 使用 --remote-plugin。该特性只应用作加载您或您信任的人所写的代码的方便。 它会随您的全部用户权限运行, 所以请保持谨慎!
$ npx codemod --remote-plugin URL …
默认, codemod 通过使用 重铸 来解析和打印您的代码,保留原始注释和格式。 如果需要, 您可以使用 高级 或ESLint 或者你喜欢的其他工具.
对于更详细的选项,运行 npx codemod --help。 。 。 .
写入插件
还有 许多, 许多已存在的插件 你可以用来。 但是,如果你需要自己写,你应该咨询 贝贝尔手册。如果您发布一个专门用作代码的插件,请考虑同时使用 babel-plugin 财务报告和审定财务报表 babel-codemod 关键词.
@codemod/cli 提供了几个帮助,使写作编码插件更容易. 例如:
/**
* This codemod rewrites `A * A` to `A ** 2` for any expression `A`.
*/
import { defineCodemod } from '@codemod/cli'
// `m` is `@codemod/matchers`, a library of useful matchers
// `t` is `@babel/types`, babel AST type predicates and builders
export default defineCodemod(({ t, m }) => {
// `operand` is a capture matcher that will be filled in by `multiplyBySelf`,
// which is a binary expression matcher that matches any expression multiplied
// by itself.
const operand = m.capture(m.anyExpression())
const multiplyBySelf = m.binaryExpression(
'*',
operand,
m.fromCapture(operand)
)
return {
visitor: {
BinaryExpression(path) {
m.match(multiplyBySelf, { operand }, path.node, ({ operand }) => {
path.replaceWith(
t.binaryExpression('**', operand, t.numericLiteral(2))
)
})
},
},
}
})
使用 Babel 插件传送
codemod 支持由标准 Babel 或 TypeScript 构建工具链所支持的解析语言特性,类似于一个 Create React App 构建管道可以处理的内容. 使用这些语言特性来写插件.
向插件传递选项
您可以将 JSON 对象作为选项传递到插件 :
# Pass a JSON object literal
$ npx codemod --plugin ./my-plugin.ts --plugin-options '{"opt": true}'
# Pass a JSON object from a file
$ npx codemod --plugin ./my-plugin.ts --plugin-options @opts.json
捐款
见 捐款额 (c) 提供关于设立发展项目和为项目作贡献的信息.
许可证
版权:2017 Brian Donovan
根据 Apache 许可证, 版本 2. 0 (“ License ”) 获得许可; 您不能使用此文件, 除非符合许可证 。 您可在下列地点获得许可证副本:
http://www.apache.org/licenses/LICENSE-2.0
除非适用的法律要求或书面同意,根据许可证分发的软件在 " AS IS " BASIS上分发,没有任何明示或默示的WARANTIES或CONDITION。 见《许可证》关于许可和许可证限制的具体语言.