Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(ingestion): record this.method() self-dispatch calls in 6 languages (#1617)

Raghav Chamadiya3931eb531e59
diff --git a/packages/core/src/repowise/core/ingestion/queries/csharp.scm b/packages/core/src/repowise/core/ingestion/queries/csharp.scm
index ffebc97e0..bf2697b85 100644
--- a/packages/core/src/repowise/core/ingestion/queries/csharp.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/csharp.scm
@@ -183,10 +183,13 @@
arguments: (argument_list) @call.arguments
) @call.site
-; Member call: obj.Method(args)
+; Member call: obj.Method(args) — and self-dispatch: this.Method(args).
+; ``this`` is an ANONYMOUS node in tree-sitter-c-sharp, so it is matched as the
+; literal token "this"; `(this)` does not compile. See typescript.scm for why
+; this is an alternation rather than a second pattern.
(invocation_expression
function: (member_access_expression
- expression: (identifier) @call.receiver
+ expression: [(identifier) "this"] @call.receiver
name: (identifier) @call.target
)
arguments: (argument_list) @call.arguments
diff --git a/packages/core/src/repowise/core/ingestion/queries/javascript.scm b/packages/core/src/repowise/core/ingestion/queries/javascript.scm
index 53874d57c..f07592eef 100644
--- a/packages/core/src/repowise/core/ingestion/queries/javascript.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/javascript.scm
@@ -179,10 +179,12 @@
arguments: (arguments) @call.arguments
) @call.site
-; Method call: obj.method(args)
+; Method call: obj.method(args) — and self-dispatch: this.method(args).
+; See typescript.scm for why ``this`` rides an alternation in the receiver slot
+; instead of a second pattern.
(call_expression
function: (member_expression
- object: (identifier) @call.receiver
+ object: [(identifier) (this)] @call.receiver
property: (property_identifier) @call.target
)
arguments: (arguments) @call.arguments
diff --git a/packages/core/src/repowise/core/ingestion/queries/kotlin.scm b/packages/core/src/repowise/core/ingestion/queries/kotlin.scm
index fc1f00833..f0b5219f7 100644
--- a/packages/core/src/repowise/core/ingestion/queries/kotlin.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/kotlin.scm
@@ -61,10 +61,12 @@
(value_arguments) @call.arguments
) @call.site
-; Member call: obj.method(args)
+; Member call: obj.method(args) — and self-dispatch: this.method(args).
+; See typescript.scm for why ``this`` rides an alternation in the receiver slot
+; instead of a second pattern.
(call_expression
(navigation_expression
- (identifier) @call.receiver
+ [(identifier) (this_expression)] @call.receiver
(identifier) @call.target
)
(value_arguments) @call.arguments
diff --git a/packages/core/src/repowise/core/ingestion/queries/swift.scm b/packages/core/src/repowise/core/ingestion/queries/swift.scm
index f382d71fd..26fb6f209 100644
--- a/packages/core/src/repowise/core/ingestion/queries/swift.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/swift.scm
@@ -72,10 +72,12 @@
)
) @call.site
-; Member call: obj.method(args)
+; Member call: obj.method(args) — and self-dispatch: self.method(args).
+; Swift spells it ``self``, which the resolver treats identically to ``this``.
+; See typescript.scm for why this is an alternation rather than a second pattern.
(call_expression
(navigation_expression
- (simple_identifier) @call.receiver
+ [(simple_identifier) (self_expression)] @call.receiver
(navigation_suffix
(simple_identifier) @call.target
)
diff --git a/packages/core/src/repowise/core/ingestion/queries/typescript.scm b/packages/core/src/repowise/core/ingestion/queries/typescript.scm
index 4a2139173..0fb24fb0f 100644
--- a/packages/core/src/repowise/core/ingestion/queries/typescript.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/typescript.scm
@@ -216,10 +216,16 @@
arguments: (arguments) @call.arguments
) @call.site
-; Method call: obj.method(args)
+; Method call: obj.method(args) — and self-dispatch: this.method(args).
+; ``this`` is its own node type, not an identifier, so it is an alternation in
+; the receiver slot rather than a second pattern: one pattern means tree-sitter
+; does not run an extra match pass over every call_expression (measured +29-40%
+; parse time on angular for the two-pattern form, ~0% for this one), and there
+; is no duplicate rule to keep in sync. ``receiver_name`` then reads "this",
+; which call_resolver Strategy 3 already resolves against the caller's class.
(call_expression
function: (member_expression
- object: (identifier) @call.receiver
+ object: [(identifier) (this)] @call.receiver
property: (property_identifier) @call.target
)
arguments: (arguments) @call.arguments
diff --git a/tests/unit/ingestion/test_self_call_resolution.py b/tests/unit/ingestion/test_self_call_resolution.py
index 17d77180c..18dddd936 100644
--- a/tests/unit/ingestion/test_self_call_resolution.py
+++ b/tests/unit/ingestion/test_self_call_resolution.py
@@ -15,6 +15,8 @@ from __future__ import annotations
from datetime import datetime
from pathlib import Path
+import pytest
+
from repowise.core.ingestion.call_resolver import CallResolver
from repowise.core.ingestion.models import FileInfo, ParsedFile
from repowise.core.ingestion.parser import parse_file
@@ -106,6 +108,145 @@ class TestSelfCallStrategy:
assert bad == [], f"cross-class self-call must not resolve: {bad}"
+# ---------------------------------------------------------------------------
+# Self-dispatch across the languages whose self-reference keyword is NOT an
+# ordinary identifier node.
+#
+# Python reaches strategy 3 because ``self`` parses as ``(identifier)`` and the
+# stock member-call pattern captures it. In these six grammars the keyword is a
+# distinct node type (``this`` / ``this_expression`` / ``self_expression``, and
+# an anonymous ``"this"`` token in C#), so the member-call pattern used to match
+# nothing at all — not a lower-confidence edge, none — and the whole call site
+# was dropped before resolution. The receiver slot is now an alternation.
+#
+# Each case pins both directions: the in-class call resolves at strategy 3's
+# 0.95, and the same call from a class WITHOUT that method stays unresolved, so
+# widening the receiver did not turn into a name-only match.
+# ---------------------------------------------------------------------------
+
+TS_SRC = """
+class Widget {
+ helper(): number { return 1; }
+ run(): number { return this.helper(); }
+}
+
+class Other {
+ lonely(): number { return this.helper(); }
+}
+"""
+
+JS_SRC = """
+class Widget {
+ helper() { return 1; }
+ run() { return this.helper(); }
+}
+
+class Other {
+ lonely() { return this.helper(); }
+}
+"""
+
+CS_SRC = """
+class Widget {
+ int helper() { return 1; }
+ int run() { return this.helper(); }
+}
+
+class Other {
+ int lonely() { return this.helper(); }
+}
+"""
+
+KT_SRC = """
+class Widget {
+ fun helper(): Int { return 1 }
+ fun run(): Int { return this.helper() }
+}
+
+class Other {
+ fun lonely(): Int { return this.helper() }
+}
+"""
+
+SWIFT_SRC = """
+class Widget {
+ func helper() -> Int { return 1 }
+ func run() -> Int { return self.helper() }
+}
+
+class Other {
+ func lonely() -> Int { return self.helper() }
+}
+"""
+
+# Same shape, but with JSX in the method bodies: a .tsx file binds the
+# JSX-aware grammar, and plain TypeScript would exercise it only incidentally.
+# The render method is what makes the file actually need that grammar.
+TSX_SRC = """
+class Widget {
+ helper(): number { return 1; }
+ run(): number { return this.helper(); }
+ render() { return <div className="w">{this.run()}</div>; }
+}
+
+class Other {
+ lonely(): number { return this.helper(); }
+}
+"""
+
+# (id, relative path, language tag, source). ``.tsx`` is listed separately
+# because it binds the JSX-aware grammar while reusing typescript.scm — a
+# pattern valid against one grammar is not automatically valid against the other.
+SELF_DISPATCH_CASES = [
+ ("typescript", "src/widget.ts", "typescript", TS_SRC),
+ ("tsx", "src/widget.tsx", "typescript", TSX_SRC),
+ ("javascript", "src/widget.js", "javascript", JS_SRC),
+ ("csharp", "src/Widget.cs", "csharp", CS_SRC),
+ ("kotlin", "src/Widget.kt", "kotlin", KT_SRC),
+ ("swift", "src/Widget.swift", "swift", SWIFT_SRC),
+]
+
+
+ ("rel", "lang", "source"),
+ [c[1:] for c in SELF_DISPATCH_CASES],
+ ids=[c[0] for c in SELF_DISPATCH_CASES],
+)
+class TestSelfDispatchAcrossLanguages:
+ def test_self_call_is_recorded_as_a_call_site(
+ self, tmp_path: Path, rel: str, lang: str, source: str
+ ) -> None:
+ """The parser must emit a CallSite at all — the original bug dropped it."""
+ parsed = _parse_all(tmp_path, {rel: (lang, source)})
+ receivers = {c.receiver_name for c in parsed[rel].calls if c.target_name == "helper"}
+ assert receivers, f"no call site recorded for the self-call in {rel}"
+ assert receivers <= {"this", "self"}, (
+ f"self-call receiver must be this/self so strategy 3 fires; got {receivers}"
+ )
+
+ def test_self_call_resolves_within_same_class(
+ self, tmp_path: Path, rel: str, lang: str, source: str
+ ) -> None:
+ parsed = _parse_all(tmp_path, {rel: (lang, source)})
+ edges = _edges(parsed, tmp_path)
+ hits = [
+ e
+ for e in edges
+ if e[0].endswith("::Widget::run") and e[1].endswith("::Widget::helper")
+ ]
+ assert hits, f"this.helper() inside Widget.run must resolve; edges: {edges}"
+ assert hits[0][2] == 0.95
+
+ def test_self_call_does_not_cross_classes(
+ self, tmp_path: Path, rel: str, lang: str, source: str
+ ) -> None:
+ """Other.lonely calls this.helper() but Other has no helper: no edge."""
+ parsed = _parse_all(tmp_path, {rel: (lang, source)})
+ edges = _edges(parsed, tmp_path)
+ bad = [e for e in edges if e[0].endswith("::Other::lonely")]
+ assert bad == [], f"cross-class self-call must not resolve: {bad}"
+
+
class TestStrategy4Shadowing:
def test_cross_file_class_method_resolves_via_global_index(self, tmp_path: Path) -> None:
"""Painter.draw() from a non-importing file resolves at 2b's 0.75,