Skip to content

feat(dead-code): detect unused return fields / dead output fields on interfaces #204

Description

@Wolfvin

Context

Saat CodeLens ditest terhadap codebase nyata (Wolfvin/Coretax-Auto-Downloader, issue #876), ditemukan bug berikut:

  • processMenuImageUpload() di vps-deploy/api/kds/src/lib/uploads.ts selalu generate dua output: fullPath (dipakai) dan thumbPath (tidak pernah dikonsumsi caller manapun)
  • Interface ProcessedMenuImage punya field thumb yang di-assign tapi tidak pernah di-read oleh consumer
  • audit --check dead-code hanya menemukan menuImageUpload (unused variable) dan MENU_UPLOAD_DIR (unused export) — adjacent noise, bukan root cause

Root cause tidak terdeteksi karena CodeLens tidak punya kemampuan: cross-file tracking property/field yang di-set tapi tidak pernah di-read oleh consumer.

Tujuan

Tambah detector baru ke audit --check dead-code: unused return fields / dead output fields.

Pattern yang harus dideteksi:

// uploads.ts
interface ProcessedMenuImage {
  fullPath: string   // DIPAKAI — consumer akses .fullPath
  thumbPath: string  // TIDAK DIPAKAI — tidak ada consumer yang akses .thumbPath
}

async function processMenuImageUpload(): Promise<ProcessedMenuImage> {
  // generate fullPath dan thumbPath, return keduanya
  return { fullPath: "...", thumbPath: "..." }
}
// menu.ts (consumer)
const result = await processMenuImageUpload()
result.fullPath  // <-- akses ini ada
// result.thumbPath  <-- tidak pernah diakses di seluruh codebase

Detector harus output: "field thumbPath pada return type ProcessedMenuImage (uploads.ts:X) tidak pernah diakses oleh consumer manapun".

Algoritma yang Disarankan

  1. Collect return types: dari graph_nodes, cari semua interface/type alias yang dipakai sebagai return type fungsi
  2. Collect field definitions: untuk setiap interface, list semua field names
  3. Collect property accesses: scan semua file untuk pola result.fieldName, obj.fieldName, destructuring { fieldName } — khususnya setelah call ke fungsi yang return interface tersebut
  4. Cross-reference: field yang ada di definition tapi tidak ditemukan di property accesses = dead output field

Constraint

  • Implementasi masuk ke scripts/commands/dead_code.py sebagai kategori baru: dead_output_fields
  • Harus bekerja tanpa LSP (tree-sitter only) — LSP bisa dipakai sebagai enhancement via --deep flag
  • False positive acceptable di versi pertama, tapi harus ada confidence score
  • Output format harus konsisten dengan kategori dead-code lain: {file, line, name, type, severity, message, suggestion, confidence}
  • Tambah dead_output_fields ke stats by_category

Source of Truth

  • Pattern dead-code detector yang sudah ada: baca scripts/commands/dead_code.py
  • Graph model: baca scripts/graph_model.py — khususnya graph_nodes schema
  • Cara akses graph: query SQLite .codelens/codelens.db di workspace

Definition of Done

  1. codelens audit <workspace> --check dead-code mendeteksi thumbPath di codebase vps-deploy/api/kds dari Wolfvin/Coretax-Auto-Downloader
  2. Output punya field type: "dead_output_field" dengan confidence >= 0.6
  3. by_category stats punya key dead_output_fields
  4. Tidak ada crash pada codebase TypeScript yang tidak punya interface dengan return type eksplisit
  5. Test case: minimal 1 unit test yang verify detection benar pada fixture TypeScript sederhana

Checklist Laporan Worker

  • Hasil codelens audit vps-deploy/api/kds --check dead-code — apakah thumbPath terdeteksi?
  • Confidence score yang dihasilkan
  • False positive count di codebase test (jika ada)
  • Link ke test case yang ditambah

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions