@huaiyou/config-eslint
v1.0.0
Published
Shared ESLint configuration with flat config (ESLint 9.x)
Readme
@huaiyou/config-eslint
基于 ESLint 9.x Flat Config 的共享 ESLint 配置,支持 TypeScript 和 React。
📦 安装
pnpm add -D @huaiyou/config-eslint eslint typescript🚀 快速开始
Base 配置(TypeScript 项目)
创建 eslint.config.js 文件:
import { base } from '@huaiyou/config-eslint';
export default base;React 配置
对于 React 项目:
import { react } from '@huaiyou/config-eslint';
export default react;✨ 特性
Base 配置
- ✅ ESLint 9.x Flat Config: 使用最新的配置格式
- ✅ TypeScript 支持: 完整的 TypeScript 类型检查
- ✅ Import 排序: 自动排序和组织 import 语句
- ✅ Prettier 集成: 与 Prettier 完美配合,无冲突
- ✅ 严格规则: 推荐的最佳实践和代码规范
React 配置
继承 Base 配置,额外包含:
- ✅ React 规则: React 最佳实践
- ✅ Hooks 检查: React Hooks 使用规范
- ✅ JSX 支持: 完整的 JSX 语法支持
- ✅ 自动检测: 自动检测 React 版本
📋 规则说明
TypeScript 规则
| 规则 | 设置 | 说明 |
| -------------------------------------------- | ----- | --------------------------------- |
| @typescript-eslint/no-unused-vars | error | 禁止未使用的变量(忽略 _ 开头) |
| @typescript-eslint/no-explicit-any | warn | 警告使用 any 类型 |
| @typescript-eslint/consistent-type-imports | error | 强制使用 type import |
| @typescript-eslint/no-misused-promises | error | 防止错误使用 Promise |
Import 规则
| 规则 | 设置 | 说明 |
| ----------------------------- | ----- | ---------------------- |
| import/order | error | 强制 import 顺序和分组 |
| import/no-duplicates | error | 禁止重复 import |
| import/newline-after-import | error | import 后需要空行 |
React 规则
| 规则 | 设置 | 说明 |
| ----------------------------- | ----- | ------------------------- |
| react/prop-types | off | 关闭 PropTypes(使用 TS) |
| react/react-in-jsx-scope | off | React 17+ 不需要 |
| react-hooks/rules-of-hooks | error | Hooks 使用规则 |
| react-hooks/exhaustive-deps | warn | Hooks 依赖检查 |
🔧 自定义配置
扩展配置
import { base } from '@huaiyou/config-eslint';
export default [
...base,
{
rules: {
// 你的自定义规则
'@typescript-eslint/no-explicit-any': 'error',
},
},
];忽略文件
import { base } from '@huaiyou/config-eslint';
export default [
...base,
{
ignores: ['dist/**', 'build/**', '*.config.js'],
},
];针对特定文件的规则
import { react } from '@huaiyou/config-eslint';
export default [
...react,
{
files: ['**/*.test.{ts,tsx}'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
},
];📝 配合 package.json 使用
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix"
}
}🔄 从 ESLint 8.x 迁移
ESLint 9.x 使用全新的 Flat Config 格式,主要变化:
- 配置文件:
.eslintrc.*→eslint.config.js - 格式: 对象配置 → 数组配置
- extends: 移除,使用数组展开
- plugins: 新的插件系统
🤝 贡献
欢迎提交 Issue 和 Pull Request 来改进配置。
📄 License
MIT
