graphify icon indicating copy to clipboard operation
graphify copied to clipboard

TS/JS: `this.field.m()` is not resolved when `this.field = new X()` is assigned outside the constructor

Open sub4biz opened this issue 1 month ago • 4 comments

Summary

Since v0.9.91 (990ab1b7, addressing #4294) the receiver of this.field.m() is typed from the caller's own class and its bases. The constructor is scanned for this.f = new X() and this.f = <typed parameter>, but the same assignment in any other method, including a static one, is not recorded, so the call stays unresolved.

This is not a regression: v0.9.90 resolved none of the three shapes below, v0.9.91 resolves the constructor one. The commit message scopes the constructor deliberately (precision measured as "0 wrong" on TypeORM/NestJS), so this is a request to extend that scope, not a bug report. Python already resolves the same shape on v0.9.91: def setup(self): self.repo = RepoA() followed by self.repo.save() in another method produces the calls edge.

Reproduction

Run from a clean checkout of the tag. Each case writes two files and checks for a calls edge .go() -> .save():

import pathlib, tempfile
from graphify.extract import extract

REPOS = "export class RepoA { save(): number { return 1; } }\n"
SVC = {
    "constructor": "constructor() { this.repo = new RepoA(); }\n  go() { return this.repo.save(); }",
    "method": "init() { this.repo = new RepoA(); }\n  go() { return this.repo.save(); }",
    "static": "static init() { this.repo = new RepoA(); }\n  static go() { return this.repo.save(); }",
}
for name, body in SVC.items():
    d = pathlib.Path(tempfile.mkdtemp())
    (d / "repos.ts").write_text(REPOS, encoding="utf-8")
    (d / "svc.ts").write_text(
        'import { RepoA } from "./repos";\nexport class Svc {\n  ' + body + "\n}\n", encoding="utf-8")
    r = extract([d / "repos.ts", d / "svc.ts"], cache_root=d, parallel=False)
    label = {n["id"]: n["label"] for n in r["nodes"]}
    hit = any(e["relation"] == "calls" and label[e["source"]] == ".go()" and label[e["target"]] == ".save()"
              for e in r["edges"])
    print(f"{name:12} go() -> RepoA.save(): {'resolved' if hit else 'NOT resolved'}")
case v0.9.90 v0.9.91
constructor() { this.repo = new RepoA(); } NOT resolved resolved
init() { this.repo = new RepoA(); } NOT resolved NOT resolved
static init() { this.repo = new RepoA(); } NOT resolved NOT resolved

Expected: the init() and static init() cases resolve like the constructor one.

Related

  • #4294 (open): the report behind the class-scoped this.field lookup this builds on (990ab1b7, v0.9.91), which reads this.f = new X() in the constructor only. #4297 (closed, not merged) proposed the same scoping.
  • #4231 (closed, not merged): typed this.field from field declarations only. A different construct: it does not read assignments at all.

Suggested direction

Collect this.f = new X() from every method of the class into the table the constructor pass already fills (static methods under the static key), keeping the current precedence: a declared field type, then the constructor-derived one, then a method assignment. If one class assigns two different types to the same field, record neither (an assignment in a nested plain function, where this is rebound, should not count). With exactly this change on top of v0.9.91 the repro above resolves all three cases; a field assigned two different classes in one class resolves nothing; an assignment in a nested function is ignored; a declared field type wins.

sub4biz avatar Oct 15 '26 11:10 sub4biz

Thanks for opening this issue, @sub4biz. A maintainer will take a look soon.

If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

github-actions[bot] avatar Oct 15 '26 11:10 github-actions[bot]

Your wish is my command I'm opening a PR

This looks fixed on the v8 branch by 6948da8e (test(ts): cover method-assigned field receivers). Could a maintainer confirm and close?

omsenjalia avatar Oct 18 '26 08:10 omsenjalia

Retested the original reproduction on clean upstream v0.9.109: constructor, ordinary method and static method assignments all resolve .go() → RepoA.save(). The equivalent JavaScript cases resolve too.

I also ran 11 focused tests covering method and static assignments, per-class isolation, conflicting assignments, declared-type precedence and nested plain functions; all passed on the clean tag. The implementation landed in d19401eb, with regression coverage in 6948da8e. Both are included from v0.9.92 onward.

This covers the reported cases and the checked safeguards. Thank you for the fix!

sub4biz avatar Nov 15 '26 06:11 sub4biz