@automattic/eslint-changed

中文文档
2.1.7发布于 2026/10/15GPL-2.0-or-laterES Module构建来源已验证近 30 天下载 2.2万

Run eslint on files, but only report warnings and errors from lines that were changed.

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Manual diff
  4. With git
  5. Examples
  6. Inspiration

ESLint Changed

Run ESLint on files and only report new warnings and errors.

Installation

Install via your favorite JS package manager. Note the peer dependency on eslint.

For example,

npm install @automattic/eslint-changed eslint

Usage

To identify the changes, eslint-changed needs the ESLint output for both the old and new versions of the file, as well as the diff between them. If you use git, it can determine this automatically. Otherwise, you can supply the necessary information manually.

Options used in both modes are:

  • --debug: Enable debug output.
  • --ext <list>: Comma-separated list of JavaScript file extensions. Ignored if files are listed. (default: ".js")
  • --format <name>: ESLint format to use for output. (default: "stylish")
  • --in-diff-only: Only include messages on lines changed in the diff. This may miss things like deleting a var that leads to a new no-undef elsewhere.

Manual diff

The following options are used with manual mode:

  • --diff <file>: A file containing the unified diff of the changes.
  • --diff-base <dir>: Base directory the diff is relative to. Defaults to the current directory.
  • --eslint-orig <file>: A file containing the JSON output of eslint on the unchanged files.
  • --eslint-new <file>: A file containing the JSON output of eslint on the changed files.

With git

In git mode, eslint-changed needs to be able to run git. If this is not available by that name in the shell path, set environment variable GIT as appropriate.

The following options are used with git mode:

  • --git: Signify that you're using git mode.
  • --git-staged: Compare the staged version to the HEAD version (this is the default).
  • --git-unstaged: Compare the working copy version to the staged (or HEAD) version.
  • --git-base <ref>: Compare the HEAD version to the HEAD of a different base (e.g. branch).
  • --eslint-options <name=value...>: Pass additional options for ESLint. Value is interpreted as JSON if possible, otherwise it's treated as a literal string. See https://eslint.org/docs/latest/integrate/nodejs-api#-new-eslintoptions (or the equivalent for the version of eslint in use) for details on available options.

Examples

This will compare the staged changes with HEAD.

npx @automattic/eslint-changed --git

This will compare HEAD with origin/trunk.

npx @automattic/eslint-changed --git --git-base origin/trunk

This does much the same as the previous example, but manually. If you're using something other than git, you might do something like this.

# Produce a diff.
git diff origin/trunk...HEAD > /tmp/diff

# Check out the merge-base of origin/trunk and HEAD.
git checkout origin/trunk...HEAD

# Run ESLint.
npx eslint --format=json . > /tmp/eslint.orig.json

# Go back to HEAD.
git checkout -

# Run ESLint again.
npx eslint --format=json . > /tmp/eslint.new.json

# Run eslint-changed.
npx @automattic/eslint-changed --diff /tmp/diff --eslint-orig /tmp/eslint.orig.json --eslint=new /tmp/eslint.new.json

Note that, to be exactly the same as the above, you'd want to extract the list of files from the diff instead of linting everything. But this will work.

This will compare file.js in HEAD with origin/trunk, with the equivalent of passing --flag v10_config_lookup_from_file to eslint.

npx @automattic/eslint-changed --git --git-base origin/trunk --eslint-options flags='["v10_config_lookup_from_file"]' -- file.js

Inspiration

We had been using phpcs-changed for a while, and wanted the same thing for ESLint.

版本与发布记录

@automattic/eslint-changed 共发布 20 个版本,首次发布于 2021/08/13;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.7latest2026/10/1523 天前
2.1.62026/02/258 个月前
2.1.52025/12/0511 个月前
2.1.42025/11/2511 个月前
2.1.32025/10/171.1 年前
2.1.22025/08/071.3 年前

其他发布通道:alpha → 2.0.2-alpha。

关于 @automattic/eslint-changed 的常见问题

@automattic/eslint-changed 是什么?

Run eslint on files, but only report warnings and errors from lines that were changed.

如何安装 @automattic/eslint-changed?

在项目目录运行 npm install @automattic/eslint-changed,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:eslint(>=7.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 eslint-changed,可以用 npx eslint-changed 直接运行。

@automattic/eslint-changed 支持 TypeScript 吗?

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

@automattic/eslint-changed 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@automattic/eslint-changed 可以商用吗?

@automattic/eslint-changed 使用 GPL-2.0-or-later 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以兼容的 GPL 许可开源。仅在内部使用、不对外分发时不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@automattic/eslint-changed 的下载量有多少?

根据 npm 官方下载统计,@automattic/eslint-changed 在 2026/10/07 – 2026/11/05 这 30 天内被下载 21,910 次,日均约 730 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@automattic/eslint-changed 还在维护吗?

最新版本 2.1.7 发布于 2026/10/15(23 天前);至今共发布 20 个版本,其中过去 12 个月发布了 4 个;首次发布于 2021/08/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。