knip-deadcode

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Community 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

按优先级划分的规则类别

PriorityCategoryImpactPrefix
1Configuration FoundationsCRITICAL
config-
2Entry Point StrategyCRITICAL
entry-
3Workspace & MonorepoHIGH
workspace-
4Dependency AnalysisHIGH
deps-
5Export DetectionMEDIUM-HIGH
exports-
6CI IntegrationMEDIUM
ci-
7Auto-Fix WorkflowMEDIUM
fix-
8Performance OptimizationLOW-MEDIUM
perf-
优先级类别影响级别前缀
1配置基础关键
config-
2入口点策略关键
entry-
3工作区与单体仓库
workspace-
4依赖分析
deps-
5导出检测中高
exports-
6CI集成
ci-
7自动修复流程
fix-
8性能优化中低
perf-

Quick Reference

快速参考

1. Configuration Foundations (CRITICAL)

1. 配置基础(关键)

  • config-avoid-broad-ignore
    - Avoid broad ignore patterns
  • config-configure-path-aliases
    - Configure path aliases in Knip
  • config-enable-plugins-explicitly
    - Enable framework plugins explicitly
  • config-run-without-config
    - Run without config first for baseline
  • config-separate-entry-project
    - Separate entry files from project files
  • config-use-json-schema
    - Use JSON schema for configuration validation
  • config-use-negation-patterns
    - Use negation patterns for exclusions
  • config-use-production-mode
    - Use production mode for shipping code analysis
  • config-avoid-broad-ignore
    - 避免宽泛的忽略模式
  • config-configure-path-aliases
    - 在Knip中配置路径别名
  • config-enable-plugins-explicitly
    - 显式启用框架插件
  • config-run-without-config
    - 先无配置运行以获取基线
  • config-separate-entry-project
    - 将入口文件与项目文件分离
  • config-use-json-schema
    - 使用JSON Schema验证配置
  • config-use-negation-patterns
    - 使用否定模式排除内容
  • config-use-production-mode
    - 使用生产模式分析待发布代码

2. Entry Point Strategy (CRITICAL)

2. 入口点策略(关键)

  • entry-add-dynamic-imports
    - Add dynamic import targets as entry points
  • entry-exclude-test-files
    - Exclude test files from production entries
  • entry-include-all-entry-points
    - Include all application entry points
  • entry-include-bin-scripts
    - Include binary scripts as entry points
  • entry-use-compilers
    - Use compilers for non-standard file types
  • entry-use-plugin-entries
    - Use plugin entry points for frameworks
  • entry-verify-with-debug
    - Verify entry points with debug mode
  • 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. 工作区与单体仓库(高)

  • workspace-configure-root-workspace
    - Configure root workspace explicitly
  • workspace-ignore-specific
    - Ignore specific workspaces when needed
  • workspace-isolate-for-strict
    - Isolate workspaces for strict dependency checking
  • workspace-list-cross-deps
    - List cross-workspace dependencies explicitly
  • workspace-per-workspace-plugins
    - Configure plugins per workspace
  • workspace-use-workspace-globs
    - Use workspace globs for consistent configuration
  • 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. 依赖分析(高)

  • deps-add-unlisted-deps
    - Add unlisted dependencies to package.json
  • deps-avoid-transitive-reliance
    - Avoid relying on transitive dependencies
  • deps-configure-plugin-deps
    - Configure plugins for tool-specific dependencies
  • deps-fix-files-first
    - Fix unused files before dependencies
  • deps-ignore-conditional-deps
    - Ignore conditionally loaded dependencies
  • deps-remove-obsolete-types
    - Remove obsolete type definition packages
  • deps-add-unlisted-deps
    - 将未列出的依赖添加至package.json
  • 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. 导出检测(中高)

  • exports-check-class-members
    - Check class members for unused code
  • exports-enable-entry-exports
    - Enable entry export checking for private packages
  • exports-handle-reexports
    - Handle re-exports in barrel files
  • exports-ignore-same-file
    - Ignore exports used in same file
  • exports-tag-public-api
    - Tag public API exports with JSDoc
  • exports-trace-usage
    - Trace export usage before removal
  • exports-use-include-libs
    - Use include libs for type-based consumption
  • exports-check-class-members
    - 检查类成员是否存在未使用代码
  • exports-enable-entry-exports
    - 为私有包启用入口导出检查
  • exports-handle-reexports
    - 处理桶文件中的重导出
  • exports-ignore-same-file
    - 忽略同一文件内使用的导出
  • exports-tag-public-api
    - 使用JSDoc标记公开API导出
  • exports-trace-usage
    - 删除前追踪导出的使用情况
  • exports-use-include-libs
    - 使用include libs处理基于类型的引用

6. CI Integration (MEDIUM)

6. CI集成(中)

  • ci-add-to-pipeline
    - Add Knip to CI pipeline
  • ci-separate-production-check
    - Separate production and default mode checks
  • ci-use-cache
    - Enable cache for faster CI runs
  • ci-use-max-issues
    - Use max issues for gradual adoption
  • ci-use-reporters
    - Use appropriate reporters for CI output
  • ci-watch-mode-local
    - Use watch mode for local development
  • ci-add-to-pipeline
    - 将Knip添加至CI流水线
  • ci-separate-production-check
    - 分离生产模式与默认模式检查
  • ci-use-cache
    - 启用缓存以加快CI运行速度
  • ci-use-max-issues
    - 使用最大问题数实现逐步适配
  • ci-use-reporters
    - 为CI输出选择合适的报告器
  • ci-watch-mode-local
    - 在本地开发中使用监听模式

7. Auto-Fix Workflow (MEDIUM)

7. 自动修复流程(中)

  • fix-allow-remove-files
    - Explicitly allow file removal
  • fix-format-after-fix
    - Format code after auto-fix
  • fix-review-before-commit
    - Review auto-fix changes before commit
  • fix-update-deps-after
    - Update package manager after dependency fix
  • fix-use-fix-type
    - Use fix type for targeted cleanup
  • 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. 性能优化(中低)

  • perf-filter-issue-types
    - Filter issue types for focused analysis
  • perf-limit-output
    - Limit output for large codebases
  • perf-profile-performance
    - Profile performance for slow analysis
  • perf-use-bun-runtime
    - Use Bun runtime for faster analysis
  • perf-use-cache-flag
    - Enable cache for repeated analysis
  • perf-use-workspace-filter
    - Filter workspaces for faster monorepo analysis
  • perf-filter-issue-types
    - 过滤问题类型以聚焦分析
  • perf-limit-output
    - 限制输出以适配大型代码库
  • perf-profile-performance
    - 分析性能以解决慢分析问题
  • perf-use-bun-runtime
    - 使用Bun运行时加快分析速度
  • 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

参考文件

FileDescription
references/_sections.mdCategory definitions and ordering
assets/templates/_template.mdTemplate for new rules
metadata.jsonVersion and reference information
文件说明
references/_sections.md类别定义与排序规则
assets/templates/_template.md新规则模板
metadata.json版本与参考信息