-
Notifications
You must be signed in to change notification settings - Fork 16
fix: classify destructured bindings as constant, not function #1902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
2b6a00d
fix: scope codegraph batch complexity targets to file paths
carlos-alm b28051a
fix: exclude parameters and interface/type members from dead-role cla…
carlos-alm 17fbcba
fix: credit import-type usages as exports consumers
carlos-alm 26918bb
fix: prevent fn-impact/query crash when -f/--file is passed
carlos-alm be6432c
fix: correct exported-symbol detection for literal and object-literal…
carlos-alm 4ead840
fix: exclude primitive type keywords from ast --kind string matching
carlos-alm f6326bf
fix: resolve call edges through renamed import specifiers
carlos-alm 590f560
fix: couple file_hashes updates with edge regeneration in incremental…
carlos-alm d3ea4a4
fix: compare signature-change diffs using new-file line ranges
carlos-alm c306812
feat: add environment doctor check for stale native binary and missin…
carlos-alm 8bb5893
fix: eliminate non-deterministic ordering in community detection
carlos-alm 46037b1
fix: sync update-graph.sh hook extension allowlist with EXTENSIONS
carlos-alm 6a84cf9
fix: recompute directory structure metrics for affected directories o…
carlos-alm 8d936d1
refactor: register hardcoded execFileSync/execSync maxBuffer values i…
carlos-alm 11c84be
fix: gate blast-radius check on newly introduced risk, not pre-existi…
carlos-alm 963301a
fix: gate identifier-argument dynamic call edges on callback-acceptin…
carlos-alm 3e8035d
fix: gate Array.from's callback arg by position, not just callee name
carlos-alm 879635e
fix: scope reexportedSymbols to actually-named re-export specifiers
carlos-alm 0fe9fc3
fix: stop CFG block/edge count from overriding AST-derived cyclomatic…
carlos-alm ccf3c19
fix: backfill edges.technique for incrementally-inserted calls edges
carlos-alm d5b1162
fix: wrap remote embedding JSON parse failure in EngineError
carlos-alm 8971017
refactor: extract shared platform-default-path helper in config.ts
carlos-alm 056c5e4
refactor: decompose loadConfig and related high-effort functions in c…
carlos-alm 0738171
refactor: decompose findDbPath and openRepo in db/connection.ts (docs…
carlos-alm 6f2423c
refactor: dedupe busy/locked error detection into isBusyOrLockedError
carlos-alm 4c02378
fix: log statSync failures in findDbPath instead of silently swallowi…
carlos-alm 0698e16
test: add direct unit coverage for closeDbPair/closeDbPairDeferred/cl…
carlos-alm 4ac3b99
fix: correct blast-radius/fn-impact computation for line-shifted decl…
carlos-alm 6767f09
feat: wire points-to solver max-iterations cap through DEFAULTS.analy…
carlos-alm 5f6f30a
refactor: route console.log calls in domain/search through logger
carlos-alm b479318
refactor: reduce cyclomatic complexity of computeDeltaModularityDirected
carlos-alm c39ac40
refactor: unify impact-level rendering format between audit and fn-im…
carlos-alm 067674f
refactor: dedupe computeSavings via pct helper and persist partial to…
carlos-alm f6807b4
fix: add main.rs driver to rust dynamic tracer fixture
carlos-alm 1c07a54
fix: scope diff file-header detection to between-hunk positions
carlos-alm bf82aa2
fix: update titan-grind's dead-symbol script for current roles --json…
carlos-alm cd02d27
fix: thread configured busyTimeoutMs through remaining read-only quer…
carlos-alm 980b5dc
fix: resolve computed string-literal keys in object-literal extractio…
carlos-alm 4a873a3
fix: add same-class bare-call fallback to incremental rebuild path (d…
carlos-alm 8f3348a
fix: unify Object.defineProperty accessor fallback kind-filtering bet…
carlos-alm acf804c
refactor: share chunked statement-cache primitive between builder and…
carlos-alm 986c851
refactor: cache prepared statements in applyEdgeTechniquesAfterNative…
carlos-alm 71e9174
fix: replace fixed-depth directory-proximity check with symmetric dis…
carlos-alm d2935cc
refactor: remove unreachable Partition delta-computation interface me…
carlos-alm 6515186
fix: emit reference edges for function identifiers used as object-lit…
carlos-alm 30c491a
refactor: unify call-ref and tests rendering format between audit and…
carlos-alm 9db911b
fix: classify destructured bindings as constant, not function (docs c…
carlos-alm 19aa6fd
fix: resolve merge conflicts with main
carlos-alm 156116b
test: add native call-edge preservation check (#1902)
carlos-alm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
tests/integration/issue-1773-destructured-binding-kind.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| /** | ||
| * Integration test for #1773: destructured object-pattern binding targets | ||
| * (renamed and non-renamed) were classified with `kind: "function"` instead | ||
| * of `kind: "constant"`, regardless of what the destructured value actually | ||
| * held. Because these bindings had no call-graph edges pointing at them by | ||
| * name, `codegraph roles --role dead` risked flagging them `dead-unresolved` | ||
| * (the "genuinely dead callable" bucket) even when read repeatedly elsewhere. | ||
| * | ||
| * Root cause: `extractDestructuredBindings` (src/extractors/javascript.ts, | ||
| * shared by both the walk and query extraction paths) and its native mirror | ||
| * `extract_destructured_bindings` (crates/codegraph-core/src/extractors/ | ||
| * javascript.rs) hardcoded `kind: 'function'` for every object-pattern | ||
| * binding target, on the theory that destructured names are usually | ||
| * callbacks. That miscategorized any destructured value that wasn't a | ||
| * function (e.g. `const { dbPath } = workerData`). | ||
| * | ||
| * Fix: both engines now emit `kind: 'constant'`, matching the existing | ||
| * convention for plain `const x = <literal>` bindings and array-pattern | ||
| * destructuring. Constants remain fully resolvable as call targets (call- | ||
| * target resolution is kind-agnostic), so callback-style destructured | ||
| * bindings still resolve; and constants with active call-graph-connected | ||
| * siblings in the same file are classified `leaf`, not `dead-unresolved` — | ||
| * exactly like any other same-file constant. | ||
| */ | ||
|
|
||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import Database from 'better-sqlite3'; | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
| import { buildGraph } from '../../src/domain/graph/builder.js'; | ||
| import { isNativeAvailable } from '../../src/infrastructure/native.js'; | ||
|
|
||
| // Repro 1 (renamed destructuring, issue #1773): a file with other real, | ||
| // call-graph-connected functions (giving it "active file siblings") plus a | ||
| // renamed destructured binding read repeatedly afterward — mirrors | ||
| // scripts/token-benchmark.ts's `const { values: flags } = parseArgs(...)`. | ||
| // Repro 2 (non-renamed destructuring, issue #1773): an isolated worker-style | ||
| // file with *only* a destructured binding from a non-call RHS and no other | ||
| // callables — mirrors tests/unit/snapshot-race-worker.mjs's | ||
| // `const { dbPath, name, force } = workerData`. | ||
| const FIXTURE = { | ||
| 'renamed-repro.js': ` | ||
| function parseArgs(opts) { return { values: computeDefaults(opts) }; } | ||
| function computeDefaults(opts) { return { runs: 3, model: opts.model }; } | ||
|
|
||
| const { values: flags } = parseArgs({ model: 'x' }); | ||
|
|
||
| function main() { | ||
| console.log(flags.runs, flags.model); | ||
| } | ||
| main(); | ||
| `, | ||
| 'worker-repro.mjs': ` | ||
| const { dbPath, name, force } = workerData; | ||
| save(name, { dbPath, force }); | ||
| `, | ||
| }; | ||
|
|
||
| function readNode(dbPath: string, file: string, name: string) { | ||
| const db = new Database(dbPath, { readonly: true }); | ||
| try { | ||
| return db | ||
| .prepare('SELECT name, kind, role FROM nodes WHERE file = ? AND name = ?') | ||
| .get(file, name) as { name: string; kind: string; role: string | null } | undefined; | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| } | ||
|
|
||
| function expectFixedKindAndRole(dbPath: string) { | ||
| // Repro 1: renamed destructured binding, active file siblings present. | ||
| const flags = readNode(dbPath, 'renamed-repro.js', 'flags'); | ||
| expect(flags, 'flags node not found').toBeDefined(); | ||
| expect(flags!.kind, 'flags must be kind constant, not function').toBe('constant'); | ||
| expect( | ||
| flags!.role, | ||
| `flags was classified as ${flags!.role} — must not be dead-unresolved`, | ||
| ).not.toBe('dead-unresolved'); | ||
|
|
||
| // Repro 2: non-renamed destructured bindings, no active file siblings. | ||
| for (const varName of ['dbPath', 'name', 'force']) { | ||
| const node = readNode(dbPath, 'worker-repro.mjs', varName); | ||
| expect(node, `${varName} node not found`).toBeDefined(); | ||
| expect(node!.kind, `${varName} must be kind constant, not function`).toBe('constant'); | ||
| // With no other call-graph-connected callable in the file, these fall | ||
| // back to 'dead-leaf' — the same honest classification any isolated, | ||
| // unreferenced-by-calls constant gets (properties/constants are leaf | ||
| // nodes by definition; call-graph reachability can't prove liveness for | ||
| // pure value bindings). The bug this test guards against is the far more | ||
| // misleading 'dead-unresolved' ("genuinely dead callable") label that a | ||
| // wrong kind: 'function' classification used to produce. | ||
| expect( | ||
| node!.role, | ||
| `${varName} was classified as ${node!.role} — must not be dead-unresolved`, | ||
| ).not.toBe('dead-unresolved'); | ||
| } | ||
| } | ||
|
|
||
| describe('destructured binding kind classification (#1773) — WASM', () => { | ||
| let tmpDir: string; | ||
|
|
||
| beforeAll(async () => { | ||
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1773-')); | ||
| for (const [rel, content] of Object.entries(FIXTURE)) { | ||
| fs.writeFileSync(path.join(tmpDir, rel), content); | ||
| } | ||
| await buildGraph(tmpDir, { engine: 'wasm', incremental: false, skipRegistry: true }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('classifies renamed and non-renamed destructured bindings as kind constant, not dead-unresolved', () => { | ||
| expectFixedKindAndRole(path.join(tmpDir, '.codegraph', 'graph.db')); | ||
| }); | ||
|
|
||
| it('still resolves calls made through a destructured callback-style binding', () => { | ||
| // `flags.runs`/`flags.model` are property reads, not calls, but `flags` | ||
| // itself must still show up as the attributed caller of `parseArgs` — the | ||
| // fix must not break caller attribution for the top-level binding. | ||
| const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); | ||
| const db = new Database(dbPath, { readonly: true }); | ||
| try { | ||
| const row = db | ||
| .prepare( | ||
| `SELECT COUNT(*) AS cnt | ||
| FROM edges e | ||
| JOIN nodes s ON e.source_id = s.id | ||
| JOIN nodes t ON e.target_id = t.id | ||
| WHERE s.name = 'flags' AND t.name = 'parseArgs' AND e.kind = 'calls'`, | ||
| ) | ||
| .get() as { cnt: number }; | ||
| expect(row.cnt, 'expected flags -> parseArgs calls edge to survive the kind fix').toBe(1); | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe.skipIf(!isNativeAvailable())( | ||
| 'destructured binding kind classification (#1773) — native', | ||
| () => { | ||
| let tmpDir: string; | ||
|
|
||
| beforeAll(async () => { | ||
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1773-native-')); | ||
| for (const [rel, content] of Object.entries(FIXTURE)) { | ||
| fs.writeFileSync(path.join(tmpDir, rel), content); | ||
| } | ||
| await buildGraph(tmpDir, { engine: 'native', incremental: false, skipRegistry: true }); | ||
| }, 60_000); | ||
|
|
||
| afterAll(() => { | ||
| fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('classifies renamed and non-renamed destructured bindings as kind constant, not dead-unresolved', () => { | ||
| expectFixedKindAndRole(path.join(tmpDir, '.codegraph', 'graph.db')); | ||
| }); | ||
|
|
||
| it('still resolves calls made through a destructured callback-style binding', () => { | ||
| // `flags.runs`/`flags.model` are property reads, not calls, but `flags` | ||
| // itself must still show up as the attributed caller of `parseArgs` — the | ||
| // fix must not break caller attribution for the top-level binding. | ||
| const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); | ||
| const db = new Database(dbPath, { readonly: true }); | ||
| try { | ||
| const row = db | ||
| .prepare( | ||
| `SELECT COUNT(*) AS cnt | ||
| FROM edges e | ||
| JOIN nodes s ON e.source_id = s.id | ||
| JOIN nodes t ON e.target_id = t.id | ||
| WHERE s.name = 'flags' AND t.name = 'parseArgs' AND e.kind = 'calls'`, | ||
| ) | ||
| .get() as { cnt: number }; | ||
| expect(row.cnt, 'expected flags -> parseArgs calls edge to survive the kind fix').toBe(1); | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| }); | ||
| }, | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The WASM describe block has two assertions — kind correctness (
expectFixedKindAndRole) and aflags → parseArgscall-edge preservation check — but the native block only covers the first. If a future change to the native engine's call-attribution for top-levelconstbindings silently drops that edge, this test suite won't catch it. Parity with the WASM block's secondit(...)would close the gap.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed — added the matching
flags → parseArgscall-edge preservation test to the native describe block in156116b8.