knip-deadcode
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity Knip Dead Code Detection Best Practices
社区Knip死代码检测最佳实践
Comprehensive guide for detecting and removing dead code in JavaScript and TypeScript projects using Knip. Contains 43 rules across 8 categories, prioritized by impact to guide configuration, CI integration, and cleanup workflows.
本指南全面介绍了如何使用Knip检测并移除JavaScript和TypeScript项目中的死代码。包含8个类别共43条规则,按影响优先级排序,可为配置、CI集成及代码清理流程提供指导。
When to Apply
适用场景
Reference these guidelines when:
- Configuring Knip for a new project or monorepo
- Investigating false positives or false negatives
- Setting up CI pipelines to prevent dead code regressions
- Using auto-fix to clean up unused code
- Optimizing Knip performance for large codebases
在以下场景中可参考本指南:
- 为新项目或单体仓库配置Knip
- 排查误报或漏报问题
- 搭建CI流水线以防止死代码回归
- 使用自动修复功能清理未使用代码
- 针对大型代码库优化Knip性能
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Configuration Foundations | CRITICAL | |
| 2 | Entry Point Strategy | CRITICAL | |
| 3 | Workspace & Monorepo | HIGH | |
| 4 | Dependency Analysis | HIGH | |
| 5 | Export Detection | MEDIUM-HIGH | |
| 6 | CI Integration | MEDIUM | |
| 7 | Auto-Fix Workflow | MEDIUM | |
| 8 | Performance Optimization | LOW-MEDIUM | |
| 优先级 | 类别 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | 配置基础 | 关键 | |
| 2 | 入口点策略 | 关键 | |
| 3 | 工作区与单体仓库 | 高 | |
| 4 | 依赖分析 | 高 | |
| 5 | 导出检测 | 中高 | |
| 6 | CI集成 | 中 | |
| 7 | 自动修复流程 | 中 | |
| 8 | 性能优化 | 中低 | |
Quick Reference
快速参考
1. Configuration Foundations (CRITICAL)
1. 配置基础(关键)
- - Avoid broad ignore patterns
config-avoid-broad-ignore - - Configure path aliases in Knip
config-configure-path-aliases - - Enable framework plugins explicitly
config-enable-plugins-explicitly - - Run without config first for baseline
config-run-without-config - - Separate entry files from project files
config-separate-entry-project - - Use JSON schema for configuration validation
config-use-json-schema - - Use negation patterns for exclusions
config-use-negation-patterns - - Use production mode for shipping code analysis
config-use-production-mode
- - 避免宽泛的忽略模式
config-avoid-broad-ignore - - 在Knip中配置路径别名
config-configure-path-aliases - - 显式启用框架插件
config-enable-plugins-explicitly - - 先无配置运行以获取基线
config-run-without-config - - 将入口文件与项目文件分离
config-separate-entry-project - - 使用JSON Schema验证配置
config-use-json-schema - - 使用否定模式排除内容
config-use-negation-patterns - - 使用生产模式分析待发布代码
config-use-production-mode
2. Entry Point Strategy (CRITICAL)
2. 入口点策略(关键)
- - Add dynamic import targets as entry points
entry-add-dynamic-imports - - Exclude test files from production entries
entry-exclude-test-files - - Include all application entry points
entry-include-all-entry-points - - Include binary scripts as entry points
entry-include-bin-scripts - - Use compilers for non-standard file types
entry-use-compilers - - Use plugin entry points for frameworks
entry-use-plugin-entries - - Verify entry points with debug mode
entry-verify-with-debug
- - 将动态导入目标添加为入口点
entry-add-dynamic-imports - - 从生产入口中排除测试文件
entry-exclude-test-files - - 包含所有应用入口点
entry-include-all-entry-points - - 将二进制脚本作为入口点
entry-include-bin-scripts - - 为非标准文件类型使用编译器
entry-use-compilers - - 为框架使用插件入口点
entry-use-plugin-entries - - 使用调试模式验证入口点
entry-verify-with-debug
3. Workspace & Monorepo (HIGH)
3. 工作区与单体仓库(高)
- - Configure root workspace explicitly
workspace-configure-root-workspace - - Ignore specific workspaces when needed
workspace-ignore-specific - - Isolate workspaces for strict dependency checking
workspace-isolate-for-strict - - List cross-workspace dependencies explicitly
workspace-list-cross-deps - - Configure plugins per workspace
workspace-per-workspace-plugins - - Use workspace globs for consistent configuration
workspace-use-workspace-globs
- - 显式配置根工作区
workspace-configure-root-workspace - - 必要时忽略特定工作区
workspace-ignore-specific - - 隔离工作区以进行严格依赖检查
workspace-isolate-for-strict - - 显式列出跨工作区依赖
workspace-list-cross-deps - - 为每个工作区配置插件
workspace-per-workspace-plugins - - 使用工作区通配符实现一致配置
workspace-use-workspace-globs
4. Dependency Analysis (HIGH)
4. 依赖分析(高)
- - Add unlisted dependencies to package.json
deps-add-unlisted-deps - - Avoid relying on transitive dependencies
deps-avoid-transitive-reliance - - Configure plugins for tool-specific dependencies
deps-configure-plugin-deps - - Fix unused files before dependencies
deps-fix-files-first - - Ignore conditionally loaded dependencies
deps-ignore-conditional-deps - - Remove obsolete type definition packages
deps-remove-obsolete-types
- - 将未列出的依赖添加至package.json
deps-add-unlisted-deps - - 避免依赖传递性依赖
deps-avoid-transitive-reliance - - 为工具特定依赖配置插件
deps-configure-plugin-deps - - 先修复未使用文件再处理依赖
deps-fix-files-first - - 忽略条件加载的依赖
deps-ignore-conditional-deps - - 移除过时的类型定义包
deps-remove-obsolete-types
5. Export Detection (MEDIUM-HIGH)
5. 导出检测(中高)
- - Check class members for unused code
exports-check-class-members - - Enable entry export checking for private packages
exports-enable-entry-exports - - Handle re-exports in barrel files
exports-handle-reexports - - Ignore exports used in same file
exports-ignore-same-file - - Tag public API exports with JSDoc
exports-tag-public-api - - Trace export usage before removal
exports-trace-usage - - Use include libs for type-based consumption
exports-use-include-libs
- - 检查类成员是否存在未使用代码
exports-check-class-members - - 为私有包启用入口导出检查
exports-enable-entry-exports - - 处理桶文件中的重导出
exports-handle-reexports - - 忽略同一文件内使用的导出
exports-ignore-same-file - - 使用JSDoc标记公开API导出
exports-tag-public-api - - 删除前追踪导出的使用情况
exports-trace-usage - - 使用include libs处理基于类型的引用
exports-use-include-libs
6. CI Integration (MEDIUM)
6. CI集成(中)
- - Add Knip to CI pipeline
ci-add-to-pipeline - - Separate production and default mode checks
ci-separate-production-check - - Enable cache for faster CI runs
ci-use-cache - - Use max issues for gradual adoption
ci-use-max-issues - - Use appropriate reporters for CI output
ci-use-reporters - - Use watch mode for local development
ci-watch-mode-local
- - 将Knip添加至CI流水线
ci-add-to-pipeline - - 分离生产模式与默认模式检查
ci-separate-production-check - - 启用缓存以加快CI运行速度
ci-use-cache - - 使用最大问题数实现逐步适配
ci-use-max-issues - - 为CI输出选择合适的报告器
ci-use-reporters - - 在本地开发中使用监听模式
ci-watch-mode-local
7. Auto-Fix Workflow (MEDIUM)
7. 自动修复流程(中)
- - Explicitly allow file removal
fix-allow-remove-files - - Format code after auto-fix
fix-format-after-fix - - Review auto-fix changes before commit
fix-review-before-commit - - Update package manager after dependency fix
fix-update-deps-after - - Use fix type for targeted cleanup
fix-use-fix-type
- - 显式允许删除文件
fix-allow-remove-files - - 自动修复后格式化代码
fix-format-after-fix - - 提交前审核自动修复的变更
fix-review-before-commit - - 修复依赖后更新包管理器
fix-update-deps-after - - 使用修复类型进行针对性清理
fix-use-fix-type
8. Performance Optimization (LOW-MEDIUM)
8. 性能优化(中低)
- - Filter issue types for focused analysis
perf-filter-issue-types - - Limit output for large codebases
perf-limit-output - - Profile performance for slow analysis
perf-profile-performance - - Use Bun runtime for faster analysis
perf-use-bun-runtime - - Enable cache for repeated analysis
perf-use-cache-flag - - Filter workspaces for faster monorepo analysis
perf-use-workspace-filter
- - 过滤问题类型以聚焦分析
perf-filter-issue-types - - 限制输出以适配大型代码库
perf-limit-output - - 分析性能以解决慢分析问题
perf-profile-performance - - 使用Bun运行时加快分析速度
perf-use-bun-runtime - - 启用缓存以加速重复分析
perf-use-cache-flag - - 过滤工作区以加快单体仓库分析
perf-use-workspace-filter
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读各参考文件以获取详细说明及代码示例:
- 章节定义 - 类别结构与影响级别说明
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 说明 |
|---|---|
| references/_sections.md | 类别定义与排序规则 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |