fix(ids): keep a type and a function differing only in case apart

DeclIdRegistry reused the id of any declaration that normalized alike, so class Foo
and function foo collapsed into one node. Reuse now needs the same kind or the same
spelling, which keeps TS class + namespace merging. Bump the AST cache schema.

(cherry picked from commit 9c44667f5f)
This commit is contained in:
berce authored and safishamsi committed 2026-10-31 23:44:04 +00:00
1 parent 2c97ae1b55
commit dc2ab47d84
3 files changed
+70 -3

No files matched your search

+1 -1
View File
@@ -38,7 +38,7 @@ except Exception:
_EXTRACTOR_VERSION = "unknown"
# Bump when AST cache-key semantics change independently of the package version.
_AST_CACHE_SCHEMA = 31 # Go credits a call in a package-level `var` initialiser (and its closures) to the file node (#4867); a Go method value passed as an argument is linked as an `indirect_call` (#4868); a Go receiver's field whose struct is declared in a sibling file of the same package is typed so its member calls resolve (#4866); and a Python dispatch table's `module.func` values resolve to the referenced functions as `indirect_call`s (#4773), so cached Go and Python extractions re-run; prior (30): File and per-declaration ids are kept distinct so a top-level declaration sharing the file's name is not dropped (#4854); a SystemVerilog `function new(...)` constructor gets its own node, `method` edge and parameter refs (#4848); a Go `var x = T{}`/`var x = &T{}` types `x` as `T` for receiver resolution like `x := T{}` (#4843); a PowerShell `using module`/`using assembly` name drops its file extension (#4847); a Solidity free-function `using {a, b} for T` set emits `uses` edges (#4846); a CommonLisp forward-referenced superclass binds to its local node instead of a sourceless stub (#4845); and Pascal `inherited`/`inline` form component blocks are recognised so later `object` blocks keep their parent (#4844), so cached extractions across those languages re-run; prior (29): OCaml `and`-chained module/class bindings are all walked (only the first was, so later bindings were silently dropped, #4822), and a SystemVerilog class `task` method is modeled like a `function` one with its own node and parameter refs while its task-locals no longer leak as class fields (#4774), so cached OCaml and Verilog/SystemVerilog extractions re-run; prior (28): Markdown ATX headings parse per CommonMark (closing `#` runs dropped, 0-3 space indent, hash-only lines mint no node, #4790) and fenced code blocks are tracked by CommonMark rules so headings inside fences stop leaking (#4787); YAML keys carry their written spelling and real line and `on`/`off`/`yes`/`no` keys no longer fold to booleans (#4784); rationale labels redact more credential shapes before caching (#4647); Razor `@implements` interface edges are extracted (#4820); aliased Terraform providers get their own node and `provider =` refs resolve (#4819); COBOL paragraphs after a nested `END PROGRAM` reattach to the enclosing program (#4824); Blade `wire:click` binds the bare method name (#4823); a Pascal record is a type with its own methods (#4825); VB.NET operator declarations are extracted as members with their bodies walked (#4821); and a Fortran call resolves to the subroutine it names (#4795), so cached Markdown/YAML/rationale/Razor/Terraform/COBOL/Blade/Pascal/VB.NET/Fortran extractions re-run; prior (27): VB.NET external namespace/type stubs now carry a `source_file` sentinel (#4725); Python member calls resolve through injected fields and method-result types (#4726); a Rust `use` is scoped to the crate it names (#4699), so cached VB.NET/Python/Rust extractions re-run; prior (26): Zig no longer crosses a call between a method and a same-named free function (#4755), and a Dart unnamed `extension on T` is detected (#4741), so cached Zig and Dart extractions re-run; prior (25): Python/Go/C++/Rust explicit type aliases (and Rust `union` items) are extracted as type nodes so references resolve instead of minting sourceless stubs (#4578/#4579/#4580/#4581); each C# method overload gets its own node bound by arity (#4584); a package.json dependency's manifest entry links to its ref node (#4569), so cached extractions re-run; prior (24): Objective-C `NS_CLOSED_ENUM` declarations are extracted as enums like `NS_ENUM` (#4705), and a `CREATE TABLE` whose column default the SQL grammar rejects (MySQL `DEFAULT current_timestamp()`) is recovered from its ERROR node (#4613), so cached ObjC and SQL extractions re-run; prior (23): `.yaml`/`.yml` files are structurally extracted (keys as nodes, config-vs-data gated) and reclassified from document to code (#2214), so cached YAML re-extracts; prior (22): SQL labels drop T-SQL brackets and routine bodies emit `reads_from`/`writes_to` (#4635), and a `GO` batch separator ends a statement (#4651), so cached `.sql` extractions re-run; prior (21): a CommonJS `const X = require('./m')` binding no longer mints a module-scope node that shadows the class the required file exports, so `new X()` resolves to the required declaration (#4621); prior (20): Obsidian `[[wikilink]]` targets that start with the scan root's own folder names now resolve to the real page (#4559), so cached markdown extractions re-run; prior (19): Dart `typedef` declarations are extracted as aliases (not variables of type `typedef`); C++ Metal/CUDA GPU qualifiers no longer mint sourceless god nodes (#4550); a `.h` header containing `extern "C"` routes to the C++ extractor; prior (18): Kotlin `typealias` (#4553), C# `delegate` (#4552), and Swift `macro` declarations (#4551) are now extracted so references stop minting sourceless stubs; Rust `use` rooted at a `mod` declaredLine truncated
_AST_CACHE_SCHEMA = 32 # A type and a function whose names differ only in case get separate node ids (#4904); Go credits a call in a package-level `var` initialiser (and its closures) to the file node (#4867); a Go method value passed as an argument is linked as an `indirect_call` (#4868); a Go receiver's field whose struct is declared in a sibling file of the same package is typed so its member calls resolve (#4866); and a Python dispatch table's `module.func` values resolve to the referenced functions as `indirect_call`s (#4773), so cached Go and Python extractions re-run; prior (30): File and per-declaration ids are kept distinct so a top-level declaration sharing the file's name is not dropped (#4854); a SystemVerilog `function new(...)` constructor gets its own node, `method` edge and parameter refs (#4848); a Go `var x = T{}`/`var x = &T{}` types `x` as `T` for receiver resolution like `x := T{}` (#4843); a PowerShell `using module`/`using assembly` name drops its file extension (#4847); a Solidity free-function `using {a, b} for T` set emits `uses` edges (#4846); a CommonLisp forward-referenced superclass binds to its local node instead of a sourceless stub (#4845); and Pascal `inherited`/`inline` form component blocks are recognised so later `object` blocks keep their parent (#4844), so cached extractions across those languages re-run; prior (29): OCaml `and`-chained module/class bindings are all walked (only the first was, so later bindings were silently dropped, #4822), and a SystemVerilog class `task` method is modeled like a `function` one with its own node and parameter refs while its task-locals no longer leak as class fields (#4774), so cached OCaml and Verilog/SystemVerilog extractions re-run; prior (28): Markdown ATX headings parse per CommonMark (closing `#` runs dropped, 0-3 space indent, hash-only lines mint no node, #4790) and fenced code blocks are tracked by CommonMark rules so headings inside fences stop leaking (#4787); YAML keys carry their written spelling and real line and `on`/`off`/`yes`/`no` keys no longer fold to booleans (#4784); rationale labels redact more credential shapes before caching (#4647); Razor `@implements` interface edges are extracted (#4820); aliased Terraform providers get their own node and `provider =` refs resolve (#4819); COBOL paragraphs after a nested `END PROGRAM` reattach to the enclosing program (#4824); Blade `wire:click` binds the bare method name (#4823); a Pascal record is a type with its own methods (#4825); VB.NET operator declarations are extracted as members with their bodies walked (#4821); and a Fortran call resolves to the subroutine it names (#4795), so cached Markdown/YAML/rationale/Razor/Terraform/COBOL/Blade/Pascal/VB.NET/Fortran extractions re-run; prior (27): VB.NET external namespace/type stubs now carry a `source_file` sentinel (#4725); Python member calls resolve through injected fields and method-result types (#4726); a Rust `use` is scoped to the crate it names (#4699), so cached VB.NET/Python/Rust extractions re-run; prior (26): Zig no longer crosses a call between a method and a same-named free function (#4755), and a Dart unnamed `extension on T` is detected (#4741), so cached Zig and Dart extractions re-run; prior (25): Python/Go/C++/Rust explicit type aliases (and Rust `union` items) are extracted as type nodes so references resolve instead of minting sourceless stubs (#4578/#4579/#4580/#4581); each C# method overload gets its own node bound by arity (#4584); a package.json dependency's manifest entry links to its ref node (#4569), so cached extractions re-run; prior (24): Objective-C `NS_CLOSED_ENUM` declarations are extracted as enums like `NS_ENUM` (#4705), and a `CREATE TABLE` whose column default the SQL grammar rejects (MySQL `DEFAULT current_timestamp()`) is recovered from its ERROR node (#4613), so cached ObjC and SQL extractions re-run; prior (23): `.yaml`/`.yml` files are structurally extracted (keys as nodes, config-vs-data gated) and reclassified from document to code (#2214), so cached YAML re-extracts; prior (22): SQL labels drop T-SQL brackets and routine bodies emit `reads_from`/`writes_to` (#4635), and a `GO` batch separator ends a statement (#4651), so cached `.sql` extractions re-run; prior (21): a CommonJS `const X = require('./m')` binding no longer mints a module-scope node that shadows the class the required file exports, so `new X()` resolves to the required declaration (#4621); prior (20): Obsidian `[[wikilink]]` targets that start with the scan root's own folder names now resolve to the real page (#4559), so cached markdown extractions re-run; prior (19): Dart `typedef` declarations are extracted as aliases (not variables of type `typedef`); C++ Metal/CUDA GPU qualifiers no longer mint sourceless god nodes (#4550); a `.h` header containing `extern "C"` routes to the C++ extractor; prior (18): Kotlin `typealias` (#4553), C# `delegate` (#4552), and Swift `macro` declarations (#4551) are now extractLine truncated
# Per-process memo: distributions() scans site-packages metadata, which is
+7 -2
View File
@@ -108,6 +108,7 @@ class DeclIdRegistry:
}
self._by_name: dict[tuple[str, str], set[str]] = {}
self._salted: set[str] = set()
self._spelled: dict[str, str] = {}
def resolve(self, nid: str, name: str, kind: str, taken) -> str:
key = (kind, nid)
@@ -117,6 +118,7 @@ class DeclIdRegistry:
if nid not in taken and owner is None:
self._owners[nid] = key
self._assigned[key] = nid
self._spelled[nid] = name
self._by_name.setdefault((kind, normalize_id(name)), set()).add(nid)
return nid
assigned = self._assigned.get(key)
@@ -125,9 +127,12 @@ class DeclIdRegistry:
# An id already used by a real declaration is a duplicate reference to
# that declaration (prototype owner, same name re-declared); reuse it
# instead of minting a second node. Only a salted id, whose owner stole
# the spelling from this declaration, forces a counter-salt (#4854).
# the spelling from this declaration, forces a counter-salt (#4854). A
# different kind only counts as a duplicate when spelled the same (TS
# `class Foo` + `namespace Foo`): `class Foo` + `function foo` are two (#4904).
if (nid in taken and owner is not None and owner[0] != "file"
and nid not in self._salted):
and nid not in self._salted
and (owner[0] == kind or self._spelled.get(nid) == name)):
return nid
base = make_id(nid, decl_salt(name))
candidate = base
+62
View File
@@ -0,0 +1,62 @@
"""Declarations of different kinds whose names differ only in case keep their own nodes (#4904)."""
import pytest
from graphify.extract import extract
from graphify.ids import DeclIdRegistry
CASES = {
"a.js": "class Foo {\n run() { return 1 }\n}\n\nfunction foo() {\n return new Foo().run()\n}\n",
"a.ts": "class Foo {\n run(): number { return 1 }\n}\n\nfunction foo(): number {\n return new Foo().run()\n}\n",
"a.kt": "class Foo { fun run(): Int = 1 }\n\nfun foo(): Int = Foo().run()\n",
"a.cpp": "class Foo { public: int run() { return 1; } };\n\nint foo() { Foo f; return f.run(); }\n",
"a.php": "<?php\nclass Foo { function run() { return 1; } }\n\nfunction foo() { return (new Foo())->run(); }\n",
"a.rb": "class Foo\n def run; 1; end\nend\n\ndef foo; Foo.new.run; end\n",
"a.swift": "struct Foo { func run() -> Int { 1 } }\n\nfunc foo() -> Int { Foo().run() }\n",
}
def _extract(tmp_path, name, source):
path = tmp_path / name
path.write_text(source, encoding="utf-8")
return extract([path], cache_root=tmp_path, root=tmp_path)
@pytest.mark.parametrize("name", sorted(CASES))
def test_type_and_function_differing_in_case_are_two_nodes(tmp_path, name):
result = _extract(tmp_path, name, CASES[name])
labels = {n["label"] for n in result["nodes"]}
assert {"Foo", "foo()"} <= labels
by_label = {n["label"]: n["id"] for n in result["nodes"]}
assert by_label["Foo"] != by_label["foo()"]
def test_the_call_belongs_to_the_function_not_the_class(tmp_path):
result = _extract(tmp_path, "a.js", CASES["a.js"])
by_label = {n["label"]: n["id"] for n in result["nodes"]}
assert not [e for e in result["edges"] if e["relation"] == "calls" and e["source"] == e["target"]]
assert by_label["foo()"] in {e["source"] for e in result["edges"] if e["relation"] == "calls"}
def test_same_spelling_across_kinds_stays_one_declaration(tmp_path):
result = _extract(
tmp_path,
"a.ts",
"export class Foo { x = 1 }\nexport namespace Foo { export const y = 2 }\n",
)
assert sum(1 for n in result["nodes"] if n["label"] == "Foo") == 1
def test_registry_reuses_only_same_kind_or_same_spelling():
taken = set()
registry = DeclIdRegistry()
assert registry.resolve("a_foo", "Foo", "type", taken) == "a_foo"
taken.add("a_foo")
assert registry.resolve("a_foo", "FOO", "type", taken) == "a_foo"
assert registry.resolve("a_foo", "Foo", "ns", taken) == "a_foo"
salted = registry.resolve("a_foo", "foo", "fn", taken)
assert salted != "a_foo"
assert registry.resolve("a_foo", "foo", "fn", taken) == salted