用途:ESLint 为 JavaScript 与 TypeScript 提供可插拔的代码检查(lint),用于发现代码问题、统一风格,并可通过 --fix 自动修复部分问题。
主要能力:
- 检查 JavaScript 与 TypeScript 代码,支持单文件或整个目录。
- 通过配置文件自定义规则、语言环境与忽略路径。
- 支持 React 相关插件(eslint-plugin-react、eslint-plugin-react-hooks、eslint-plugin-react-refresh)。
- 可输出多种格式,便于阅读或机器处理。
- 可集成到 CI,例如 GitHub Actions。
安装(资料中的命令):bun add -D eslint @eslint/js typescript-eslint globals
运行方式(保持原样):
- eslint . 检查所有文件
- eslint --fix . 自动修复
- eslint src/file.ts 检查指定文件
- eslint --format json . 输出 JSON
- eslint --format stylish . 输出带样式的结果
配置:资料采用 ESLint 9+ 的 Flat Config,文件为 eslint.config.js。基础配置使用 @eslint/js 的 recommended 与 typescript-eslint 的 recommended,并通过 globals 设定 Node 与 ES2022 全局变量;严格模式可使用 strictTypeChecked、stylisticTypeChecked,并设置 parserOptions 的 projectService 与 tsconfigRootDir。可用 ignores 忽略 dist/、node_modules/、.config.js 等路径,也可按 files(如 src/**/.ts、tests/**/*.ts)为不同目录设置不同规则。
常用规则:资料示例包括 TypeScript 的 @typescript-eslint/no-unused-vars、no-explicit-any、explicit-function-return-type、no-floating-promises、consistent-type-imports,以及 JavaScript 的 no-console、no-var、no-unused-vars,并可配置 argsIgnorePattern 等选项。
React:需安装 eslint-plugin-react、eslint-plugin-react-hooks、eslint-plugin-react-refresh,并在 plugins 中注册、合并规则。
CI:资料给出 GitHub Actions 工作流示例,包含 checkout、setup-bun、bun install 与 bun run lint。
限制:资料未说明内置规则全集、性能表现、各规则适用版本差异;收费、价格、平台支持、实际使用效果等内容暂未提供。以上仅为资料所列用法,未展开处不做推断。