devlensio 1.2.0 → 1.4.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 (47) hide show
  1. package/dist/graph/edges/callEdges.js +77 -3
  2. package/dist/graph/edges/callEdges.test.js +1 -1
  3. package/dist/graph/edges/callOrder.test.d.ts +1 -0
  4. package/dist/graph/edges/callOrder.test.js +227 -0
  5. package/dist/graph/edges/propEdges.js +6 -1
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +1 -0
  8. package/dist/parser/callFlow.d.ts +15 -0
  9. package/dist/parser/callFlow.js +302 -0
  10. package/dist/parser/callOrder.test.d.ts +1 -0
  11. package/dist/parser/callOrder.test.js +407 -0
  12. package/dist/parser/extractors/components.js +18 -28
  13. package/dist/parser/extractors/functions.d.ts +12 -3
  14. package/dist/parser/extractors/functions.js +58 -36
  15. package/dist/parser/extractors/moduleScope.js +1 -1
  16. package/dist/parser/moduleScope.test.js +5 -3
  17. package/dist/summarizer/index.js +53 -4
  18. package/dist/summarizer/prompts.js +111 -50
  19. package/dist/summarizer/prompts.test.d.ts +1 -0
  20. package/dist/summarizer/prompts.test.js +112 -0
  21. package/dist/types.d.ts +63 -0
  22. package/dist/version.d.ts +1 -0
  23. package/dist/version.js +2 -0
  24. package/extractors/go/bin/darwin-amd64/devlens_go_extractor +0 -0
  25. package/extractors/go/bin/darwin-arm64/devlens_go_extractor +0 -0
  26. package/extractors/go/bin/linux-amd64/devlens_go_extractor +0 -0
  27. package/extractors/go/bin/linux-arm64/devlens_go_extractor +0 -0
  28. package/extractors/go/bin/windows-amd64/devlens_go_extractor.exe +0 -0
  29. package/extractors/go/calls.go +89 -10
  30. package/extractors/go/nodes.go +29 -11
  31. package/extractors/go/parser.go +311 -21
  32. package/extractors/java/devlens_java_extractor.jar +0 -0
  33. package/extractors/java/src/devlens/extractor/Extractor.java +36 -10
  34. package/extractors/java/src/devlens/extractor/Parser.java +387 -0
  35. package/extractors/java/src/devlens/extractor/edges/Calls.java +66 -10
  36. package/extractors/python/devlens_extractors_python/edges/calls.py +26 -3
  37. package/extractors/python/devlens_extractors_python/parser/__init__.py +4 -3
  38. package/extractors/python/devlens_extractors_python/parser/functions.py +199 -42
  39. package/extractors/rust/bin/darwin-amd64/devlens_rust_extractor +0 -0
  40. package/extractors/rust/bin/darwin-arm64/devlens_rust_extractor +0 -0
  41. package/extractors/rust/bin/linux-amd64/devlens_rust_extractor +0 -0
  42. package/extractors/rust/bin/linux-arm64/devlens_rust_extractor +0 -0
  43. package/extractors/rust/bin/windows-amd64/devlens_rust_extractor.exe +0 -0
  44. package/extractors/rust/src/calls.rs +82 -8
  45. package/extractors/rust/src/nodes.rs +29 -0
  46. package/extractors/rust/src/parser.rs +362 -27
  47. package/package.json +77 -73
@@ -8,7 +8,8 @@
8
8
  // same-package func; alias.member → symbol maps → local method/func or
9
9
  // [mod] lazy member (python's proven chain rule).
10
10
  //
11
- // metadata.calls (sorted unique strings) + metadata.resolvedCalls written on
11
+ // metadata.calls (sorted unique strings) + metadata.callSites (one record per
12
+ // occurrence) + metadata.resolvedCalls (first occurrence per target) written on
12
13
  // every caller node (contract compliance + LLM context).
13
14
 
14
15
  package main
@@ -21,10 +22,68 @@ import (
21
22
  "strings"
22
23
  )
23
24
 
25
+ // resolvedCall — one metadata.resolvedCalls entry, shaped exactly like ResolvedCall
26
+ // in src/types.ts: name is the DECLARATION name the edge points at, firstOrder is
27
+ // the first position that reached it, orders carries the repeat positions.
24
28
  type resolvedCall struct {
25
- Target string `json:"target"`
26
- Method string `json:"method,omitempty"`
27
- Class string `json:"class,omitempty"`
29
+ Name string `json:"name"`
30
+ NodeID string `json:"nodeId"`
31
+ FirstOrder int `json:"firstOrder,omitempty"`
32
+ Line int `json:"line,omitempty"`
33
+ Defer string `json:"defer,omitempty"`
34
+ DeferDetail string `json:"deferDetail,omitempty"`
35
+ Conc bool `json:"conc,omitempty"`
36
+ Repeat bool `json:"repeat,omitempty"`
37
+ Maybe bool `json:"maybe,omitempty"`
38
+ Orders []int `json:"orders,omitempty"`
39
+ }
40
+
41
+ // declaredName — the name of the node the edge points at, falling back to the
42
+ // call-site text when the target is not in the registry (third-party ids).
43
+ func declaredName(l *LookupMaps, target, fallback string) string {
44
+ if n, ok := l.NodeByID[target]; ok {
45
+ if s, ok := n["name"].(string); ok && s != "" {
46
+ return s
47
+ }
48
+ }
49
+ return fallback
50
+ }
51
+
52
+ func newResolvedCall(c CallSite, target, name string) resolvedCall {
53
+ return resolvedCall{
54
+ Name: name, NodeID: target,
55
+ FirstOrder: c.Order, Line: c.Line,
56
+ Defer: c.Defer, DeferDetail: c.DeferDetail,
57
+ Conc: c.Conc, Repeat: c.Repeat, Maybe: c.Maybe,
58
+ }
59
+ }
60
+
61
+ func sortedUniqueInts(in []int) []int {
62
+ seen := map[int]bool{}
63
+ out := make([]int, 0, len(in))
64
+ for _, v := range in {
65
+ if v > 0 && !seen[v] {
66
+ seen[v] = true
67
+ out = append(out, v)
68
+ }
69
+ }
70
+ sort.Ints(out)
71
+ return out
72
+ }
73
+
74
+ // attachRepeatOrders — fill orders[] only when a target was reached more than
75
+ // once, so a renderer never needs the edge mirror for repeated calls.
76
+ func attachRepeatOrders(details []resolvedCall, index map[string]int, orders map[string][]int) {
77
+ for target, list := range orders {
78
+ idx, ok := index[target]
79
+ if !ok {
80
+ continue
81
+ }
82
+ unique := sortedUniqueInts(list)
83
+ if len(unique) > 1 {
84
+ details[idx].Orders = unique
85
+ }
86
+ }
28
87
  }
29
88
 
30
89
  // detectCalls — CALLS edges for every non-test function/method body.
@@ -40,16 +99,26 @@ func detectCalls(pr *parsedRepo, ti *TypeInfo, l *LookupMaps, opts *Options) []m
40
99
  callerID := nodeIDForFunc(pf.RelPath, fn)
41
100
  callStrings := make([]string, 0, len(fn.Calls))
42
101
  var details []resolvedCall
102
+ resolvedIndex := map[string]int{}
103
+ ordersByTarget := map[string][]int{}
43
104
  for _, c := range fn.Calls {
44
105
  callStrings = append(callStrings, c.Str)
45
- target, method, class, e := resolveCall(c, fn, pf, info, l)
106
+ target, _, _, e := resolveCall(c, fn, pf, info, l)
46
107
  if e != nil {
47
108
  edges = append(edges, e)
48
109
  }
49
- if target != "" {
50
- details = append(details, resolvedCall{Target: target, Method: method, Class: class})
110
+ if target == "" {
111
+ continue
112
+ }
113
+ if c.Order > 0 {
114
+ ordersByTarget[target] = append(ordersByTarget[target], c.Order)
115
+ }
116
+ if _, seen := resolvedIndex[target]; !seen {
117
+ resolvedIndex[target] = len(details)
118
+ details = append(details, newResolvedCall(c, target, declaredName(l, target, c.Str)))
51
119
  }
52
120
  }
121
+ attachRepeatOrders(details, resolvedIndex, ordersByTarget)
53
122
  // metadata enrichment on the (already created) caller node
54
123
  if n, ok := l.NodeByID[callerID]; ok {
55
124
  m := n["metadata"].(map[string]any)
@@ -72,18 +141,28 @@ func detectCalls(pr *parsedRepo, ti *TypeInfo, l *LookupMaps, opts *Options) []m
72
141
  info := ti.Infos[pf.RelPath]
73
142
  callStrings := make([]string, 0, len(pf.FileCalls))
74
143
  var details []resolvedCall
144
+ resolvedIndex := map[string]int{}
145
+ ordersByTarget := map[string][]int{}
75
146
 
76
147
  for _, c := range pf.FileCalls {
77
148
  callStrings = append(callStrings, c.Str)
78
- target, method, class, e := resolveCallFor(c, pf.PkgPath, callerID, nil, pf, info, l)
149
+ target, _, _, e := resolveCallFor(c, pf.PkgPath, callerID, nil, pf, info, l)
79
150
  if e != nil {
80
151
  e["metadata"].(map[string]any)["scope"] = "module"
81
152
  edges = append(edges, e)
82
153
  }
83
- if target != "" {
84
- details = append(details, resolvedCall{Target: target, Method: method, Class: class})
154
+ if target == "" {
155
+ continue
156
+ }
157
+ if c.Order > 0 {
158
+ ordersByTarget[target] = append(ordersByTarget[target], c.Order)
159
+ }
160
+ if _, seen := resolvedIndex[target]; !seen {
161
+ resolvedIndex[target] = len(details)
162
+ details = append(details, newResolvedCall(c, target, declaredName(l, target, c.Str)))
85
163
  }
86
164
  }
165
+ attachRepeatOrders(details, resolvedIndex, ordersByTarget)
87
166
 
88
167
  if n, ok := l.NodeByID[callerID]; ok {
89
168
  m := n["metadata"].(map[string]any)
@@ -5,8 +5,6 @@
5
5
  package main
6
6
 
7
7
  import (
8
- "strconv"
9
- "strings"
10
8
  "unicode"
11
9
  "unicode/utf8"
12
10
  )
@@ -129,26 +127,46 @@ func nodeTypeForFunc(fn *ParsedFunc) string {
129
127
  return NodeFunction
130
128
  }
131
129
 
132
- // callSitesMeta — structured call-site records from the parse-time facts:
133
- // {name, argCount, argTypes, hasSpread?}. One record per (name, arity,
134
- // argTypes) triple; order follows the AST walk (deterministic).
130
+ // callSitesMeta — structured call-site records from the parse-time facts,
131
+ // ONE RECORD PER OCCURRENCE in source order (docs/CALL_ORDER.md §3.1), so
132
+ // `order` is the owner node's lexical call sequence. De-duplication lives in
133
+ // metadata.calls only (name-only, sorted) — never here.
135
134
  func callSitesMeta(calls []CallSite) []map[string]any {
136
135
  sites := make([]map[string]any, 0, len(calls))
137
- seen := map[string]bool{}
138
136
  for _, c := range calls {
139
- key := c.Str + "/" + strconv.Itoa(c.ArgCount) + "/" + strings.Join(c.ArgTypes, ",")
140
- if seen[key] {
141
- continue
142
- }
143
- seen[key] = true
144
137
  site := map[string]any{
145
138
  "name": c.Str,
146
139
  "argCount": c.ArgCount,
147
140
  "argTypes": c.ArgTypes,
141
+ "order": c.Order,
142
+ "line": c.Line,
143
+ "col": c.Col,
144
+ "scope": c.Scope,
148
145
  }
149
146
  if c.HasSpread {
150
147
  site["hasSpread"] = true
151
148
  }
149
+ if c.Nested > 0 {
150
+ site["nested"] = c.Nested
151
+ }
152
+ if c.Defer != "" {
153
+ site["defer"] = c.Defer
154
+ }
155
+ if c.DeferDetail != "" {
156
+ site["deferDetail"] = c.DeferDetail
157
+ }
158
+ if c.Conc {
159
+ site["conc"] = true
160
+ }
161
+ if c.Repeat {
162
+ site["repeat"] = true
163
+ }
164
+ if c.Maybe {
165
+ site["maybe"] = true
166
+ }
167
+ if c.EarlyExit {
168
+ site["earlyExit"] = true
169
+ }
152
170
  sites = append(sites, site)
153
171
  }
154
172
  return sites
@@ -29,8 +29,37 @@ type CallSite struct {
29
29
  ArgCount int // number of arguments at the call site
30
30
  ArgTypes []string // per-arg type tags (Go type vocabulary, "unknown" when not cheaply inferable)
31
31
  HasSpread bool // f(xs...) — variadic spread, runtime arity unknowable
32
+
33
+ // ── callOrder/v1 (docs/CALL_ORDER.md §3.1) — metadata only, never identity ──
34
+ Order int // 1-based lexical rank among the owner's recorded sites
35
+ Line int // 1-based line of the call expression
36
+ Col int // 0-based column, so two calls on one line stay distinct
37
+ Nested int // order of the enclosing call expression: outer(mid()) → 1
38
+ Scope string // scopeInline | scopeCallback | scopeNested | scopeModule
39
+ Defer string // deferTimer | deferEvent | deferOnReturn ("" = no evidence)
40
+ DeferDetail string // the scheduler symbol that produced Defer
41
+ Conc bool // go statement: may run alongside the frame
42
+ Repeat bool // loop body: may run 0..N times
43
+ Maybe bool // branch arm / block behind a terminating guard: may run 0 times
44
+ EarlyExit bool // Go has no `?` operator — always false; shared contract shape
32
45
  }
33
46
 
47
+ // CallSite.Scope values — the execution region a site belongs to.
48
+ const (
49
+ scopeInline = "inline"
50
+ scopeCallback = "callback"
51
+ scopeNested = "nested"
52
+ scopeModule = "module"
53
+ )
54
+
55
+ // CallSite.Defer values Go can evidence. The remaining contract kinds
56
+ // (promise/microtask/postRender/unknown) simply stay absent for Go.
57
+ const (
58
+ deferTimer = "timer"
59
+ deferEvent = "event"
60
+ deferOnReturn = "onReturn"
61
+ )
62
+
34
63
  type ParsedFunc struct {
35
64
  Name string
36
65
  IsMethod bool
@@ -273,7 +302,9 @@ func parseFile(repoPath, rel string, pkg *ParsedPackage, addError errFn) *Parsed
273
302
  continue
274
303
  }
275
304
  for _, val := range vs.Values {
276
- pf.FileCalls = append(pf.FileCalls, collectModuleCalls(val)...)
305
+ // ordinals continue the FILE node's sequence across every
306
+ // package-level var in the file
307
+ pf.FileCalls = append(pf.FileCalls, collectModuleCalls(fset, val, len(pf.FileCalls))...)
277
308
  }
278
309
  }
279
310
  case token.CONST:
@@ -358,7 +389,7 @@ func parseFuncDecl(fset *token.FileSet, src []byte, rel string, pkg *ParsedPacka
358
389
  fn.IsTest = true
359
390
  }
360
391
  if d.Body != nil {
361
- fn.Calls = collectCalls(d.Body)
392
+ fn.Calls = collectCalls(fset, d.Body)
362
393
  }
363
394
  return fn
364
395
  }
@@ -380,41 +411,300 @@ func receiverTypeName(e ast.Expr) string {
380
411
  return exprText(e)
381
412
  }
382
413
 
383
- // collectCalls — all CallExprs in a body (scope rule: closures inside the
384
- // body attribute to the enclosing function — Go has no nested func decls).
385
- func collectCalls(body ast.Node) []CallSite {
386
- var calls []CallSite
387
- ast.Inspect(body, func(n ast.Node) bool {
414
+ // ── call collection (the only call-site walk) ──
415
+
416
+ // collectCalls — every call site of one function body, ONE RECORD PER
417
+ // OCCURRENCE (docs/CALL_ORDER.md §2). The recorded SET is unchanged from the
418
+ // pre-callOrder extractor: the walk descends into FuncLit bodies because Go has
419
+ // no nested func decls — a closure's calls belong to the enclosing function.
420
+ func collectCalls(fset *token.FileSet, body ast.Node) []CallSite {
421
+ w := &callWalk{
422
+ fset: fset,
423
+ inherit: callCtx{scope: scopeInline},
424
+ guarded: markEarlyExitGuards(body),
425
+ chanCb: channelCallbackVars(body),
426
+ }
427
+ w.walk(body)
428
+ return w.calls
429
+ }
430
+
431
+ // collectModuleCalls — calls in a package-level VAR initializer (FILE node).
432
+ // Closures are NOT module scope: `var h = func() { svc.Get() }` runs svc.Get
433
+ // when h is invoked, so the walk does not descend into FuncLit. `base` is the
434
+ // number of ordinals the FILE node already used.
435
+ func collectModuleCalls(fset *token.FileSet, root ast.Node, base int) []CallSite {
436
+ w := &callWalk{
437
+ fset: fset,
438
+ base: base,
439
+ inherit: callCtx{scope: scopeModule},
440
+ skipLit: true,
441
+ }
442
+ w.walk(root)
443
+ return w.calls
444
+ }
445
+
446
+ // callWalk — one ast.Inspect pass whose pre-order = source order, so the
447
+ // ordinal a site receives is its lexical rank among the owner's recorded
448
+ // sites. Every entered node pushes a frame (the exit callback pops it), which
449
+ // is how the flags of the ancestors reach the site.
450
+ type callWalk struct {
451
+ fset *token.FileSet
452
+ base int // ordinals already used by the owner
453
+ inherit callCtx // context at the root
454
+ guarded map[ast.Node]bool // statements behind a terminating guard
455
+ chanCb map[string]string // func values received from a channel
456
+ skipLit bool // do not descend into FuncLit (module scope)
457
+ stack []callFrame
458
+ calls []CallSite
459
+ }
460
+
461
+ // callCtx — the flags an owner region imposes on every site inside it.
462
+ type callCtx struct {
463
+ scope string
464
+ conc bool
465
+ repeat bool
466
+ maybe bool
467
+ deferKind string
468
+ deferDetail string
469
+ }
470
+
471
+ type callFrame struct {
472
+ node ast.Node
473
+ funcLit bool // function-literal boundary: `nested` never crosses it
474
+ order int // ordinal of this frame when it is a recorded call site
475
+ ctx callCtx
476
+ }
477
+
478
+ func (w *callWalk) walk(root ast.Node) {
479
+ ast.Inspect(root, func(n ast.Node) bool {
480
+ if n == nil {
481
+ w.stack = w.stack[:len(w.stack)-1]
482
+ return false
483
+ }
484
+ if _, isLit := n.(*ast.FuncLit); isLit && w.skipLit {
485
+ return false // no frame pushed — ast.Inspect sends no exit callback
486
+ }
487
+ parent := w.parent()
488
+ ctx := w.narrow(w.ctx(), n, parent)
489
+ order := 0
388
490
  if call, ok := n.(*ast.CallExpr); ok {
389
491
  if site := callSiteFrom(call); site != nil {
390
- calls = append(calls, *site)
492
+ w.annotate(site, ctx)
493
+ w.calls = append(w.calls, *site)
494
+ order = site.Order
391
495
  }
392
496
  }
497
+ _, isLit := n.(*ast.FuncLit)
498
+ w.stack = append(w.stack, callFrame{node: n, funcLit: isLit, order: order, ctx: ctx})
393
499
  return true
394
500
  })
395
- return calls
396
501
  }
397
502
 
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
503
+ func (w *callWalk) parent() ast.Node {
504
+ if len(w.stack) == 0 {
505
+ return nil
506
+ }
507
+ return w.stack[len(w.stack)-1].node
508
+ }
509
+
510
+ func (w *callWalk) ctx() callCtx {
511
+ if len(w.stack) == 0 {
512
+ return w.inherit
513
+ }
514
+ return w.stack[len(w.stack)-1].ctx
515
+ }
516
+
517
+ // narrow — widen the inherited context for the subtree rooted at n. Only
518
+ // metadata changes here; the recorded set is decided by walk alone.
519
+ func (w *callWalk) narrow(ctx callCtx, n, parent ast.Node) callCtx {
520
+ switch n.(type) {
521
+ case *ast.FuncLit:
522
+ // a closure body: callback when registered, nested when declared
523
+ ctx.scope = funcLitScope(parent)
524
+ if kind, detail, ok := registrationScheduler(parent); ok {
525
+ ctx.deferKind, ctx.deferDetail = kind, detail
526
+ }
527
+ case *ast.GoStmt:
528
+ ctx.conc = true // go f() starts now — concurrent, NOT deferred
529
+ case *ast.DeferStmt:
530
+ ctx.deferKind, ctx.deferDetail = deferOnReturn, "defer"
531
+ case *ast.CaseClause, *ast.CommClause:
532
+ ctx.maybe = true // switch / type-switch / select arm
533
+ }
534
+ switch p := parent.(type) {
535
+ case *ast.IfStmt:
536
+ if p.Body == n || p.Else == n {
537
+ ctx.maybe = true // then arm, else arm, or an else-if chain
538
+ }
539
+ case *ast.RangeStmt:
540
+ if p.Body == n {
541
+ ctx.repeat = true // range body
542
+ }
543
+ case *ast.ForStmt:
544
+ if p.Body == n {
545
+ ctx.repeat = true // for body
546
+ }
547
+ }
548
+ if w.guarded[n] {
549
+ ctx.maybe = true // the rest of a block behind a terminating guard
550
+ }
551
+ return ctx
552
+ }
553
+
554
+ // annotate — write the callOrder/v1 fields of one recorded site.
555
+ func (w *callWalk) annotate(site *CallSite, ctx callCtx) {
556
+ pos := w.fset.Position(site.Pos)
557
+ site.Order = w.base + len(w.calls) + 1
558
+ site.Line = pos.Line
559
+ if pos.Column > 0 {
560
+ site.Col = pos.Column - 1
561
+ }
562
+ site.Scope = ctx.scope
563
+ if site.Scope == "" {
564
+ site.Scope = scopeInline
565
+ }
566
+ site.Conc = ctx.conc
567
+ site.Repeat = ctx.repeat
568
+ site.Maybe = ctx.maybe
569
+ site.Defer = ctx.deferKind
570
+ site.DeferDetail = ctx.deferDetail
571
+
572
+ // the nearest enclosing call expression, without crossing a closure: the
573
+ // calls of a callback passed to foo() are not evaluated as part of foo()
574
+ for i := len(w.stack) - 1; i >= 0; i-- {
575
+ f := w.stack[i]
576
+ if f.funcLit {
577
+ break
578
+ }
579
+ if f.order > 0 {
580
+ site.Nested = f.order
581
+ break
582
+ }
583
+ }
584
+
585
+ // a func value that arrived over a channel is registered externally
586
+ if site.Defer == "" && !site.IsSel {
587
+ if ch, ok := w.chanCb[site.Str]; ok {
588
+ site.Defer, site.DeferDetail = deferEvent, "<-"+ch
589
+ }
590
+ }
591
+ }
592
+
593
+ // funcLitScope — a closure body's scope: passed to a call (or dispatched by go
594
+ // / defer) is a callback; a variable initializer or any other position is a
595
+ // function declared inside the owner, i.e. "nested".
596
+ func funcLitScope(parent ast.Node) string {
597
+ switch parent.(type) {
598
+ case *ast.CallExpr, *ast.GoStmt, *ast.DeferStmt:
599
+ return scopeCallback
600
+ }
601
+ return scopeNested
602
+ }
603
+
604
+ // registrationScheduler — the call that receives this closure, when it is a
605
+ // scheduler the contract knows about (docs/CALL_ORDER.md §4.2).
606
+ func registrationScheduler(parent ast.Node) (string, string, bool) {
607
+ call, ok := parent.(*ast.CallExpr)
608
+ if !ok {
609
+ return "", "", false
610
+ }
611
+ text := exprText(call.Fun)
612
+ if text == "time.AfterFunc" || strings.HasSuffix(text, ".AfterFunc") {
613
+ return deferTimer, text, true
614
+ }
615
+ return "", "", false
616
+ }
617
+
618
+ // markEarlyExitGuards — the Go diamond `if err != nil { return }`: the rest of
619
+ // the block only runs when the guard did not fire, so its calls are `maybe`.
620
+ func markEarlyExitGuards(root ast.Node) map[ast.Node]bool {
621
+ guarded := map[ast.Node]bool{}
403
622
  ast.Inspect(root, func(n ast.Node) bool {
404
- if n == nil {
405
- return false
623
+ block, ok := n.(*ast.BlockStmt)
624
+ if !ok {
625
+ return true
406
626
  }
407
- if _, isFunc := n.(*ast.FuncLit); isFunc {
627
+ for i, stmt := range block.List {
628
+ if !terminatingGuard(stmt) {
629
+ continue
630
+ }
631
+ for _, rest := range block.List[i+1:] {
632
+ guarded[rest] = true
633
+ }
634
+ break
635
+ }
636
+ return true
637
+ })
638
+ return guarded
639
+ }
640
+
641
+ func terminatingGuard(stmt ast.Stmt) bool {
642
+ guard, ok := stmt.(*ast.IfStmt)
643
+ if !ok || guard.Else != nil {
644
+ return false
645
+ }
646
+ return terminates(guard.Body)
647
+ }
648
+
649
+ // terminates — the block always leaves the enclosing block (return, break,
650
+ // continue, goto, or a panic-style call).
651
+ func terminates(block *ast.BlockStmt) bool {
652
+ if block == nil || len(block.List) == 0 {
653
+ return false
654
+ }
655
+ switch last := block.List[len(block.List)-1].(type) {
656
+ case *ast.ReturnStmt, *ast.BranchStmt:
657
+ return true
658
+ case *ast.ExprStmt:
659
+ call, ok := last.X.(*ast.CallExpr)
660
+ if !ok {
408
661
  return false
409
662
  }
410
- if call, ok := n.(*ast.CallExpr); ok {
411
- if site := callSiteFrom(call); site != nil {
412
- calls = append(calls, *site)
663
+ name := exprText(call.Fun)
664
+ if i := strings.LastIndex(name, "."); i >= 0 {
665
+ name = name[i+1:]
666
+ }
667
+ switch name {
668
+ case "panic", "Fatal", "Fatalf", "Fatalln", "Exit", "Goexit":
669
+ return true
670
+ }
671
+ }
672
+ return false
673
+ }
674
+
675
+ // channelCallbackVars — `cb := <-cbCh`: a func value that arrives over a
676
+ // channel is triggered from outside the frame, so calling it is `defer:event`
677
+ // (the contract's "channel consumer", docs/CALL_ORDER.md §4.2).
678
+ func channelCallbackVars(root ast.Node) map[string]string {
679
+ vars := map[string]string{}
680
+ ast.Inspect(root, func(n ast.Node) bool {
681
+ switch s := n.(type) {
682
+ case *ast.AssignStmt:
683
+ if len(s.Lhs) == 1 && len(s.Rhs) == 1 {
684
+ addChannelVar(vars, s.Lhs[0], s.Rhs[0])
685
+ }
686
+ case *ast.ValueSpec:
687
+ for i, name := range s.Names {
688
+ if i < len(s.Values) {
689
+ addChannelVar(vars, name, s.Values[i])
690
+ }
413
691
  }
414
692
  }
415
693
  return true
416
694
  })
417
- return calls
695
+ return vars
696
+ }
697
+
698
+ func addChannelVar(vars map[string]string, target, value ast.Expr) {
699
+ id, ok := target.(*ast.Ident)
700
+ if !ok {
701
+ return
702
+ }
703
+ recv, ok := value.(*ast.UnaryExpr)
704
+ if !ok || recv.Op != token.ARROW {
705
+ return
706
+ }
707
+ vars[id.Name] = exprText(recv.X)
418
708
  }
419
709
 
420
710
  // callSiteFrom — one CallExpr → CallSite (nil for inline func-literal
@@ -285,17 +285,14 @@ public final class Extractor {
285
285
  }
286
286
 
287
287
  /**
288
- * Structured call-site records ({name, argCount, argTypes, hasSpread?})
289
- * mirroring metadata.callSites in the TypeScript engine. One record per
290
- * (name, argCount, argTypes) triple; first-seen order.
288
+ * Structured call-site records mirroring metadata.callSites in the
289
+ * TypeScript engine — ONE RECORD PER OCCURRENCE since callOrder/v1, so
290
+ * `order` is the node's lexical call sequence. No shape de-duplication and
291
+ * no reordering: the list is already in `order` (pre-order walk).
291
292
  */
292
293
  private static List<Map<String, Object>> callSitesOf(List<Parser.CallInfo> calls) {
293
- LinkedHashMap<String, Map<String, Object>> out = new LinkedHashMap<>();
294
+ List<Map<String, Object>> out = new ArrayList<>();
294
295
  for (Parser.CallInfo ci : calls) {
295
- String key = ci.name + "/" + ci.argCount + "/" + String.join(",", ci.argTypes);
296
- if (out.containsKey(key)) {
297
- continue;
298
- }
299
296
  Map<String, Object> cs = new LinkedHashMap<>();
300
297
  cs.put("name", ci.name);
301
298
  cs.put("argCount", ci.argCount);
@@ -303,8 +300,37 @@ public final class Extractor {
303
300
  if (ci.hasSpread) {
304
301
  cs.put("hasSpread", true);
305
302
  }
306
- out.put(key, cs);
303
+ cs.put("kind", "call"); // Java records no hooks
304
+ cs.put("order", ci.order);
305
+ cs.put("line", ci.line);
306
+ cs.put("col", ci.col);
307
+ if (ci.nested != null) {
308
+ cs.put("nested", ci.nested);
309
+ }
310
+ cs.put("scope", ci.scope);
311
+ if (ci.awaited) {
312
+ cs.put("awaited", true);
313
+ }
314
+ if (ci.defer != null) {
315
+ cs.put("defer", ci.defer);
316
+ }
317
+ if (ci.deferDetail != null) {
318
+ cs.put("deferDetail", ci.deferDetail);
319
+ }
320
+ if (ci.conc) {
321
+ cs.put("conc", true);
322
+ }
323
+ if (ci.repeat) {
324
+ cs.put("repeat", true);
325
+ }
326
+ if (ci.maybe) {
327
+ cs.put("maybe", true);
328
+ }
329
+ if (ci.earlyExit) {
330
+ cs.put("earlyExit", true);
331
+ }
332
+ out.add(cs);
307
333
  }
308
- return new ArrayList<>(out.values());
334
+ return out;
309
335
  }
310
336
  }