Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(bindings): TS constructor parameter properties bind this.<name> (#76)
Shrish Dwivedi94b2d6ed93a5
diff --git a/src/graph/bindings.ts b/src/graph/bindings.tsindex 017fb7fd..c1765e35 100644--- a/src/graph/bindings.ts+++ b/src/graph/bindings.ts@@ -305,7 +305,18 @@ function handleTs(const pattern = node.childForFieldName("pattern");if (pattern?.type !== "identifier") return;const typeName = tsAnnotationTypeName(node.childForFieldName("type"), aliases);- if (typeName) bindings.set(scopePath, pattern.text, typeName);+ if (!typeName) return;+ bindings.set(scopePath, pattern.text, typeName);+ // A parameter PROPERTY (`constructor(private readonly svc: Svc){}`) is a parameter+ // AND a class field, so `this.svc` must resolve to its type — the default DI idiom+ // in NestJS/Angular. The plain-parameter binding above keys on the bare name and at+ // the constructor scope, which `this.svc.method()` call sites never reach; without+ // the field-style binding here their recvType is undefined and the call edge is+ // dropped (#76). Detected by the modifier child a plain parameter never carries.+ const isParamProperty = node.children.some(+ (c) => c.type === "accessibility_modifier" || c.type === "readonly" || c.type === "override_modifier",+ );+ if (isParamProperty) bindings.set(classScope ?? scopePath, `this.${pattern.text}`, typeName);}}diff --git a/test/graph-bindings.test.ts b/test/graph-bindings.test.tsindex 4d3a7a7a..3c6ac735 100644--- a/test/graph-bindings.test.ts+++ b/test/graph-bindings.test.ts@@ -43,6 +43,33 @@ test("ts: new-expression + type annotation + this", () => {assert.equal(edges.find((e) => e.name === "use")?.recvType, "Router");});+test("ts: constructor parameter property binds this.<name> (#76 — the NestJS/Angular DI idiom)", () => {+ // `private readonly svc: Svc` is a parameter AND a class field; `this.svc.m()` must+ // resolve. Before the fix its recvType was undefined and the call edge was dropped.+ const src = "class C {\n constructor(private readonly svc: Svc) {}\n run() { this.svc.m(); }\n}\n";+ assert.equal(callEdges(src, "typescript").find((e) => e.name === "m")?.recvType, "Svc");+ // every parameter-property modifier form: public / protected / bare readonly / override+ const forms = [+ "constructor(public svc: Svc) {}",+ "constructor(protected svc: Svc) {}",+ "constructor(readonly svc: Svc) {}",+ "constructor(private override svc: Svc) {}",+ ];+ for (const ctor of forms) {+ const s = `class C {\n ${ctor}\n run() { this.svc.m(); }\n}\n`;+ assert.equal(callEdges(s, "typescript").find((e) => e.name === "m")?.recvType, "Svc", ctor);+ }+});++test("ts: a PLAIN constructor parameter creates no this.<name> binding (no over-reach)", () => {+ // bare-name use of an ordinary parameter still binds (unchanged)+ const bare = "class C {\n constructor(svc: Svc) { svc.m(); }\n}\n";+ assert.equal(callEdges(bare, "typescript").find((e) => e.name === "m")?.recvType, "Svc");+ // but with no modifier it is NOT a field, so `this.svc` must stay unresolved (drop, not guess)+ const viaThis = "class C {\n constructor(svc: Svc) {}\n run() { this.svc.m(); }\n}\n";+ assert.equal(callEdges(viaThis, "typescript").find((e) => e.name === "m")?.recvType, undefined);+});+test("go: composite literal, var decl, NewX convention, receiver var", () => {const src = "package m\nfunc f() {\n u := User{}\n var d *DB\n s := NewServer()\n u.Save()\n d.Query()\n s.Start()\n}\nfunc (w *Worker) run() { w.stop() }\n";const edges = callEdges(src, "go", "x.go");