5to6-codemod

中文文档
1.8.0发布于 2019/04/02MITCommonJS近 30 天下载 5.6万

A collection of codemods that allow you to transform your js code from ES5 to ES6.

原始使用文档
文档目录5 节
  1. Usage
  2. Option flags
  3. Recast options
  4. Transforms
  5. Known issues

Build Status GitHub version NPM version Downloads

5to6-codemod

A collection of codemods that allow you to transform your JavaScript code from ES5 to ES6 using jscodeshift.

Usage

  1. npm install -g jscodeshift
  2. npm install 5to6-codemod
  3. jscodeshift -t node_modules/5to6-codemod/transforms/[transform].js [files]
  4. Review changes via git diff. Keep what you want, throw it out if you don't. Magic!

Option flags

When executing codemods, you can configure options like so:

jscodeshift -t node_modules/5to6-codemod/transforms/[transform].js [files] --key=value

Recast options

Our transforms will automatically distinguish and pass through Recast config keys via jscodeshift. Official documentation for Recast's configuration can be found here. We currently support the following Recast keys:

  • esprima
  • inputSourceMap
  • lineTerminator
  • quote
  • range
  • reuseWhitespace
  • sourceFileName
  • sourceMapName
  • sourceRoot
  • tabWidth
  • tolerant
  • trailingComma
  • useTabs
  • wrapColumn

Transforms

  • amd - Transforms AMD style modules to ES6 import/export
  • cjs - Transforms CommonJS style require() calls to ES6 import statements
    • This transformation accepts the following option flags:
      • hoist: Enables hoisting require statements to the top
  • no-strict - Removes "use strict" statements
  • exports - Move CommonJS style module.exports statements to ES6 export statements
  • named-export-generation - Adds named exports corresponding to default export object keys. Only valid for ES6 modules exporting an object as the default export.
  • let - Replace all var calls to use let
  • simple-arrow - Replace all function expressions with a body of a sole return statement into arrow functions

Known issues

  • Can't automagically figure out when you want to use import * as varName.
  • End-of-line comments also missing in many situations
  • simple-arrow loses comments in the function expression body

版本与发布记录

5to6-codemod 共发布 14 个版本,首次发布于 2016/01/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.8.0latest2019/04/027 年前
1.7.12017/06/179 年前
1.6.02017/05/239 年前
1.5.02017/03/179 年前
1.4.02017/03/099 年前
1.3.02017/03/089 年前

关于 5to6-codemod 的常见问题

5to6-codemod 是什么?

A collection of codemods that allow you to transform your js code from ES5 to ES6.

如何安装 5to6-codemod?

在项目目录运行 npm install 5to6-codemod,或使用 pnpm add / yarn add / bun add。

5to6-codemod 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/5to6-codemod 是否存在。

5to6-codemod 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

5to6-codemod 可以商用吗?

5to6-codemod 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

5to6-codemod 的下载量有多少?

根据 npm 官方下载统计,5to6-codemod 在 2026/10/04 – 2026/11/02 这 30 天内被下载 55,922 次,日均约 1,864 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

5to6-codemod 还在维护吗?

最新版本 1.8.0 发布于 2019/04/02(7 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/01/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。