{"_rev":"3-1cd3a2471ffff3c680c8bacf9a53c268","time":{"created":"2025-11-04T05:57:02.032Z","modified":"2025-11-04T05:57:02.584Z","0.0.1":"2025-11-04T05:42:10.547Z","0.0.2":"2025-11-04T05:57:02.303Z"},"_id":"@bugfix2019/eslint-custom-rules","name":"@bugfix2019/eslint-custom-rules","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.2":{"name":"@bugfix2019/eslint-custom-rules","version":"0.0.2","description":"Custom ESLint rules for Site Backend project with ESLint v9 support","main":"dist/index.cjs","types":"dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js","require":"./dist/index.cjs"}},"type":"module","scripts":{"build":"tsup","test":"vitest","test:run":"vitest --run","test:coverage":"vitest --run --coverage","lint":"eslint src","prepublishOnly":"pnpm run build && pnpm run test:run"},"keywords":["eslint","eslint-plugin","eslint-rules","custom-rules","eslint9","flat-config","typescript","dto","code-style","linter"],"author":{"name":"ts02315607@gmail.com"},"contributors":[{"name":"Polaris","email":"ts02315607@gmail.com","url":"https://github.com/bugfix2020"},{"name":"Mingzhan Tang","email":"mzhantd@gmail.com","url":"https://github.com/mzhantd-crypto"}],"license":"MIT","repository":{"type":"git","url":"git+https://github.com/bugfix2020/eslint-custom-rules.git"},"bugs":{"url":"https://github.com/your-org/ce-platform/issues"},"homepage":"https://github.com/your-org/ce-platform/tree/main/apps/site-backend/eslint-custom-rules#readme","peerDependencies":{"eslint":"^9.0.0"},"devDependencies":{"@types/node":"^20.0.0","@typescript-eslint/parser":"^7.18.0","@typescript-eslint/types":"^8.46.2","@vitest/coverage-v8":"^4.0.6","eslint":"^9.33.0","tsup":"^8.5.0","typescript":"^5.9.3","vitest":"^4.0.6"},"engines":{"node":">=18.0.0"},"_id":"@bugfix2019/eslint-custom-rules@0.0.2","gitHead":"ff2c473a87ec58a53ce8ce1b2a85bfcbea9e08c5","_nodeVersion":"22.18.0","_npmVersion":"10.9.3","dist":{"integrity":"sha512-G5msgfQcCDklthk8w6ToGLdF1RQnArUyIBS4xYqu8Mgl466uHGJ8uS2K735LAMyv4NxODYS5LEE+hBFIkyeuKw==","shasum":"ded6301ea1de72d352d60b6e96f89d21eae1b423","tarball":"https://registry.npmjs.org/@bugfix2019/eslint-custom-rules/-/eslint-custom-rules-0.0.2.tgz","fileCount":10,"unpackedSize":84865,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDKkdNFoW6aC41h3ftAcxaU2YV6TUDMEEUtiMHXSyCW6AIgeEtkWjRIvnL43ax3PFpKjie5dT7VBAeE70Z10I4hANk="}]},"_npmUser":{"name":"bugfix2019","email":"670882978@qq.com"},"directories":{},"maintainers":[{"name":"bugfix2019","email":"670882978@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/eslint-custom-rules_0.0.2_1762235822111_0.9969695425992391"},"_hasShrinkwrap":false}},"maintainers":[{"name":"bugfix2019","email":"670882978@qq.com"}],"description":"Custom ESLint rules for Site Backend project with ESLint v9 support","homepage":"https://github.com/your-org/ce-platform/tree/main/apps/site-backend/eslint-custom-rules#readme","keywords":["eslint","eslint-plugin","eslint-rules","custom-rules","eslint9","flat-config","typescript","dto","code-style","linter"],"repository":{"type":"git","url":"git+https://github.com/bugfix2020/eslint-custom-rules.git"},"contributors":[{"name":"Polaris","email":"ts02315607@gmail.com","url":"https://github.com/bugfix2020"},{"name":"Mingzhan Tang","email":"mzhantd@gmail.com","url":"https://github.com/mzhantd-crypto"}],"author":{"name":"ts02315607@gmail.com"},"bugs":{"url":"https://github.com/your-org/ce-platform/issues"},"license":"MIT","readme":"# Site Backend ESLint Custom Rules\n\n[![npm version](https://img.shields.io/npm/v/@bugfix2019/eslint-custom-rules.svg)](https://www.npmjs.com/package/@bugfix2019/eslint-custom-rules)\n[![License](https://img.shields.io/npm/l/@bugfix2019/eslint-custom-rules.svg)](https://github.com/your-org/ce-platform/blob/main/LICENSE)\n[![ESLint](https://img.shields.io/badge/ESLint-9.x-blue.svg)](https://eslint.org/)\n[![Test Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](https://github.com/your-org/ce-platform)\n\n自定义 ESLint 规则集，用于强制执行 Site Backend 项目的代码规范。支持 **ESLint v9 Flat Config**。\n\n## ✨ 特性\n\n- 🎯 **100% TypeScript** - 完全类型安全\n- ✅ **100% 测试覆盖率** - 48 个单元测试\n- 🚀 **ESLint v9 原生支持** - 使用最新 Flat Config\n- 📦 **零依赖** - 仅需要 ESLint 作为 peer dependency\n- 🔧 **可配置** - 支持自定义规则级别\n\n## 📦 安装\n\n```bash\n# npm\nnpm install --save-dev @bugfix2019/eslint-custom-rules\n\n# pnpm\npnpm add -D @bugfix2019/eslint-custom-rules\n\n# yarn\nyarn add -D @bugfix2019/eslint-custom-rules\n```\n\n**前置要求**:\n- Node.js >= 18.0.0\n- ESLint >= 9.0.0\n\n## 🚀 快速开始\n\n在项目根目录的 `eslint.config.js` 中引入：\n\n```javascript\nimport customRules from '@bugfix2019/eslint-custom-rules';\n\nexport default [\n  // ... 其他配置\n  {\n    plugins: {\n      'custom': customRules\n    },\n    rules: {\n      'custom/dto-naming': 'error',\n      'custom/no-demo-code': 'error',\n      'custom/style-import': 'warn',\n      'custom/directory-structure': 'error',\n    }\n  }\n];\n```\n\n## 📚 包含的规则\n\n### 1. `dto-naming` - DTO 命名规范检测\n\n**用途**: 确保数据传输对象（DTO）遵循命名规范。\n\n**规则要求**:\n- `models/` 目录下的文件必须以 `.dto.ts` 结尾\n- DTO 接口必须以 `RequestDTO` 或 `ResponseDTO` 结尾\n- DTO 后缀必须为大写 `DTO`（不能是 `Dto`）\n\n**示例**:\n\n```typescript\n// ✅ 正确\n// 文件: src/models/user.dto.ts\nexport interface CreateUserRequestDTO {\n  name: string;\n}\nexport interface CreateUserResponseDTO {\n  id: string;\n}\n\n// ❌ 错误\n// 文件: src/models/user.ts (应该是 .dto.ts)\nexport interface CreateUserRequest { } // 应该是 RequestDTO\nexport interface CreateUserDto { } // 应该是 DTO (大写)\n```\n\n---\n\n### 2. `no-demo-code` - 禁止示例代码\n\n**用途**: 防止示例代码被提交到主分支。\n\n**规则要求**:\n- 禁止 `src/view/demo/*` 路径下的文件\n- 禁止 `*.example.tsx` 文件\n- 禁止以 `Demo` 开头的组件文件（如 `DemoButton.tsx`）\n\n**示例**:\n\n```typescript\n// ❌ 错误路径\nsrc/view/demo/TestComponent.tsx\nsrc/components/Button.example.tsx\nsrc/components/DemoButton.tsx\n\n// ✅ 正确路径\nsrc/components/Button.tsx\nsrc/pages/dashboard/index.tsx\n```\n\n---\n\n### 3. `style-import` - 样式导入规范\n\n**用途**: 强制使用 CSS Modules 避免样式冲突。\n\n**规则要求**:\n- 禁止直接导入全局样式 `index.scss`（非 `.module.scss`）\n- 必须使用 `.module.scss` 后缀导入样式\n- className 必须通过 `styles.*` 引用\n\n**示例**:\n\n```typescript\n// ❌ 错误\nimport './index.scss'; // 全局样式\nimport styles from './Button.scss'; // 应该是 .module.scss\n\nexport const Button = () => (\n  <div className=\"button\">Button</div> // 应该使用 styles.button\n);\n\n// ✅ 正确\nimport styles from './Button.module.scss';\n\nexport const Button = () => (\n  <div className={styles.button}>Button</div>\n);\n```\n\n---\n\n### 4. `directory-structure` - 目录结构检测\n\n**用途**: 确保文件放在正确的目录位置。\n\n**规则要求**:\n- `.dto.ts` 文件必须在 `models/` 目录下\n- Hook 文件必须在 `hooks/` 目录下且以 `use` 开头\n- 样式文件应与组件在同一目录\n\n**示例**:\n\n```typescript\n// ❌ 错误\nsrc/components/user.dto.ts // DTO 应该在 models/ 目录\nsrc/utils/useAuth.ts // Hook 应该在 hooks/ 目录\n\n// ✅ 正确\nsrc/models/user.dto.ts\nsrc/hooks/useAuth.ts\n```\n\n---\n\n## ⚙️ 配置选项\n\n### 规则级别说明\n\n- `\"error\"` (2): 违反规则会导致 ESLint 返回错误退出码（推荐用于严格规范）\n- `\"warn\"` (1): 违反规则会显示警告，但不影响退出码（推荐用于渐进式迁移）\n- `\"off\"` (0): 禁用规则\n\n### 推荐配置\n\n```javascript\n// 严格模式（推荐用于新项目）\nexport default [\n  {\n    plugins: { 'custom': customRules },\n    rules: {\n      'custom/dto-naming': 'error',\n      'custom/no-demo-code': 'error',\n      'custom/style-import': 'error',\n      'custom/directory-structure': 'error',\n    }\n  }\n];\n\n// 宽松模式（推荐用于旧项目迁移）\nexport default [\n  {\n    plugins: { 'custom': customRules },\n    rules: {\n      'custom/dto-naming': 'warn',\n      'custom/no-demo-code': 'error', // demo 代码始终禁止\n      'custom/style-import': 'warn',\n      'custom/directory-structure': 'warn',\n    }\n  }\n];\n```\n\n## 🧪 测试覆盖率\n\n| 规则 | 语句覆盖率 | 分支覆盖率 | 函数覆盖率 | 行覆盖率 |\n|------|-----------|-----------|-----------|---------|\n| dto-naming | 100% | 88.88% | 100% | 100% |\n| no-demo-code | 100% | 83.33% | 100% | 100% |\n| style-import | 100% | 95.83% | 100% | 100% |\n| directory-structure | 100% | 88% | 100% | 100% |\n| **总计** | **100%** | **89.77%** | **100%** | **100%** |\n\n运行测试：\n```bash\npnpm test              # 运行测试\npnpm test -- --coverage # 查看覆盖率报告\n```\n\n## 🛠️ 开发\n\n```bash\n# 安装依赖\npnpm install\n\n# 构建\npnpm run build\n\n# 运行测试\npnpm test\n\n# 查看覆盖率\npnpm test -- --coverage\n\n# Lint 检查\npnpm run lint\n```\n\n## 📦 发布到 npm\n\n```bash\n# 1. 确保构建成功\npnpm run build\n\n# 2. 确保测试通过\npnpm test -- --run\n\n# 3. 更新版本号\nnpm version patch  # 1.0.0 -> 1.0.1\nnpm version minor  # 1.0.0 -> 1.1.0\nnpm version major  # 1.0.0 -> 2.0.0\n\n# 4. 发布\nnpm publish --access public\n```\n\n## 📂 项目结构\n\n```\neslint-custom-rules/\n├── src/\n│   ├── rules/\n│   │   ├── dto-naming.ts           # DTO 命名规范\n│   │   ├── no-demo-code.ts         # 禁止示例代码\n│   │   ├── style-import.ts         # 样式导入规范\n│   │   └── directory-structure.ts  # 目录结构检测\n│   └── index.ts                    # 规则导出\n├── tests/\n│   ├── dto-naming.test.ts          # 15 个测试用例\n│   ├── no-demo-code.test.ts        # 7 个测试用例\n│   ├── style-import.test.ts        # 12 个测试用例\n│   └── directory-structure.test.ts # 14 个测试用例\n├── dist/                           # 构建输出\n├── package.json\n├── tsconfig.json\n├── tsup.config.ts                  # 构建配置\n├── vitest.config.ts                # 测试配置\n└── README.md\n```\n\n## 🤝 贡献\n\n如需添加新规则：\n\n1. 在 `src/rules/` 目录下创建新文件\n2. 在 `src/index.ts` 中注册规则\n3. 在 `tests/` 目录下添加对应测试\n4. 确保测试覆盖率达到 100%\n5. 更新 README.md\n\n## 📄 许可证\n\nMIT\n\n## 👥 贡献者\n\n感谢以下贡献者对本项目的贡献：\n\n<div style=\"display: flex; justify-content: center; align-items: flex-start; gap: 40px; flex-wrap: wrap;\">\n  <div style=\"text-align: center;\">\n    <a href=\"https://github.com/bugfix2020\"><img src=\"https://github.com/bugfix2020.png?size=100\" width=\"100px;\" style=\"border-radius: 50%;border:1px solid #efefef;\" alt=\"Yuxuan Liu\"/></a>\n    <br/>\n    <a href=\"https://github.com/bugfix2020\"><strong>Polaris</strong></a>\n    <br/>\n    <sub>📧 ts02315607@gmail.com</sub>\n  </div>\n  <div style=\"text-align: center;\">\n    <a href=\"https://github.com/mzhantd-crypto\"><img src=\"https://github.com/mzhantd-crypto.png?size=100\" width=\"100px;\" style=\"border-radius: 50%;border:1px solid #efefef;\" alt=\"Mingzhan Tang\"/></a>\n    <br/>\n    <a href=\"https://github.com/mzhantd-crypto\"><strong>Mingzhan Tang</strong></a>\n    <br/>\n    <sub>📧 mzhantd@gmail.com</sub>\n  </div>\n</div>\n\n## 🔗 相关链接\n\n- [ESLint v9 文档](https://eslint.org/docs/latest/)\n- [ESLint Flat Config](https://eslint.org/docs/latest/use/configure/configuration-files)\n- [自定义 ESLint 规则开发指南](https://eslint.org/docs/latest/extend/custom-rules)\n\n## ❓ 常见问题\n\n### Q: 如何在 Vite 项目中使用？\n\nA: 在 `vite.config.ts` 中使用 `vite-plugin-checker`：\n\n```typescript\nimport checker from 'vite-plugin-checker';\n\nexport default defineConfig({\n  plugins: [\n    checker({\n      eslint: {\n        lintCommand: 'eslint . --max-warnings=0',\n        useFlatConfig: true, // 🔑 关键配置\n      },\n    }),\n  ],\n});\n```\n\n### Q: 是否支持 ESLint v8？\n\nA: 本包仅支持 ESLint v9+。如需 v8 支持，请使用旧版本或手动迁移。\n\n### Q: 如何临时禁用某个规则？\n\nA: 使用 ESLint 注释：\n\n```typescript\n/* eslint-disable custom/style-import */\nimport './global.scss';\n/* eslint-enable custom/style-import */\n\n// 或单行禁用\nimport './global.scss'; // eslint-disable-line custom/style-import\n```\n\n---\n\n## 🙏 致谢\n\n感谢所有为本项目做出贡献的开发者！查看完整的[贡献者列表](./CONTRIBUTORS.md)。\n\n---\n\n**Made with ❤️ by [Polaris](https://github.com/bugfix2020)**\n\n","readmeFilename":"README.md"}