@e18e/web-features-codemods (英语)
这个项目包含了一套用于转换代码的代码集以使用更新的网页特性和语法.
例如,从 array[array.length - 1] 改为 array.at(-1)。 。 。 .
安装
您可以使用 npm 或 纱线安装代码 :
npm install @e18e/web-features-codemods --save-dev
可用的代码
| 代码 | 说明 | 示例 |
|---|---|---|
arrayAt | 将基于长的数组索引转换为 at() 方法 | array[array.length - 1] → array.at(-1) |
arrayFill | 优化数组填充模式 | 各种填充模式优化 |
arrayIncludes | 转换 indexOf 检查到 includes() | array.indexOf(item) !== -1 → array.includes(item) |
arrayToReversed | 将复制和反向模式转换为 toReversed() | array.slice().reverse() → array.toReversed() |
arrayToSorted | 将复制和类型模式转换为 toSorted() | array.slice().sort() → array.toSorted() |
arrayToSpliced | 将复制和切换模式转换为 toSpliced() | const copy = arr.slice(); copy.splice(0, 1); → const copy = arr.toSpliced(0, 1); |
exponentiation | 转换 Math.pow() 以表示责任运算符 | Math.pow(base, exp) → base ** exp |
nullishCoalescing | 将无效/ 未定义的检查转换为无效连接 | value != null ? value : default → value ?? default |
objectHasOwn | 转换 hasOwnProperty 改为 Object.hasOwn() | obj.hasOwnProperty(prop) → Object.hasOwn(obj, prop) |
postcssSignFunctions | 删除导入 postcss-sign-functions 多边填充 | 取消过时的多填充进口 |
spreadSyntax | 转换数组/对象方法以传播语法 | array.concat(other) → [...array, ...other] |
stringIncludes | 转换 indexOf 检查到 includes() | string.indexOf(substr) !== -1 → string.includes(substr) |
urlCanParse | 将 URL 验证尝试捕获到 URL.canParse() | try { new URL(url) } catch { } → URL.canParse(url) |
使用量
每个编码器都可以在程序上被导入和使用. 所有代码模块执行相同的接口 test() 财务报告和审定财务报表 apply() 方法。 。 .
基本示例
import {arrayAt} from '@e18e/web-features-codemods';
const sourceCode = `
const lastItem = myArray[myArray.length - 1];
const firstItem = myArray[0];
`;
// Optionally check if the codemod applies to this source.
// You can also skip this step and directly call apply() as non-matching
// codemods will simply return the original source.
if (arrayAt.test({source: sourceCode})) {
// Apply the transformation
const transformed = arrayAt.apply({source: sourceCode});
console.log(transformed);
// Output:
// const lastItem = myArray.at(-1);
// const firstItem = myArray[0];
}
发起人
许可证
麻省理工学院