ln-626-dead-code-auditor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dead Code Auditor (L3 Worker)

死代码审计器(L3 Worker)

Specialized worker auditing unused and unreachable code.
专门用于审计未使用和不可达代码的Worker。

Purpose & Scope

目标与范围

  • Worker in ln-620 coordinator pipeline
  • Audit dead code (Category 9: Low Priority)
  • Find unused imports, variables, functions, commented-out code
  • Calculate compliance score (X/10)
  • ln-620协调器流水线中的Worker
  • 审计死代码(类别9:低优先级)
  • 查找未使用的导入、变量、函数和被注释的代码
  • 计算合规性分数(X/10)

Inputs (from Coordinator)

输入(来自协调器)

Receives
contextStore
with tech stack, codebase root.
接收包含技术栈和代码库根目录的
contextStore

Workflow

工作流程

  1. Parse context
  2. Run dead code detection (linters, grep)
  3. Collect findings
  4. Calculate score
  5. Return JSON
  1. 解析上下文
  2. 运行死代码检测(使用代码检查器、grep工具)
  3. 收集检测结果
  4. 计算分数
  5. 返回JSON格式结果

Audit Rules

审计规则

1. Unreachable Code

1. 不可达代码

Detection:
  • Linter rules:
    no-unreachable
    (ESLint)
  • Check code after
    return
    ,
    throw
    ,
    break
Severity: MEDIUM
检测方式:
  • 代码检查器规则:
    no-unreachable
    (ESLint)
  • 检查
    return
    throw
    break
    语句之后的代码
严重程度:中等

2. Unused Imports/Variables/Functions

2. 未使用的导入/变量/函数

Detection:
  • ESLint:
    no-unused-vars
  • TypeScript:
    noUnusedLocals
    ,
    noUnusedParameters
  • Python:
    flake8
    with
    F401
    ,
    F841
Severity:
  • MEDIUM: Unused functions (dead weight)
  • LOW: Unused imports (cleanup needed)
检测方式:
  • ESLint:
    no-unused-vars
  • TypeScript:
    noUnusedLocals
    noUnusedParameters
  • Python:带有
    F401
    F841
    规则的
    flake8
严重程度:
  • 中等:未使用的函数(冗余代码)
  • :未使用的导入(需要清理)

3. Commented-Out Code

3. 被注释的代码

Detection:
  • Grep for
    //.*{
    or
    /*.*function
    patterns
  • Large comment blocks (>10 lines) with code syntax
Severity: LOW
Recommendation: Delete (git preserves history)
检测方式:
  • 使用grep匹配
    //.*{
    /*.*function
    模式
  • 包含代码语法的大注释块(超过10行)
严重程度:低
建议: 删除(git会保留历史记录)

4. Legacy Code & Backward Compatibility

4. 遗留代码与向后兼容

What: Backward compatibility shims, deprecated patterns, old code that should be removed
Detection:
  • Renamed variables/functions with old aliases:
    • Pattern:
      const oldName = newName
      or
      export { newModule as oldModule }
    • Pattern:
      function oldFunc() { return newFunc(); }
      (wrapper for backward compatibility)
  • Deprecated exports/re-exports:
    • Grep for
      // DEPRECATED
      ,
      @deprecated
      JSDoc tags
    • Pattern:
      export.*as.*old.*
      or
      export.*legacy.*
  • Conditional code for old versions:
    • Pattern:
      if.*legacy.*
      or
      if.*old.*version.*
      or
      isOldVersion ? oldFunc() : newFunc()
  • Migration shims and adapters:
    • Pattern:
      migrate.*
      ,
      Legacy.*Adapter
      ,
      .*Shim
      ,
      .*Compat
  • Comment markers:
    • Grep for
      // backward compatibility
      ,
      // legacy support
      ,
      // TODO: remove in v
    • Grep for
      // old implementation
      ,
      // deprecated
      ,
      // kept for backward
Severity:
  • HIGH: Backward compatibility shims in critical paths (auth, payment, core features)
  • MEDIUM: Deprecated exports still in use, migration code from >6 months ago
  • LOW: Recent migration code (<3 months), planned deprecation with clear removal timeline
Recommendation:
  • Remove backward compatibility shims - breaking changes are acceptable when properly versioned
  • Delete old implementations - keep only the correct/new version
  • Remove deprecated exports - update consumers to use new API
  • Delete migration code after grace period (3-6 months)
  • Clean legacy support comments - git history preserves old implementations
Effort:
  • S: Remove simple aliases, delete deprecated exports
  • M: Refactor code using old APIs to new APIs
  • L: Remove complex backward compatibility layer affecting multiple modules
说明: 向后兼容垫片、废弃模式以及应被移除的旧代码
检测方式:
  • 带有旧别名的重命名变量/函数:
    • 模式:
      const oldName = newName
      export { newModule as oldModule }
    • 模式:
      function oldFunc() { return newFunc(); }
      (用于向后兼容的包装函数)
  • 废弃的导出/重导出:
    • 使用grep匹配
      // DEPRECATED
      @deprecated
      JSDoc标签
    • 模式:
      export.*as.*old.*
      export.*legacy.*
  • 针对旧版本的条件代码:
    • 模式:
      if.*legacy.*
      if.*old.*version.*
      isOldVersion ? oldFunc() : newFunc()
  • 迁移垫片与适配器:
    • 模式:
      migrate.*
      Legacy.*Adapter
      .*Shim
      .*Compat
  • 注释标记:
    • 使用grep匹配
      // backward compatibility
      // legacy support
      // TODO: remove in v
    • 使用grep匹配
      // old implementation
      // deprecated
      // kept for backward
严重程度:
  • :关键路径(认证、支付、核心功能)中的向后兼容垫片
  • 中等:仍在使用的废弃导出、超过6个月的迁移代码
  • :近期迁移代码(少于3个月)、有明确移除时间线的计划中废弃项
建议:
  • 移除向后兼容垫片——在版本管理规范的情况下,破坏性变更可被接受
  • 删除旧实现——仅保留正确/新版本的代码
  • 移除废弃的导出——更新消费者以使用新API
  • 宽限期(3-6个月)后删除迁移代码
  • 清理遗留支持注释——git历史记录会保留旧实现
工作量:
  • :移除简单别名,删除废弃的导出
  • :重构使用旧API的代码以适配新API
  • :移除影响多个模块的复杂向后兼容层

Scoring Algorithm

评分算法

See
shared/references/audit_scoring.md
for unified formula and score interpretation.
统一的计算公式和分数说明请参考
shared/references/audit_scoring.md

Output Format

输出格式

json
{
  "category": "Dead Code",
  "score": 6,
  "total_issues": 12,
  "critical": 0,
  "high": 2,
  "medium": 3,
  "low": 7,
  "checks": [
    {"id": "unreachable_code", "name": "Unreachable Code", "status": "passed", "details": "No unreachable code detected"},
    {"id": "unused_exports", "name": "Unused Exports", "status": "failed", "details": "3 unused functions found"},
    {"id": "commented_code", "name": "Commented Code", "status": "warning", "details": "7 blocks of commented code"},
    {"id": "legacy_shims", "name": "Legacy Shims", "status": "failed", "details": "2 backward compatibility shims"}
  ],
  "findings": [
    {
      "severity": "MEDIUM",
      "location": "src/utils/helpers.ts:45",
      "issue": "Function 'formatDate' is never used",
      "principle": "Code Maintainability / Clean Code",
      "recommendation": "Remove unused function or export if needed elsewhere",
      "effort": "S"
    },
    {
      "severity": "HIGH",
      "location": "src/api/v1/auth.ts:12-15",
      "issue": "Backward compatibility shim for old password validation (6+ months old)",
      "principle": "No Legacy Code / Clean Architecture",
      "recommendation": "Remove old password validation, keep only new implementation. Update API version if breaking.",
      "effort": "M"
    }
  ]
}
json
{
  "category": "Dead Code",
  "score": 6,
  "total_issues": 12,
  "critical": 0,
  "high": 2,
  "medium": 3,
  "low": 7,
  "checks": [
    {"id": "unreachable_code", "name": "Unreachable Code", "status": "passed", "details": "No unreachable code detected"},
    {"id": "unused_exports", "name": "Unused Exports", "status": "failed", "details": "3 unused functions found"},
    {"id": "commented_code", "name": "Commented Code", "status": "warning", "details": "7 blocks of commented code"},
    {"id": "legacy_shims", "name": "Legacy Shims", "status": "failed", "details": "2 backward compatibility shims"}
  ],
  "findings": [
    {
      "severity": "MEDIUM",
      "location": "src/utils/helpers.ts:45",
      "issue": "Function 'formatDate' is never used",
      "principle": "Code Maintainability / Clean Code",
      "recommendation": "Remove unused function or export if needed elsewhere",
      "effort": "S"
    },
    {
      "severity": "HIGH",
      "location": "src/api/v1/auth.ts:12-15",
      "issue": "Backward compatibility shim for old password validation (6+ months old)",
      "principle": "No Legacy Code / Clean Architecture",
      "recommendation": "Remove old password validation, keep only new implementation. Update API version if breaking.",
      "effort": "M"
    }
  ]
}

Reference Files

参考文件

  • Audit scoring formula:
    shared/references/audit_scoring.md
  • Audit output schema:
    shared/references/audit_output_schema.md

Version: 3.0.0 Last Updated: 2025-12-23
  • 审计评分公式:
    shared/references/audit_scoring.md
  • 审计输出 schema:
    shared/references/audit_output_schema.md

版本: 3.0.0 最后更新时间: 2025-12-23