devlensio 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/graph/buildLookup.d.ts +30 -0
  2. package/dist/graph/buildLookup.js +36 -1
  3. package/dist/graph/edges/aliasChain.d.ts +7 -0
  4. package/dist/graph/edges/aliasChain.js +63 -0
  5. package/dist/graph/edges/aliasResolution.test.d.ts +1 -0
  6. package/dist/graph/edges/aliasResolution.test.js +351 -0
  7. package/dist/graph/edges/callEdges.js +101 -15
  8. package/dist/graph/edges/callEdges.test.js +119 -0
  9. package/dist/graph/edges/importEdges.js +124 -0
  10. package/dist/parser/declarationIds.d.ts +23 -0
  11. package/dist/parser/declarationIds.js +123 -0
  12. package/dist/parser/exportsMap.js +1 -90
  13. package/dist/parser/extractors/functions.d.ts +11 -0
  14. package/dist/parser/extractors/functions.js +69 -59
  15. package/dist/parser/extractors/moduleScope.d.ts +8 -0
  16. package/dist/parser/extractors/moduleScope.js +80 -0
  17. package/dist/parser/index.js +12 -0
  18. package/dist/parser/moduleScope.test.d.ts +1 -0
  19. package/dist/parser/moduleScope.test.js +174 -0
  20. package/extractors/go/bin/darwin-amd64/devlens_go_extractor +0 -0
  21. package/extractors/go/bin/darwin-arm64/devlens_go_extractor +0 -0
  22. package/extractors/go/bin/linux-amd64/devlens_go_extractor +0 -0
  23. package/extractors/go/bin/linux-arm64/devlens_go_extractor +0 -0
  24. package/extractors/go/bin/windows-amd64/devlens_go_extractor.exe +0 -0
  25. package/extractors/go/calls.go +45 -3
  26. package/extractors/go/parser.go +81 -36
  27. package/extractors/java/devlens_java_extractor.jar +0 -0
  28. package/extractors/java/src/devlens/extractor/Extractor.java +16 -2
  29. package/extractors/java/src/devlens/extractor/LookupMaps.java +2 -0
  30. package/extractors/java/src/devlens/extractor/Parser.java +33 -1
  31. package/extractors/java/src/devlens/extractor/edges/Calls.java +44 -0
  32. package/extractors/python/devlens_extractors_python/edges/calls.py +173 -33
  33. package/extractors/python/devlens_extractors_python/edges/imports.py +21 -1
  34. package/extractors/python/devlens_extractors_python/lookup.py +9 -0
  35. package/extractors/python/devlens_extractors_python/parser/__init__.py +10 -1
  36. package/extractors/rust/bin/darwin-amd64/devlens_rust_extractor +0 -0
  37. package/extractors/rust/bin/darwin-arm64/devlens_rust_extractor +0 -0
  38. package/extractors/rust/bin/linux-amd64/devlens_rust_extractor +0 -0
  39. package/extractors/rust/bin/linux-arm64/devlens_rust_extractor +0 -0
  40. package/extractors/rust/bin/windows-amd64/devlens_rust_extractor.exe +0 -0
  41. package/extractors/rust/src/calls.rs +32 -0
  42. package/extractors/rust/src/lookup.rs +66 -0
  43. package/package.json +1 -1
@@ -81,15 +81,19 @@ type ImportSpec struct {
81
81
  }
82
82
 
83
83
  type ParsedFile struct {
84
- RelPath string
85
- PkgName string
86
- IsTest bool
84
+ RelPath string
85
+ PkgName string
86
+ PkgPath string // import path of the containing package (module + dir)
87
+ IsTest bool
87
88
  Imports []ImportSpec
88
89
  Funcs []*ParsedFunc
89
90
  Structs []*ParsedStruct
90
91
  Interfaces []*ParsedInterface
91
- EndLine int
92
- AST *ast.File // kept for go/types check (typeload.go)
92
+ // FileCalls — calls in package-level VAR initializers (run at init time,
93
+ // owned by the FILE node). Function bodies stay on ParsedFunc.Calls.
94
+ FileCalls []CallSite
95
+ EndLine int
96
+ AST *ast.File // kept for go/types check (typeload.go)
93
97
  // iota const blocks on named numeric types (enum pattern, stretch)
94
98
  numericEnums []numericEnum
95
99
  }
@@ -175,6 +179,7 @@ func parseFile(repoPath, rel string, pkg *ParsedPackage, addError errFn) *Parsed
175
179
  pf := &ParsedFile{
176
180
  RelPath: rel,
177
181
  PkgName: f.Name.Name,
182
+ PkgPath: pkg.ImportPath,
178
183
  IsTest: strings.HasSuffix(rel, "_test.go"),
179
184
  AST: f,
180
185
  }
@@ -258,6 +263,19 @@ func parseFile(repoPath, rel string, pkg *ParsedPackage, addError errFn) *Parsed
258
263
  }
259
264
  }
260
265
  }
266
+ case token.VAR:
267
+ // Package-level initializers execute at init time — attribute
268
+ // their calls to the FILE node (function bodies stay on the
269
+ // ParsedFunc). const blocks are compile-time, no calls.
270
+ for _, spec := range d.Specs {
271
+ vs, ok := spec.(*ast.ValueSpec)
272
+ if !ok {
273
+ continue
274
+ }
275
+ for _, val := range vs.Values {
276
+ pf.FileCalls = append(pf.FileCalls, collectModuleCalls(val)...)
277
+ }
278
+ }
261
279
  case token.CONST:
262
280
  // Go const blocks inherit type + value from the previous spec
263
281
  // (`Red Color = iota; Green; Blue`). Two passes: resolve names
@@ -364,49 +382,76 @@ func receiverTypeName(e ast.Expr) string {
364
382
 
365
383
  // collectCalls — all CallExprs in a body (scope rule: closures inside the
366
384
  // body attribute to the enclosing function — Go has no nested func decls).
367
- // Each call also records its argument shape (count, cheap type tags, spread)
368
- // for arity-aware CALLS resolution.
369
385
  func collectCalls(body ast.Node) []CallSite {
370
386
  var calls []CallSite
371
387
  ast.Inspect(body, func(n ast.Node) bool {
372
- call, ok := n.(*ast.CallExpr)
373
- if !ok {
374
- return true
388
+ if call, ok := n.(*ast.CallExpr); ok {
389
+ if site := callSiteFrom(call); site != nil {
390
+ calls = append(calls, *site)
391
+ }
375
392
  }
376
- argCount := len(call.Args)
377
- hasSpread := call.Ellipsis != token.NoPos
378
- argTypes := make([]string, 0, argCount)
379
- for _, arg := range call.Args {
380
- argTypes = append(argTypes, argTypeTag(arg))
393
+ return true
394
+ })
395
+ return calls
396
+ }
397
+
398
+ // collectModuleCalls — calls in a package-level VAR initializer. Closures are
399
+ // NOT module scope: `var h = func() { svc.Get() }` runs svc.Get when h is
400
+ // invoked, so the walk does not descend into FuncLit.
401
+ func collectModuleCalls(root ast.Node) []CallSite {
402
+ var calls []CallSite
403
+ ast.Inspect(root, func(n ast.Node) bool {
404
+ if n == nil {
405
+ return false
381
406
  }
382
- fun := call.Fun
383
- // unwrap generic calls: f[T](...)
384
- if idx, ok := fun.(*ast.IndexExpr); ok {
385
- fun = idx.X
386
- } else if idx, ok := fun.(*ast.IndexListExpr); ok {
387
- fun = idx.X
407
+ if _, isFunc := n.(*ast.FuncLit); isFunc {
408
+ return false
388
409
  }
389
- switch f := fun.(type) {
390
- case *ast.Ident:
391
- calls = append(calls, CallSite{Str: f.Name, Node: f, Pos: call.Pos(), ArgCount: argCount, ArgTypes: argTypes, HasSpread: hasSpread})
392
- case *ast.SelectorExpr:
393
- calls = append(calls, CallSite{
394
- Str: exprText(f),
395
- Sel: f.Sel.Name,
396
- Receiver: identName(f.X), // root ident survives call chains: DB.Model(...).Updates → "DB"
397
- IsSel: true,
398
- Node: f,
399
- Pos: call.Pos(),
400
- ArgCount: argCount, ArgTypes: argTypes, HasSpread: hasSpread,
401
- })
402
- case *ast.FuncLit:
403
- // inline anonymous invocation — no symbol to resolve
410
+ if call, ok := n.(*ast.CallExpr); ok {
411
+ if site := callSiteFrom(call); site != nil {
412
+ calls = append(calls, *site)
413
+ }
404
414
  }
405
415
  return true
406
416
  })
407
417
  return calls
408
418
  }
409
419
 
420
+ // callSiteFrom — one CallExpr → CallSite (nil for inline func-literal
421
+ // invocation, which has no symbol to resolve). Each call records its argument
422
+ // shape (count, cheap type tags, spread) for arity-aware CALLS resolution.
423
+ func callSiteFrom(call *ast.CallExpr) *CallSite {
424
+ argCount := len(call.Args)
425
+ hasSpread := call.Ellipsis != token.NoPos
426
+ argTypes := make([]string, 0, argCount)
427
+ for _, arg := range call.Args {
428
+ argTypes = append(argTypes, argTypeTag(arg))
429
+ }
430
+ fun := call.Fun
431
+ // unwrap generic calls: f[T](...)
432
+ if idx, ok := fun.(*ast.IndexExpr); ok {
433
+ fun = idx.X
434
+ } else if idx, ok := fun.(*ast.IndexListExpr); ok {
435
+ fun = idx.X
436
+ }
437
+ switch f := fun.(type) {
438
+ case *ast.Ident:
439
+ return &CallSite{Str: f.Name, Node: f, Pos: call.Pos(), ArgCount: argCount, ArgTypes: argTypes, HasSpread: hasSpread}
440
+ case *ast.SelectorExpr:
441
+ return &CallSite{
442
+ Str: exprText(f),
443
+ Sel: f.Sel.Name,
444
+ Receiver: identName(f.X), // root ident survives call chains: DB.Model(...).Updates → "DB"
445
+ IsSel: true,
446
+ Node: f,
447
+ Pos: call.Pos(),
448
+ ArgCount: argCount, ArgTypes: argTypes, HasSpread: hasSpread,
449
+ }
450
+ }
451
+ // inline anonymous invocation — no symbol to resolve
452
+ return nil
453
+ }
454
+
410
455
  // argTypeTag — cheap per-argument type tag in Go's own vocabulary: literals
411
456
  // and composite literals carry their type, everything else (idents, calls,
412
457
  // expressions) degrades to "unknown". No go/types dependency at parse time.
@@ -97,9 +97,18 @@ public final class Extractor {
97
97
 
98
98
  // ── 2. build nodes + call facts ─────────────────────────────────
99
99
  for (Parser.ParsedFile pf : parsedFiles) {
100
- nodes.add(Contract.fileNode(pf.relPath, pf.endLine, pf.isTest ? "TEST" : "FILE", LANGUAGE));
100
+ Map<String, Object> fileNode =
101
+ Contract.fileNode(pf.relPath, pf.endLine, pf.isTest ? "TEST" : "FILE", LANGUAGE);
102
+ if (!pf.isTest && !pf.fileCalls.isEmpty()) {
103
+ // Module-scope facts — same shape as METHOD metadata.
104
+ @SuppressWarnings("unchecked")
105
+ Map<String, Object> fmeta = (Map<String, Object>) fileNode.get("metadata");
106
+ fmeta.put("calls", pf.fileCalls.stream().map(c -> c.name).distinct().sorted().toList());
107
+ fmeta.put("callSites", callSitesOf(pf.fileCalls));
108
+ }
109
+ nodes.add(fileNode);
101
110
  if (pf.isTest) {
102
- continue; // test files are LEAF nodes — children feed metadata only
111
+ continue;
103
112
  }
104
113
  List<String> childIds = new ArrayList<>();
105
114
  for (Parser.TypeInfo t : pf.allTypes()) {
@@ -134,6 +143,11 @@ public final class Extractor {
134
143
  // ── 3. shared lookup (built ONCE, consumed by every detector) ───
135
144
  LookupMaps lookup = LookupMaps.build(parsedFiles, nodes);
136
145
  lookup.methodCallFacts.putAll(methodCallFacts);
146
+ for (Parser.ParsedFile pf : parsedFiles) {
147
+ if (!pf.isTest && !pf.fileCalls.isEmpty()) {
148
+ lookup.fileCallFacts.put("file::" + pf.relPath, pf.fileCalls);
149
+ }
150
+ }
137
151
  lookup.testCases.putAll(testCases);
138
152
  ThirdParty tp = new ThirdParty(fp.rawDependencies, allowedThirdPartyLibs);
139
153
 
@@ -32,6 +32,8 @@ public final class LookupMaps {
32
32
  public final Map<String, List<String>> methodOverloads = new LinkedHashMap<>();
33
33
  /** method node id → parse-time call facts (CallInfo) */
34
34
  public final Map<String, List<Parser.CallInfo>> methodCallFacts = new LinkedHashMap<>();
35
+ /** FILE node id → call facts from field initializers + { } blocks */
36
+ public final Map<String, List<Parser.CallInfo>> fileCallFacts = new LinkedHashMap<>();
35
37
  /** test file relPath → test method names (metadata.testCases) */
36
38
  public final Map<String, List<String>> testCases = new HashMap<>();
37
39
 
@@ -10,6 +10,7 @@ import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
10
10
  import com.github.javaparser.ast.body.ConstructorDeclaration;
11
11
  import com.github.javaparser.ast.body.EnumDeclaration;
12
12
  import com.github.javaparser.ast.body.FieldDeclaration;
13
+ import com.github.javaparser.ast.body.InitializerDeclaration;
13
14
  import com.github.javaparser.ast.body.MethodDeclaration;
14
15
  import com.github.javaparser.ast.body.Parameter;
15
16
  import com.github.javaparser.ast.body.RecordDeclaration;
@@ -20,6 +21,7 @@ import com.github.javaparser.ast.type.ReferenceType;
20
21
  import com.github.javaparser.ast.expr.AnnotationExpr;
21
22
  import com.github.javaparser.ast.expr.Expression;
22
23
  import com.github.javaparser.ast.expr.FieldAccessExpr;
24
+ import com.github.javaparser.ast.expr.LambdaExpr;
23
25
  import com.github.javaparser.ast.expr.MethodCallExpr;
24
26
  import com.github.javaparser.ast.expr.NameExpr;
25
27
  import com.github.javaparser.ast.expr.NormalAnnotationExpr;
@@ -129,6 +131,9 @@ public final class Parser {
129
131
  public List<TypeInfo> types = new ArrayList<>();
130
132
  public Set<String> usedTypeNames = new LinkedHashSet<>();
131
133
  public List<String> errors = new ArrayList<>();
134
+ /** Calls in field initializers + static/instance { } blocks. These run
135
+ * at class-init time and are owned by the FILE node (module scope). */
136
+ public List<CallInfo> fileCalls = new ArrayList<>();
132
137
 
133
138
  /** Top-level + nested types flattened (nested get dotted names). */
134
139
  public List<TypeInfo> allTypes() {
@@ -232,6 +237,9 @@ public final class Parser {
232
237
  f.line = lineOf(fd, true);
233
238
  f.annotations.addAll(annotationNames(fd.getAnnotations()));
234
239
  t.fields.add(f);
240
+ // Field initializers run at class-init time — module scope.
241
+ v.getInitializer().ifPresent(init ->
242
+ pf.fileCalls.addAll(collectCalls(init, null, true)));
235
243
  }
236
244
  }
237
245
  }
@@ -243,6 +251,9 @@ public final class Parser {
243
251
  t.methods.add(extractMethod(md, pf, t.isInterface));
244
252
  } else if (member instanceof ConstructorDeclaration cd) {
245
253
  t.methods.add(extractConstructor(cd, pf));
254
+ } else if (member instanceof InitializerDeclaration id) {
255
+ // static { } / instance { } blocks — module-scope call sites.
256
+ pf.fileCalls.addAll(collectCalls(id.getBody(), null, true));
246
257
  }
247
258
  }
248
259
  }
@@ -297,12 +308,23 @@ public final class Parser {
297
308
  * lambdas attributed to another owner (same guard as Python's
298
309
  * NESTED_SCOPES).
299
310
  */
300
- private static List<CallInfo> collectCalls(BlockStmt body, MethodInfo owner) {
311
+ private static List<CallInfo> collectCalls(Node body, MethodInfo owner) {
312
+ return collectCalls(body, owner, false);
313
+ }
314
+
315
+ /**
316
+ * @param moduleScope when true, skip calls nested in a lambda / anonymous
317
+ * class method inside the node — those execute later, not at init time.
318
+ */
319
+ private static List<CallInfo> collectCalls(Node body, MethodInfo owner, boolean moduleScope) {
301
320
  List<CallInfo> calls = new ArrayList<>();
302
321
  if (body == null) {
303
322
  return calls;
304
323
  }
305
324
  body.walk(MethodCallExpr.class, call -> {
325
+ if (moduleScope && isNestedScope(call)) {
326
+ return;
327
+ }
306
328
  CallInfo ci = new CallInfo();
307
329
  ci.methodName = call.getNameAsString();
308
330
  ci.line = lineOf(call, true);
@@ -330,6 +352,16 @@ public final class Parser {
330
352
  return calls;
331
353
  }
332
354
 
355
+ /**
356
+ * True when the call executes inside a lambda or an anonymous/local class
357
+ * method — i.e. not at the initializer's own init time.
358
+ */
359
+ private static boolean isNestedScope(MethodCallExpr call) {
360
+ return call.findAncestor(LambdaExpr.class).isPresent()
361
+ || call.findAncestor(MethodDeclaration.class).isPresent()
362
+ || call.findAncestor(ConstructorDeclaration.class).isPresent();
363
+ }
364
+
333
365
  /**
334
366
  * Cheap argument type tag in the same vocabulary as declared param types:
335
367
  * literals map directly, anything else goes through the symbol solver
@@ -68,6 +68,50 @@ public final class Calls {
68
68
  metadata.put("resolvedCallDetails", resolvedDetails);
69
69
  }
70
70
  }
71
+
72
+ // ── module scope: field initializers + static/instance blocks ──
73
+ // Owned by the FILE node. Same ladder + symbol maps, so the
74
+ // third-party gate (Imports' allowed prefixes) applies identically.
75
+ for (Map.Entry<String, List<Parser.CallInfo>> entry : lookup.fileCallFacts.entrySet()) {
76
+ String callerId = entry.getKey();
77
+ Map<String, Object> caller = lookup.nodeById.get(callerId);
78
+ if (caller == null) {
79
+ continue;
80
+ }
81
+ String relPath = (String) caller.get("filePath");
82
+ String pkg = packageOf(callerId, relPath);
83
+ List<String> resolvedCalls = new ArrayList<>();
84
+ List<Map<String, String>> resolvedDetails = new ArrayList<>();
85
+
86
+ for (Parser.CallInfo ci : entry.getValue()) {
87
+ // parentClass null is safe: sameClassMethod builds a
88
+ // "file::x::null" key, which has no candidates.
89
+ Overloads.Pick pick = resolveOne(ci, lookup, relPath, null, pkg, callerId);
90
+ if (pick == null) {
91
+ continue;
92
+ }
93
+ Map<String, Object> edgeMeta = new LinkedHashMap<>();
94
+ edgeMeta.put("line", ci.line);
95
+ edgeMeta.put("matchedBy", pick.matchedBy);
96
+ edgeMeta.put("scope", "module");
97
+ edgesOut.add(Contract.edge("CALLS", callerId, pick.id, edgeMeta));
98
+ resolvedCalls.add(pick.id);
99
+
100
+ Map<String, String> detail = new LinkedHashMap<>();
101
+ detail.put("target", pick.id);
102
+ detail.put("method", ci.methodName);
103
+ detail.put("class", classNodeOf(pick.id));
104
+ resolvedDetails.add(detail);
105
+ }
106
+
107
+ if (!resolvedCalls.isEmpty()) {
108
+ java.util.Collections.sort(resolvedCalls);
109
+ @SuppressWarnings("unchecked")
110
+ Map<String, Object> metadata = (Map<String, Object>) caller.get("metadata");
111
+ metadata.put("resolvedCalls", resolvedCalls);
112
+ metadata.put("resolvedCallDetails", resolvedDetails);
113
+ }
114
+ }
71
115
  }
72
116
 
73
117
  private static Overloads.Pick resolveOne(Parser.CallInfo ci, LookupMaps lookup,
@@ -5,9 +5,16 @@ Consumes the shared LookupMaps (lookup.py). Resolution ladder:
5
5
  ClassName.method → same-file dotted lookup
6
6
  alias.member → lazy [pip]/pkg::member node
7
7
  module-alias.walk → submodule walk via module_map
8
- plain name → global nodes_by_name → import alias refinement
9
- Unknown/builtin names resolve to nothing (no edge) — metadata.calls keeps
10
- them for LLM context.
8
+ plain name → own file → import binding → global name
9
+
10
+ Aliasing (added 2026-10): a name at a call site is not always the name of a
11
+ declaration. `from .http import data as httpResponse` and a barrel that only
12
+ re-exports (`__init__.py: from .http import data`) mean the caller writes
13
+ `httpResponse` while the node is `http.py::data`. Python has no compiler symbol
14
+ table, so the chain is walked here by `_resolve_in_module()`: it asks a module
15
+ for a name, and when the module only *binds* that name from somewhere else, it
16
+ follows the binding — cycle-guarded, depth-capped, memo-free (chains are short
17
+ and every hop is an O(1) dict hit).
11
18
 
12
19
  Overload-aware tiebreak (mirrors src/graph/edges/callEdges.ts): when a lookup
13
20
  returns multiple same-name candidates, the call site's argument shape picks
@@ -16,9 +23,22 @@ legacy closest-by-path heuristic. The edge records matchedBy.
16
23
  """
17
24
  from __future__ import annotations
18
25
 
26
+ from dataclasses import dataclass, field
27
+
19
28
  from ..lookup import LookupMaps, closest_by_path, module_name
20
29
  from ..third_party import ThirdPartyRegistry
21
30
 
31
+ # Re-export chains are short in practice; the cap exists so a pathological
32
+ # barrel ring cannot loop. Cycles are also caught by `seen`.
33
+ MAX_ALIAS_HOPS = 24
34
+
35
+
36
+ @dataclass
37
+ class _Scoped:
38
+ """Result of resolving a name against one module's export surface."""
39
+ candidates: list[tuple[str, str]] = field(default_factory=list) # (node_id, rel)
40
+ chain: list[str] = field(default_factory=list) # file::name hops
41
+
22
42
 
23
43
  def _find_in_file(lookup: LookupMaps, rel: str, name: str) -> str | None:
24
44
  return lookup.nodes_by_file.get(rel, {}).get(name)
@@ -30,6 +50,69 @@ def _find_all_in_file(lookup: LookupMaps, rel: str, name: str) -> list[tuple[str
30
50
  return [(node_id, rel) for node_id in ids]
31
51
 
32
52
 
53
+ def _resolve_in_module(lookup: LookupMaps, rel: str, name: str,
54
+ seen: set[tuple[str, str]] | None = None) -> _Scoped:
55
+ """Resolve `name` against the EXPORT SURFACE of module `rel`.
56
+
57
+ In Python a module's namespace is its declarations PLUS everything it
58
+ imported, so `from pkg import name` works whenever `name` is a module-level
59
+ attribute of `pkg` — including names `pkg` itself imported from elsewhere.
60
+ That is exactly how a package re-exports a symbol (`__init__.py`), and it is
61
+ the phenomenon this resolver models:
62
+
63
+ 1. declared here → the node
64
+ 2. `Alias.member` where Alias is bound → resolve Alias, then the member
65
+ 3. bound by an import → recurse into the binding's target
66
+ 4. `from .x import *` sources → recurse with the same name
67
+ """
68
+ seen = seen if seen is not None else set()
69
+ key = (rel, name)
70
+ if key in seen or len(seen) >= MAX_ALIAS_HOPS:
71
+ return _Scoped()
72
+ seen.add(key)
73
+
74
+ # 1. a declaration in this module (dotted names included: "Class.method")
75
+ declared = _find_all_in_file(lookup, rel, name)
76
+ if declared:
77
+ return _Scoped(declared, [f"{rel}::{name}"])
78
+
79
+ symbols = lookup.symbol_maps.get(rel, {})
80
+ names = lookup.symbol_names.get(rel, {})
81
+
82
+ # 2. member access on a bound name: `US.get` → resolve `US`
83
+ if "." in name:
84
+ root, suffix = name.split(".", 1)
85
+ binding = symbols.get(root)
86
+ if binding and binding.startswith("file::"):
87
+ original = names.get(root, root)
88
+ deeper = _resolve_in_module(lookup, binding[len("file::"):],
89
+ f"{original}.{suffix}", seen)
90
+ if deeper.candidates:
91
+ return _Scoped(deeper.candidates, [f"{rel}::{name}"] + deeper.chain)
92
+ return _Scoped()
93
+
94
+ # 3. a module-level binding created by an import — the re-export hop
95
+ binding = symbols.get(name)
96
+ if binding:
97
+ original = names.get(name, name)
98
+ if binding.startswith("file::"):
99
+ deeper = _resolve_in_module(lookup, binding[len("file::"):], original, seen)
100
+ if deeper.candidates:
101
+ return _Scoped(deeper.candidates, [f"{rel}::{name}"] + deeper.chain)
102
+ return _Scoped()
103
+ # third-party node id or a direct node id — nothing to walk
104
+ origin_rel = lookup.node_by_id.get(binding, {}).get("filePath", rel)
105
+ return _Scoped([(binding, origin_rel)], [f"{rel}::{name}"])
106
+
107
+ # 4. star imports copy public names into this namespace
108
+ for star_rel in lookup.star_imports.get(rel, []):
109
+ deeper = _resolve_in_module(lookup, star_rel, name, seen)
110
+ if deeper.candidates:
111
+ return _Scoped(deeper.candidates, [f"{rel}::{name}"] + deeper.chain)
112
+
113
+ return _Scoped()
114
+
115
+
33
116
  def _bound_params(node: dict) -> tuple[list, list]:
34
117
  """(effective_params, effective_parameters) for arity/type matching —
35
118
  bound methods hide self/cls from the call site's argument count."""
@@ -114,9 +197,22 @@ def _pick(candidates: list[tuple[str, str]], node: dict, call_site: dict | None,
114
197
  return closest_by_path(pool, rel), "name"
115
198
 
116
199
 
117
- def _walk_module(lookup: LookupMaps, alias_target: str, rest: str) -> list[tuple[str, str]]:
200
+ def _pick3(candidates: list[tuple[str, str]], node: dict, call_site: dict | None,
201
+ lookup: LookupMaps, rel: str, chain: list[str]) -> tuple[str | None, str, list[str]]:
202
+ """_pick() plus the alias chain — the shape _resolve() returns."""
203
+ target, matched_by = _pick(candidates, node, call_site, lookup, rel)
204
+ return target, matched_by, chain
205
+
206
+
207
+ def _walk_module(lookup: LookupMaps, alias_target: str, rest: str,
208
+ root_hint: str | None = None) -> _Scoped:
118
209
  """'user.User' with alias → file::models/__init__.py: consume submodule
119
- segments via module_map, then look up the symbol in the final file."""
210
+ segments via module_map, then resolve the symbol in the final module's
211
+ export surface (which may itself re-export it).
212
+
213
+ `root_hint` is what the alias was called at the source
214
+ (`from .services import UserService as US` → "UserService"), so a member
215
+ call `US.get()` resolves to the dotted node `UserService.get`."""
120
216
  current_rel = alias_target[len("file::"):]
121
217
  current_module = module_name(current_rel)
122
218
  segments = rest.split(".")
@@ -130,21 +226,29 @@ def _walk_module(lookup: LookupMaps, alias_target: str, rest: str) -> list[tuple
130
226
  current_rel, current_module = nxt, candidate
131
227
  consumed += 1
132
228
 
133
- return _find_all_in_file(lookup, current_rel, ".".join(segments[consumed:]))
229
+ tail = ".".join(segments[consumed:])
230
+ if root_hint and consumed == 0 and tail:
231
+ scoped = _resolve_in_module(lookup, current_rel, f"{root_hint}.{tail}")
232
+ if scoped.candidates:
233
+ return scoped
234
+ return _resolve_in_module(lookup, current_rel, tail)
134
235
 
135
236
 
136
237
  def _resolve(called: str, node: dict, lookup: LookupMaps,
137
- registry: ThirdPartyRegistry, call_site: dict | None) -> tuple[str | None, str]:
238
+ registry: ThirdPartyRegistry,
239
+ call_site: dict | None) -> tuple[str | None, str, list[str]]:
138
240
  rel = node["filePath"]
139
241
  symbols = lookup.symbol_maps.get(rel, {})
242
+ symbol_names = lookup.symbol_names.get(rel, {})
140
243
 
141
244
  # ── 1. self.x() / cls.x() → method of the enclosing class ──
142
245
  if called.startswith(("self.", "cls.")):
143
246
  parent_class = node["metadata"].get("parentClass")
144
247
  if parent_class:
145
- return _pick(_find_all_in_file(lookup, rel, f"{parent_class}.{called.split('.', 1)[1]}"),
146
- node, call_site, lookup, rel)
147
- return None, "name"
248
+ return _pick3(_find_all_in_file(lookup, rel,
249
+ f"{parent_class}.{called.split('.', 1)[1]}"),
250
+ node, call_site, lookup, rel, [])
251
+ return None, "name", []
148
252
 
149
253
  # ── 2. dotted name ──
150
254
  if "." in called:
@@ -152,40 +256,63 @@ def _resolve(called: str, node: dict, lookup: LookupMaps,
152
256
 
153
257
  # 2a. ClassName.method defined in the same file
154
258
  if root in lookup.nodes_by_file.get(rel, {}):
155
- return _pick(_find_all_in_file(lookup, rel, called),
156
- node, call_site, lookup, rel)
259
+ return _pick3(_find_all_in_file(lookup, rel, called),
260
+ node, call_site, lookup, rel, [])
157
261
 
158
- # 2b. member access through an import alias
159
262
  alias_target = symbols.get(root)
160
263
  if not alias_target:
161
- return None, "name"
264
+ # 2c. the root itself may be re-exported into THIS module
265
+ scoped = _resolve_in_module(lookup, rel, called)
266
+ if scoped.candidates:
267
+ return _pick3(scoped.candidates, node, call_site, lookup, rel, scoped.chain)
268
+ return None, "name", []
269
+
162
270
  if alias_target.startswith("[pip]/"):
163
271
  # third-party chain: requests.get → lazily create [pip]/requests::get
164
272
  pkg = alias_target.split("/", 1)[1]
165
273
  if "::" in pkg:
166
274
  # chain root is already a named import — current_app.logger.info
167
275
  # → [pip]/flask::current_app (the meaningful hop)
168
- return alias_target, "name"
276
+ return alias_target, "name", []
169
277
  member = registry.method_node(pkg, rest.split(".")[0])
170
- return (member["id"] if member else None), "name"
171
- if alias_target.startswith("file::"):
172
- return _pick(_walk_module(lookup, alias_target, rest),
173
- node, call_site, lookup, rel)
174
- return None, "name"
175
-
176
- # ── 3. plain name — global lookup, overload ladder on collision ──
177
- candidates = lookup.nodes_by_name.get(called)
178
- if candidates:
179
- return _pick(candidates, node, call_site, lookup, rel)
278
+ return (member["id"] if member else None), "name", []
180
279
 
181
- # ── 4. import alias — refine file targets to the actual symbol ──
280
+ if alias_target.startswith("file::"):
281
+ # 2b. member access through an import alias — resolve against the
282
+ # target module's export surface (follows re-export chains)
283
+ scoped = _walk_module(lookup, alias_target, rest, symbol_names.get(root))
284
+ if scoped.candidates:
285
+ return _pick3(scoped.candidates, node, call_site, lookup, rel, scoped.chain)
286
+ return None, "name", []
287
+
288
+ # ── 3. plain name ──
289
+ # 3a. declared in the caller's own file (exact, no guessing)
290
+ local = _find_all_in_file(lookup, rel, called)
291
+ if local:
292
+ return _pick3(local, node, call_site, lookup, rel, [])
293
+
294
+ # 3b. imported into this file → the target module's export surface
182
295
  alias_target = symbols.get(called)
183
296
  if alias_target:
184
297
  if alias_target.startswith("file::"):
185
- return _pick(_find_all_in_file(lookup, alias_target[len("file::"):], called),
186
- node, call_site, lookup, rel)
187
- return alias_target, "name" # "[pip]/..." or "path.py::Symbol"
188
- return None, "name"
298
+ scoped = _resolve_in_module(lookup, alias_target[len("file::"):], called)
299
+ if scoped.candidates:
300
+ return _pick3(scoped.candidates, node, call_site, lookup, rel, scoped.chain)
301
+ return None, "name", []
302
+ # direct node id ("path.py::Symbol" or a third-party id, e.g. decorators)
303
+ return alias_target, "name", []
304
+
305
+ # 3c. global name — overload ladder on collision
306
+ candidates = lookup.nodes_by_name.get(called)
307
+ if candidates:
308
+ return _pick3(candidates, node, call_site, lookup, rel, [])
309
+
310
+ # 3d. copied into this namespace by `from .x import *`
311
+ scoped = _resolve_in_module(lookup, rel, called)
312
+ if scoped.candidates:
313
+ return _pick3(scoped.candidates, node, call_site, lookup, rel, scoped.chain)
314
+
315
+ return None, "name", []
189
316
 
190
317
 
191
318
  def resolve_calls(lookup: LookupMaps, registry: ThirdPartyRegistry) -> list[dict]:
@@ -196,17 +323,30 @@ def resolve_calls(lookup: LookupMaps, registry: ThirdPartyRegistry) -> list[dict
196
323
  edges: list[dict] = []
197
324
 
198
325
  for node in lookup.node_by_id.values():
199
- if node["type"] not in ("FUNCTION", "METHOD"):
326
+ # FILE nodes are callers for module-scope execution (calls run at
327
+ # import time, attached by parser/__init__.py). TEST leaves excluded.
328
+ if node["type"] not in ("FUNCTION", "METHOD", "FILE"):
200
329
  continue
201
330
  metadata = node["metadata"]
202
331
  sites = metadata.get("callSites") or [{"name": c} for c in metadata.get("calls", [])]
203
332
  resolved: list[dict] = []
204
333
  for site in sites:
205
334
  called = site["name"]
206
- target, matched_by = _resolve(called, node, lookup, registry, site)
335
+ target, matched_by, chain = _resolve(called, node, lookup, registry, site)
207
336
  if target and target != node["id"]:
337
+ edge_meta = {"calledName": called, "matchedBy": matched_by}
338
+ origin = lookup.node_by_id.get(target, {})
339
+ origin_name = origin.get("name")
340
+ if origin_name and origin_name != called:
341
+ # what the graph edge actually points at (alias → origin)
342
+ edge_meta["resolvedName"] = origin_name
343
+ edge_meta["aliased"] = True
344
+ if chain and len(chain) > 1:
345
+ edge_meta["aliasChain"] = chain
346
+ if node["type"] == "FILE":
347
+ edge_meta["scope"] = "module"
208
348
  edges.append({"from": node["id"], "to": target, "type": "CALLS",
209
- "metadata": {"calledName": called, "matchedBy": matched_by}})
349
+ "metadata": edge_meta})
210
350
  resolved.append({"name": called, "nodeId": target})
211
351
  metadata["resolvedCalls"] = resolved
212
352