callback-synthesizer.ts 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. /**
  2. * Callback / observer edge synthesis — Phase 1 + 2.
  3. *
  4. * Closes dynamic-dispatch holes where a dispatcher invokes callbacks registered
  5. * elsewhere. Two channel shapes:
  6. *
  7. * (1) Field-backed observer (Phase 1):
  8. * onUpdate(cb) { this.callbacks.add(cb); } // registrar
  9. * triggerUpdate() { for (cb of this.callbacks) cb(); } // dispatcher
  10. * scene.onUpdate(this.triggerRender) // registration
  11. * → synthesize triggerUpdate → triggerRender
  12. *
  13. * (2) String-keyed EventEmitter (Phase 2):
  14. * this.on('mount', function onmount(){...}) // registration
  15. * fn.emit('mount', this) // dispatch
  16. * → synthesize (method containing emit('mount')) → onmount
  17. *
  18. * Whole-graph pass after base resolution. High-precision/low-recall by design:
  19. * named callbacks only; field channels paired by file+field; EventEmitter
  20. * channels capped by event fan-out (generic names like 'error' skipped — they
  21. * need receiver-type matching, deferred to Phase 3). All synthesized edges are
  22. * tagged `provenance:'heuristic'`. See docs/design/callback-edge-synthesis.md.
  23. */
  24. import type { Edge, Node } from '../types';
  25. import type { QueryBuilder } from '../db/queries';
  26. import type { ResolutionContext } from './types';
  27. import { isGeneratedFile } from '../extraction/generated-detection';
  28. import { stripCommentsForRegex } from './strip-comments';
  29. const REGISTRAR_NAME = /^(on[A-Z]\w*|subscribe|addListener|addEventListener|register|watch|listen|addCallback)$/;
  30. const DISPATCHER_NAME = /(emit|trigger|notify|dispatch|fire|publish|flush)/i;
  31. const MAX_CALLBACKS_PER_CHANNEL = 40;
  32. const EVENT_FANOUT_CAP = 6; // skip events with more handlers/dispatchers than this (too generic without type info)
  33. const ON_RE = /\.(?:on|once|addListener)\(\s*['"]([^'"]+)['"]\s*,\s*(?:function\s+(\w+)|(?:this\.)?(\w+))/g;
  34. const EMIT_RE = /\.(?:emit|fire|dispatchEvent)\(\s*['"]([^'"]+)['"]/g;
  35. const SETSTATE_RE = /this\.setState\s*\(/;
  36. const FLUTTER_SETSTATE_RE = /\bsetState\s*\(/; // Flutter: setState((){…}) / this.setState
  37. const JSX_TAG_RE = /<([A-Z][A-Za-z0-9_]*)[\s/>]/g;
  38. const MAX_JSX_CHILDREN = 30;
  39. // Vue SFC templates: kebab-case child components (<el-button> → ElButton) and
  40. // event bindings (@click="fn" / v-on:click="fn"). PascalCase children (<VPNav/>)
  41. // are already caught by JSX_TAG_RE via the SFC component node.
  42. const VUE_KEBAB_RE = /<([a-z][a-z0-9]*(?:-[a-z0-9]+)+)[\s/>]/g;
  43. // PascalCase component tags — `<MediaCard ...>`, `<NavBar/>`. HTML elements are
  44. // lowercase, so an uppercase-initial tag is a component usage; built-ins
  45. // (`<NuxtLink>`, `<Transition>`) simply resolve to nothing and emit no edge.
  46. const VUE_PASCAL_RE = /<([A-Z][A-Za-z0-9]*)[\s/>]/g;
  47. const VUE_HANDLER_RE = /(?:@|v-on:)([a-zA-Z][\w-]*)(?:\.[\w]+)*\s*=\s*"([^"]+)"/g;
  48. // Composable/hook destructure: `const { close: closeSidebar } = useSidebarControl()`.
  49. // Captures the destructure body + the called composable; only `use*` calls qualify.
  50. const VUE_DESTRUCTURE_RE = /(?:const|let|var)\s*\{([^}]+)\}\s*=\s*(\w+)\s*\(/g;
  51. // Closure-collection dynamic dispatch (language-agnostic, Swift-first). A method
  52. // appends a closure to a collection property; another method iterates that
  53. // property *invoking each element* (`coll.forEach { $0() }` / `{ it() }`). The
  54. // element-invoke (`$0(` / `it(`) PROVES the collection holds closures, so pairing
  55. // a dispatcher to same-named registrars (`.append`/`.add`/`.push`/`.insert`,
  56. // incl. Swift `prop.write { $0.append }`) is high-precision. Cross-file/class by
  57. // design: Alamofire appends in `DataRequest.validate` but iterates in the base
  58. // `Request.didCompleteTask` — neither same-file nor same-class pairing reaches it.
  59. const CC_DISPATCH_RE = /(\w+)\.forEach\s*\{\s*(?:\$0|it)\s*\(/g;
  60. const CC_APPEND_WRITE_RE = /(\w+)\.write\s*\{\s*\$0(?:\.(\w+))?\.(?:append|add|push|insert)\s*\(/g;
  61. const CC_APPEND_DIRECT_RE = /(\w+)\.(?:append|add|push|insert)\s*\(/g;
  62. const CC_FANOUT_CAP = 8; // skip a field name with more dispatchers/registrars than this (too generic to pair confidently)
  63. function kebabToPascal(s: string): string {
  64. return s.split('-').map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join('');
  65. }
  66. /**
  67. * Nuxt auto-import name for a component, derived from its path UNDER `components/`:
  68. * `components/media/Card.vue` → `MediaCard`, `components/base/foo/Bar.vue` →
  69. * `BaseFooBar`. Each directory segment and the filename is PascalCased and
  70. * concatenated; a directory whose PascalCase name prefixes the next segment is
  71. * collapsed (Nuxt's de-dup: `base/BaseButton.vue` → `BaseButton`, not
  72. * `BaseBaseButton`). Returns null for a flat component (`components/NavBar.vue`)
  73. * — its node is already named by basename, so a direct tag match finds it.
  74. */
  75. function nuxtComponentName(filePath: string): string | null {
  76. const marker = filePath.lastIndexOf('components/');
  77. if (marker === -1) return null;
  78. const rel = filePath.slice(marker + 'components/'.length).replace(/\.(vue|ts|tsx|js|jsx)$/i, '');
  79. const segs = rel.split('/').filter(Boolean).map(kebabToPascal);
  80. if (segs.length < 2) return null;
  81. const out: string[] = [];
  82. for (const s of segs) {
  83. const prev = out[out.length - 1];
  84. if (prev && s.startsWith(prev)) out[out.length - 1] = s;
  85. else out.push(s);
  86. }
  87. return out.join('');
  88. }
  89. function sliceLines(content: string, startLine?: number, endLine?: number): string | null {
  90. if (!startLine || !endLine) return null;
  91. return content.split('\n').slice(startLine - 1, endLine).join('\n');
  92. }
  93. function registrarField(src: string): string | null {
  94. const m = src.match(/this\.(\w+)\.(?:add|push|set)\(/);
  95. return m ? m[1]! : null;
  96. }
  97. function dispatcherField(src: string): string | null {
  98. const forOf = src.match(/\bof\s+(?:Array\.from\(\s*)?this\.(\w+)/);
  99. if (forOf && /\b\w+\s*\(/.test(src)) return forOf[1]!;
  100. const forEach = src.match(/this\.(\w+)\.forEach\(/);
  101. if (forEach) return forEach[1]!;
  102. return null;
  103. }
  104. const FN_KINDS = new Set(['method', 'function', 'component']);
  105. /** Innermost function/method node whose line range contains `line`. */
  106. function enclosingFn(nodesInFile: Node[], line: number): Node | null {
  107. let best: Node | null = null;
  108. for (const n of nodesInFile) {
  109. if (!FN_KINDS.has(n.kind)) continue;
  110. const end = n.endLine ?? n.startLine;
  111. if (n.startLine <= line && end >= line) {
  112. if (!best || n.startLine >= best.startLine) best = n; // prefer the tightest (latest-starting) encloser
  113. }
  114. }
  115. return best;
  116. }
  117. /**
  118. * Stream method + function nodes lazily. The synthesizers only scan-and-filter
  119. * down to a tiny matched subset, so materializing every function/method (which
  120. * is gigabytes on a symbol-dense project) just to iterate it once is what OOM'd
  121. * #610. Iterating keeps memory O(1) in the node count.
  122. */
  123. function* methodAndFunctionNodes(queries: QueryBuilder): IterableIterator<Node> {
  124. yield* queries.iterateNodesByKind('method');
  125. yield* queries.iterateNodesByKind('function');
  126. }
  127. /** Phase 1: field-backed observer channels (registrar/dispatcher share a store). */
  128. function fieldChannelEdges(queries: QueryBuilder, ctx: ResolutionContext): Edge[] {
  129. const registrars: Array<{ node: Node; field: string }> = [];
  130. const dispatchers: Array<{ node: Node; field: string }> = [];
  131. for (const m of methodAndFunctionNodes(queries)) {
  132. const isReg = REGISTRAR_NAME.test(m.name);
  133. const isDisp = DISPATCHER_NAME.test(m.name);
  134. if (!isReg && !isDisp) continue;
  135. const content = ctx.readFile(m.filePath);
  136. const src = content && sliceLines(content, m.startLine, m.endLine);
  137. if (!src) continue;
  138. if (isReg) { const f = registrarField(src); if (f) registrars.push({ node: m, field: f }); }
  139. if (isDisp) { const f = dispatcherField(src); if (f) dispatchers.push({ node: m, field: f }); }
  140. }
  141. const edges: Edge[] = [];
  142. const seen = new Set<string>();
  143. for (const reg of registrars) {
  144. const chDispatchers = dispatchers.filter(
  145. (d) => d.node.filePath === reg.node.filePath && d.field === reg.field
  146. );
  147. if (chDispatchers.length === 0) continue;
  148. const argRe = new RegExp(`${reg.node.name}\\s*\\(\\s*(?:this\\.)?(\\w+)`);
  149. let added = 0;
  150. for (const e of queries.getIncomingEdges(reg.node.id, ['calls'])) {
  151. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  152. if (!e.line) continue;
  153. const caller = queries.getNodeById(e.source);
  154. if (!caller) continue;
  155. const line = ctx.readFile(caller.filePath)?.split('\n')[e.line - 1];
  156. const am = line?.match(argRe);
  157. if (!am) continue;
  158. const fn = ctx.getNodesByName(am[1]!).find((n) => n.kind === 'method' || n.kind === 'function');
  159. if (!fn) continue;
  160. for (const disp of chDispatchers) {
  161. if (disp.node.id === fn.id) continue;
  162. const key = `${disp.node.id}>${fn.id}`;
  163. if (seen.has(key)) continue;
  164. seen.add(key);
  165. edges.push({
  166. source: disp.node.id, target: fn.id, kind: 'calls', line: disp.node.startLine,
  167. provenance: 'heuristic',
  168. metadata: {
  169. synthesizedBy: 'callback', via: reg.node.name, field: reg.field,
  170. // Where the callback was wired up (`scene.onUpdate(this.triggerRender)`).
  171. // This is the #1 thing an agent reads/greps to explain the flow — surface
  172. // it so node/trace/context can show it without a callers() + Read round-trip.
  173. registeredAt: `${caller.filePath}:${e.line}`,
  174. },
  175. });
  176. added++;
  177. }
  178. }
  179. }
  180. return edges;
  181. }
  182. /**
  183. * Closure-collection dispatch: dispatcher iterates a closure-collection property
  184. * invoking each element; registrar appends a closure to the same-named property.
  185. * Emits dispatcher → registrar so a flow reaches the registration site (where the
  186. * appended closure's body — and its callers — live). High-precision: the
  187. * dispatcher's element-invoke is the gate (a `.forEach` that does NOT invoke its
  188. * element is ignored), so a repo with no closure-collection dispatch yields zero
  189. * edges regardless of how many `.append`/`.push` sites it has.
  190. *
  191. * Pairs globally by field name (cross-file/class is required — see Alamofire's
  192. * base-class `Request.didCompleteTask` iterating `validators` appended by the
  193. * subclass `DataRequest.validate`), bounded by a fan-out cap so a generic field
  194. * name shared across unrelated classes can't fan out into noise.
  195. */
  196. function closureCollectionEdges(queries: QueryBuilder, ctx: ResolutionContext): Edge[] {
  197. const dispatchers = new Map<string, Array<{ node: Node; line: number }>>(); // field → dispatcher methods + forEach line
  198. const registrars = new Map<string, Array<{ node: Node; line: number }>>(); // field → registrar methods + append line
  199. const addReg = (field: string | undefined, node: Node, absLine: number) => {
  200. if (!field || /^\d+$/.test(field)) return; // `$0.append` mis-captures the `0`; the write-RE owns that field
  201. const arr = registrars.get(field) ?? [];
  202. if (!arr.some((r) => r.node.id === node.id)) arr.push({ node, line: absLine });
  203. registrars.set(field, arr);
  204. };
  205. for (const m of methodAndFunctionNodes(queries)) {
  206. const content = ctx.readFile(m.filePath);
  207. const src = content && sliceLines(content, m.startLine, m.endLine);
  208. if (!src) continue;
  209. const hasForEach = src.includes('.forEach');
  210. const hasAppend = src.includes('.append(') || src.includes('.add(') || src.includes('.push(') || src.includes('.insert(');
  211. if (!hasForEach && !hasAppend) continue;
  212. const lineAt = (idx: number) => (m.startLine ?? 1) + src.slice(0, idx).split('\n').length - 1;
  213. if (hasForEach) {
  214. CC_DISPATCH_RE.lastIndex = 0;
  215. let d: RegExpExecArray | null;
  216. while ((d = CC_DISPATCH_RE.exec(src))) {
  217. const arr = dispatchers.get(d[1]!) ?? [];
  218. if (!arr.some((n) => n.node.id === m.id)) arr.push({ node: m, line: lineAt(d.index) });
  219. dispatchers.set(d[1]!, arr);
  220. }
  221. }
  222. if (hasAppend) {
  223. CC_APPEND_WRITE_RE.lastIndex = 0;
  224. let w: RegExpExecArray | null;
  225. while ((w = CC_APPEND_WRITE_RE.exec(src))) addReg(w[2] || w[1], m, lineAt(w.index)); // nested `$0.streams` else the `.write` receiver
  226. CC_APPEND_DIRECT_RE.lastIndex = 0;
  227. let a: RegExpExecArray | null;
  228. while ((a = CC_APPEND_DIRECT_RE.exec(src))) addReg(a[1], m, lineAt(a.index));
  229. }
  230. }
  231. const edges: Edge[] = [];
  232. const seen = new Set<string>();
  233. for (const [field, disps] of dispatchers) {
  234. const regs = registrars.get(field);
  235. if (!regs || regs.length === 0) continue;
  236. if (disps.length > CC_FANOUT_CAP || regs.length > CC_FANOUT_CAP) continue; // generic field — can't pair confidently
  237. for (const disp of disps) for (const reg of regs) {
  238. if (disp.node.id === reg.node.id) continue;
  239. const key = `${disp.node.id}>${reg.node.id}`;
  240. if (seen.has(key)) continue;
  241. seen.add(key);
  242. edges.push({
  243. source: disp.node.id, target: reg.node.id, kind: 'calls', line: disp.line,
  244. provenance: 'heuristic',
  245. metadata: { synthesizedBy: 'closure-collection', field, registeredAt: `${reg.node.filePath}:${reg.line}` },
  246. });
  247. }
  248. }
  249. return edges;
  250. }
  251. /** Phase 2: string-keyed EventEmitter channels (on('e', fn) ↔ emit('e')). */
  252. function eventEmitterEdges(ctx: ResolutionContext): Edge[] {
  253. const emitsByEvent = new Map<string, Set<string>>(); // event → dispatcher node ids
  254. const handlersByEvent = new Map<string, Map<string, string>>(); // event → handler id → registration site (file:line)
  255. for (const file of ctx.getAllFiles()) {
  256. const content = ctx.readFile(file);
  257. if (!content) continue;
  258. const hasEmit = content.includes('.emit(') || content.includes('.fire(') || content.includes('.dispatchEvent(');
  259. const hasOn = content.includes('.on(') || content.includes('.once(') || content.includes('.addListener(');
  260. if (!hasEmit && !hasOn) continue;
  261. const nodesInFile = ctx.getNodesInFile(file);
  262. const lineOf = (idx: number) => content.slice(0, idx).split('\n').length;
  263. if (hasEmit) {
  264. EMIT_RE.lastIndex = 0;
  265. let m: RegExpExecArray | null;
  266. while ((m = EMIT_RE.exec(content))) {
  267. const disp = enclosingFn(nodesInFile, lineOf(m.index));
  268. if (!disp) continue;
  269. const set = emitsByEvent.get(m[1]!) ?? new Set<string>();
  270. set.add(disp.id); emitsByEvent.set(m[1]!, set);
  271. }
  272. }
  273. if (hasOn) {
  274. ON_RE.lastIndex = 0;
  275. let m: RegExpExecArray | null;
  276. while ((m = ON_RE.exec(content))) {
  277. const handlerName = m[2] || m[3];
  278. if (!handlerName) continue;
  279. const handler = ctx.getNodesByName(handlerName).find((n) => n.kind === 'function' || n.kind === 'method');
  280. if (!handler) continue;
  281. const map = handlersByEvent.get(m[1]!) ?? new Map<string, string>();
  282. map.set(handler.id, `${file}:${lineOf(m.index)}`); handlersByEvent.set(m[1]!, map);
  283. }
  284. }
  285. }
  286. const edges: Edge[] = [];
  287. const seen = new Set<string>();
  288. for (const [event, dispatchers] of emitsByEvent) {
  289. const handlers = handlersByEvent.get(event);
  290. if (!handlers) continue;
  291. // Precision guard: a generic event name with many handlers/dispatchers can't
  292. // be matched without receiver-type info (Phase 3) — skip rather than over-link.
  293. if (dispatchers.size > EVENT_FANOUT_CAP || handlers.size > EVENT_FANOUT_CAP) continue;
  294. for (const d of dispatchers) for (const [h, registeredAt] of handlers) {
  295. if (d === h) continue;
  296. const key = `${d}>${h}`;
  297. if (seen.has(key)) continue;
  298. seen.add(key);
  299. edges.push({ source: d, target: h, kind: 'calls', provenance: 'heuristic', metadata: { synthesizedBy: 'event-emitter', event, registeredAt } });
  300. }
  301. }
  302. return edges;
  303. }
  304. /**
  305. * Phase 4: React class-component re-render. `this.setState(...)` re-runs the
  306. * component's `render()`, but that hop is React-internal — no static edge — so a
  307. * flow like "mutation → setState → canvas repaint" dead-ends at setState even
  308. * though `render → getRenderableElements → …` is fully call-connected after it.
  309. * Bridge it: for each class that has a `render` method, link every sibling method
  310. * whose body calls `this.setState(` → `render`. The setState gate keeps this to
  311. * React class components (a non-React class with a `render` method won't call
  312. * `this.setState`). Over-approximation (all setState methods reach render) is
  313. * accepted — it's reachability-correct, like the callback channels.
  314. */
  315. function reactRenderEdges(queries: QueryBuilder, ctx: ResolutionContext): Edge[] {
  316. const edges: Edge[] = [];
  317. const seen = new Set<string>();
  318. for (const cls of queries.getNodesByKind('class')) {
  319. const children = queries.getOutgoingEdges(cls.id, ['contains'])
  320. .map((e) => queries.getNodeById(e.target))
  321. .filter((n): n is Node => !!n && n.kind === 'method');
  322. const render = children.find((n) => n.name === 'render');
  323. if (!render) continue;
  324. let added = 0;
  325. for (const m of children) {
  326. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  327. if (m.id === render.id) continue;
  328. const content = ctx.readFile(m.filePath);
  329. const src = content && sliceLines(content, m.startLine, m.endLine);
  330. if (!src || !SETSTATE_RE.test(src)) continue;
  331. const key = `${m.id}>${render.id}`;
  332. if (seen.has(key)) continue;
  333. seen.add(key);
  334. edges.push({
  335. source: m.id, target: render.id, kind: 'calls', line: m.startLine,
  336. provenance: 'heuristic',
  337. metadata: { synthesizedBy: 'react-render', via: 'setState', registeredAt: `${render.filePath}:${render.startLine}` },
  338. });
  339. added++;
  340. }
  341. }
  342. return edges;
  343. }
  344. /**
  345. * Phase 4b: Flutter setState → build (the Dart analog of react-render). In a
  346. * StatefulWidget's State class, `setState(() {…})` re-runs `build(context)`, but
  347. * that hop is framework-internal (Flutter calls build), so a flow like
  348. * "onPressed → _increment → setState → rebuilt UI" dead-ends at setState. Bridge
  349. * it: for each Dart class with a `build` method, link every sibling method whose
  350. * body calls `setState(` → `build`. The setState gate + `.dart` file keep this to
  351. * Flutter State classes. Over-approximation accepted (reachability-correct).
  352. */
  353. function flutterBuildEdges(queries: QueryBuilder, ctx: ResolutionContext): Edge[] {
  354. const edges: Edge[] = [];
  355. const seen = new Set<string>();
  356. for (const cls of queries.getNodesByKind('class')) {
  357. const children = queries.getOutgoingEdges(cls.id, ['contains'])
  358. .map((e) => queries.getNodeById(e.target))
  359. .filter((n): n is Node => !!n && n.kind === 'method');
  360. const build = children.find((n) => n.name === 'build');
  361. if (!build || !build.filePath.endsWith('.dart')) continue;
  362. let added = 0;
  363. for (const m of children) {
  364. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  365. if (m.id === build.id) continue;
  366. const content = ctx.readFile(m.filePath);
  367. const src = content && sliceLines(content, m.startLine, m.endLine);
  368. if (!src || !FLUTTER_SETSTATE_RE.test(src)) continue;
  369. const key = `${m.id}>${build.id}`;
  370. if (seen.has(key)) continue;
  371. seen.add(key);
  372. edges.push({
  373. source: m.id, target: build.id, kind: 'calls', line: m.startLine,
  374. provenance: 'heuristic',
  375. metadata: { synthesizedBy: 'flutter-build', via: 'setState', registeredAt: `${build.filePath}:${build.startLine}` },
  376. });
  377. added++;
  378. }
  379. }
  380. return edges;
  381. }
  382. /**
  383. * Phase 4c: C++ virtual override. A call through a base/interface pointer
  384. * (`db->Get(...)`, `iter->Next()`) dispatches at runtime to a subclass override,
  385. * but that hop is a vtable indirection — no static call edge — so a flow stops at
  386. * the abstract base method. Bridge it like react-render: for each C++ class that
  387. * `extends` a base, link each base method → the subclass method of the same name
  388. * (the override), so trace/callees from the interface method reach the
  389. * implementation(s). Over-approximation accepted (reachability-correct); capped
  390. * per class and gated to C++ to avoid touching other languages' dispatch.
  391. */
  392. function cppOverrideEdges(queries: QueryBuilder): Edge[] {
  393. const edges: Edge[] = [];
  394. const seen = new Set<string>();
  395. const methodsOf = (classId: string): Node[] =>
  396. queries
  397. .getOutgoingEdges(classId, ['contains'])
  398. .map((e) => queries.getNodeById(e.target))
  399. .filter((n): n is Node => !!n && n.kind === 'method');
  400. for (const cls of queries.getNodesByKind('class')) {
  401. const subMethods = methodsOf(cls.id).filter((n) => n.language === 'cpp');
  402. if (subMethods.length === 0) continue;
  403. for (const ext of queries.getOutgoingEdges(cls.id, ['extends'])) {
  404. const base = queries.getNodeById(ext.target);
  405. if (!base || base.language !== 'cpp' || base.id === cls.id) continue;
  406. const baseMethods = new Map(methodsOf(base.id).map((m) => [m.name, m]));
  407. let added = 0;
  408. for (const m of subMethods) {
  409. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  410. const bm = baseMethods.get(m.name);
  411. if (!bm || bm.id === m.id) continue;
  412. const key = `${bm.id}>${m.id}`;
  413. if (seen.has(key)) continue;
  414. seen.add(key);
  415. edges.push({
  416. source: bm.id,
  417. target: m.id,
  418. kind: 'calls',
  419. line: bm.startLine,
  420. provenance: 'heuristic',
  421. metadata: { synthesizedBy: 'cpp-override', via: m.name, registeredAt: `${m.filePath}:${m.startLine}` },
  422. });
  423. added++;
  424. }
  425. }
  426. }
  427. return edges;
  428. }
  429. /**
  430. * Phase 5.5: interface / abstract dispatch (Java, Kotlin). A call through an
  431. * injected interface (`@Autowired FooService svc; svc.list()`) or an abstract
  432. * base dispatches at runtime to the implementing class's override — a vtable
  433. * indirection with no static call edge — so a request→service flow stops at the
  434. * interface method. Bridge it like cpp-override: for each class that
  435. * `implements` an interface (or `extends` an abstract base), link each
  436. * base/interface method → the class's same-name method (the override) so
  437. * trace/callees reach the implementation. Over-approximation accepted
  438. * (reachability-correct); capped per class, gated to JVM languages.
  439. */
  440. // Languages whose static `implements`/`extends` edges should bridge an
  441. // interface (or abstract base) method to the matching concrete-class method.
  442. // The set is "languages with explicit nominal subtyping and a single class
  443. // kind that holds methods" — i.e. the shape this loop expects. Swift and
  444. // Scala fit shape-wise (Swift `protocol`/`class`, Scala `trait`/`class`)
  445. // and are added below; their concrete-side nodes can be a `struct` (Swift)
  446. // or an `object` (Scala) so the loop also iterates those kinds.
  447. const IFACE_OVERRIDE_LANGS = new Set([
  448. 'java', 'kotlin', 'csharp', 'typescript', 'javascript', 'swift', 'scala', 'go', 'rust',
  449. ]);
  450. /**
  451. * Go implicit interface satisfaction (#584). Go has no `implements` keyword — a
  452. * struct satisfies an interface structurally when its method set covers the
  453. * interface's. Synthesize the missing `implements` edge (struct → interface) by
  454. * matching method-NAME sets, so impl-navigation works and the interface-dispatch
  455. * bridge ({@link interfaceOverrideEdges}, now 'go'-enabled) can link an interface
  456. * method call to the concrete overrides.
  457. *
  458. * Name-only matching (signatures ignored) — over-approximation accepted, in line
  459. * with the other dispatch synthesizers; capped per interface. Empty interfaces
  460. * (`any`) are skipped so they don't match every struct.
  461. */
  462. function goImplementsEdges(queries: QueryBuilder): Edge[] {
  463. const edges: Edge[] = [];
  464. const seen = new Set<string>();
  465. const methodNameSet = (id: string): Set<string> =>
  466. new Set(
  467. queries
  468. .getOutgoingEdges(id, ['contains'])
  469. .map((e) => queries.getNodeById(e.target))
  470. .filter((n): n is Node => !!n && n.kind === 'method')
  471. .map((n) => n.name),
  472. );
  473. const goStructs = queries.getNodesByKind('struct').filter((s) => s.language === 'go');
  474. const structMethods = new Map<string, Set<string>>();
  475. for (const s of goStructs) structMethods.set(s.id, methodNameSet(s.id));
  476. for (const iface of queries.getNodesByKind('interface')) {
  477. if (iface.language !== 'go') continue;
  478. const want = methodNameSet(iface.id);
  479. if (want.size === 0) continue; // empty interface (`any`) — would match everything
  480. let added = 0;
  481. for (const s of goStructs) {
  482. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  483. const have = structMethods.get(s.id);
  484. if (!have || have.size < want.size) continue;
  485. let all = true;
  486. for (const m of want) {
  487. if (!have.has(m)) { all = false; break; }
  488. }
  489. if (!all) continue;
  490. const key = `${s.id}>${iface.id}`;
  491. if (seen.has(key)) continue;
  492. seen.add(key);
  493. edges.push({
  494. source: s.id,
  495. target: iface.id,
  496. kind: 'implements',
  497. line: s.startLine,
  498. provenance: 'heuristic',
  499. metadata: { synthesizedBy: 'go-implements', via: iface.name, registeredAt: `${s.filePath}:${s.startLine}` },
  500. });
  501. added++;
  502. }
  503. }
  504. return edges;
  505. }
  506. /**
  507. * Kotlin Multiplatform `expect`/`actual` linking. A `common` source set declares
  508. * `expect fun foo()` / `expect class Bar`; each platform source set (jvm, native,
  509. * js, …) provides an `actual` implementation with the IDENTICAL fully-qualified
  510. * name in a different file. Callers in common code resolve to the `expect`
  511. * declaration, so every `actual` impl ends up with zero dependents — invisible to
  512. * impact/affected even though editing it can break every caller of the API.
  513. *
  514. * Synthesize a `calls` edge from the common declaration to each platform `actual`
  515. * (mirroring the interface-impl bridge: abstract → concrete), so editing a
  516. * platform impl surfaces the common `expect` and its callers, and the impl file
  517. * participates in the graph.
  518. *
  519. * `expect`/`actual` are captured onto the node's `decorators` list at extraction
  520. * (kotlin.ts `extractModifiers`). Members of an `expect class` are NOT themselves
  521. * keyword-marked, so the declaration side is matched as the same-FQN, same-kind
  522. * node that is NOT marked `actual`. Requiring an `actual`-marked counterpart also
  523. * gates out plain cross-file overloads (neither side is marked).
  524. */
  525. // Kinds that an `expect`/`actual` pair may legitimately straddle. `expect class`
  526. // is routinely fulfilled by an `actual typealias` (e.g. `actual typealias
  527. // CancellationException = …`, `actual typealias SchedulerTask = Task`), so a
  528. // strict kind match would miss those one-line alias files. Same-FQN + the
  529. // `actual` marker already gates out unrelated symbols, so widening to the
  530. // type-like kinds is safe.
  531. const KMP_TYPE_KINDS = new Set(['class', 'interface', 'struct', 'enum', 'type_alias']);
  532. function kmpKindsCompatible(a: string, b: string): boolean {
  533. return a === b || (KMP_TYPE_KINDS.has(a) && KMP_TYPE_KINDS.has(b));
  534. }
  535. function kotlinExpectActualEdges(queries: QueryBuilder): Edge[] {
  536. const edges: Edge[] = [];
  537. const seen = new Set<string>();
  538. const actuals = queries
  539. .getAllNodes()
  540. .filter((n) => n.language === 'kotlin' && !!n.decorators?.includes('actual'));
  541. for (const act of actuals) {
  542. let added = 0;
  543. for (const cand of queries.getNodesByQualifiedNameExact(act.qualifiedName)) {
  544. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  545. // The declaration side: same FQN + compatible kind, a different file, NOT
  546. // itself an `actual` (that would be a sibling platform impl, not the decl).
  547. if (cand.language !== 'kotlin' || cand.id === act.id) continue;
  548. if (!kmpKindsCompatible(cand.kind, act.kind) || cand.filePath === act.filePath) continue;
  549. if (cand.decorators?.includes('actual')) continue;
  550. const key = `${cand.id}>${act.id}`;
  551. if (seen.has(key)) continue;
  552. seen.add(key);
  553. edges.push({
  554. source: cand.id,
  555. target: act.id,
  556. kind: 'calls',
  557. line: cand.startLine,
  558. provenance: 'heuristic',
  559. metadata: {
  560. synthesizedBy: 'kotlin-expect-actual',
  561. via: act.name,
  562. registeredAt: `${act.filePath}:${act.startLine}`,
  563. },
  564. });
  565. added++;
  566. }
  567. }
  568. return edges;
  569. }
  570. function interfaceOverrideEdges(queries: QueryBuilder): Edge[] {
  571. const edges: Edge[] = [];
  572. const seen = new Set<string>();
  573. const methodsOf = (classId: string): Node[] =>
  574. queries
  575. .getOutgoingEdges(classId, ['contains'])
  576. .map((e) => queries.getNodeById(e.target))
  577. .filter((n): n is Node => !!n && n.kind === 'method');
  578. // Concrete-side kinds vary by language: `class` covers Java / Kotlin /
  579. // C# / TS / Swift-classes / Scala-classes; `struct` covers Swift value
  580. // types that conform to protocols. Iterate both.
  581. const concreteKinds = ['class', 'struct'] as const;
  582. for (const kind of concreteKinds) {
  583. for (const cls of queries.getNodesByKind(kind)) {
  584. const implMethods = methodsOf(cls.id).filter((n) => IFACE_OVERRIDE_LANGS.has(n.language));
  585. if (implMethods.length === 0) continue;
  586. for (const sup of queries.getOutgoingEdges(cls.id, ['implements', 'extends'])) {
  587. const base = queries.getNodeById(sup.target);
  588. if (!base || !IFACE_OVERRIDE_LANGS.has(base.language) || base.id === cls.id) continue;
  589. // Group impl methods by name to handle OVERLOADS: an interface `list()` and
  590. // `list(params)` are distinct nodes and a call may resolve to either, so
  591. // link every base overload → every same-name impl overload (keying by name
  592. // alone would drop all but one and miss the resolved overload).
  593. const implByName = new Map<string, Node[]>();
  594. for (const m of implMethods) {
  595. const arr = implByName.get(m.name);
  596. if (arr) arr.push(m); else implByName.set(m.name, [m]);
  597. }
  598. let added = 0;
  599. for (const bm of methodsOf(base.id)) {
  600. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  601. for (const m of implByName.get(bm.name) ?? []) {
  602. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  603. if (bm.id === m.id) continue;
  604. const key = `${bm.id}>${m.id}`;
  605. if (seen.has(key)) continue;
  606. seen.add(key);
  607. edges.push({
  608. source: bm.id,
  609. target: m.id,
  610. kind: 'calls',
  611. line: bm.startLine,
  612. provenance: 'heuristic',
  613. metadata: { synthesizedBy: 'interface-impl', via: m.name, registeredAt: `${m.filePath}:${m.startLine}` },
  614. });
  615. added++;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. return edges;
  622. }
  623. /**
  624. * Go gRPC stub → impl bridge. The protoc-gen-go-grpc codegen emits an
  625. * `UnimplementedXxxServer` struct in `*_grpc.pb.go` carrying one method
  626. * per service RPC; the real handler is a hand-written struct in another
  627. * file (`x/bank/keeper/msg_server.go::msgServer.Send` in cosmos-sdk).
  628. * Go's structural typing means no `implements` edge exists for our
  629. * resolver to follow, so `trace("Send","SendCoins")` lands on the
  630. * empty stub and reports "no path" (validated empirically — the cosmos
  631. * Q1 r1 trace failure that drove this work).
  632. *
  633. * Bridge: for each `UnimplementedXxxServer` whose RPC-method names are
  634. * a SUBSET of some other Go struct's method names, emit `calls` edges
  635. * `stub.method → impl.method` (paired by name). Excludes the gRPC
  636. * internal markers `mustEmbedUnimplementedXxxServer` and
  637. * `testEmbeddedByValue`, and skips candidate impls that themselves
  638. * live in a generated file (their `xxxClient` / sibling stubs would
  639. * otherwise look like impls).
  640. *
  641. * Multiple candidates is allowed and capped at MAX_CALLBACKS_PER_CHANNEL —
  642. * a service often has both a production impl and one or more test
  643. * mocks; linking to all preserves trace utility without false-favoring.
  644. *
  645. * Provenance: `heuristic`, `synthesizedBy: 'go-grpc-stub-impl'`. The
  646. * stub's source line is the wiring site shown in the trace trail.
  647. */
  648. function goGrpcStubImplEdges(queries: QueryBuilder): Edge[] {
  649. const edges: Edge[] = [];
  650. const seen = new Set<string>();
  651. const STUB_RE = /^Unimplemented.*Server$/;
  652. // gRPC internal-helper methods that appear on every Unimplemented*Server;
  653. // not part of the service contract, so exclude when computing the RPC-method
  654. // signature used to match impls.
  655. const isInternalMarker = (n: string) => n.startsWith('mustEmbed') || n === 'testEmbeddedByValue';
  656. // Methods directly contained by each Go struct, name-only. Built once.
  657. const methodNamesByStruct = new Map<string, Set<string>>();
  658. const methodNodesByStruct = new Map<string, Node[]>();
  659. const goStructs: Node[] = [];
  660. for (const s of queries.getNodesByKind('struct')) {
  661. if (s.language !== 'go') continue;
  662. goStructs.push(s);
  663. const ms = queries
  664. .getOutgoingEdges(s.id, ['contains'])
  665. .map((e) => queries.getNodeById(e.target))
  666. .filter((n): n is Node => !!n && n.kind === 'method');
  667. methodNodesByStruct.set(s.id, ms);
  668. methodNamesByStruct.set(s.id, new Set(ms.map((m) => m.name)));
  669. }
  670. for (const stub of goStructs) {
  671. if (!STUB_RE.test(stub.name)) continue;
  672. // The stub MUST live in a generated file — that's what tells us this is
  673. // a protoc-emitted scaffold rather than someone naming a struct
  674. // `UnimplementedXxxServer` by hand. Without this gate we'd also bridge
  675. // such hand-written structs and create misleading edges.
  676. if (!isGeneratedFile(stub.filePath)) continue;
  677. const stubMethods = (methodNodesByStruct.get(stub.id) ?? []).filter(
  678. (m) => !isInternalMarker(m.name),
  679. );
  680. if (stubMethods.length === 0) continue;
  681. const stubMethodNames = stubMethods.map((m) => m.name);
  682. for (const cand of goStructs) {
  683. if (cand.id === stub.id) continue;
  684. // Skip generated-file candidates — they're siblings (msgClient,
  685. // UnsafeMsgServer, …) whose method sets coincidentally match.
  686. if (isGeneratedFile(cand.filePath)) continue;
  687. const candNames = methodNamesByStruct.get(cand.id);
  688. if (!candNames) continue;
  689. // Subset: every RPC method must exist on the candidate by name.
  690. // Signature-level match would tighten this further, but name-match
  691. // alone already gives one-to-one pairing in real codebases because
  692. // gRPC method-name sets are highly distinctive (Send + MultiSend +
  693. // UpdateParams + SetSendEnabled is unique to bank's MsgServer).
  694. if (!stubMethodNames.every((n) => candNames.has(n))) continue;
  695. const candMethods = methodNodesByStruct.get(cand.id) ?? [];
  696. let added = 0;
  697. for (const sm of stubMethods) {
  698. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  699. for (const cm of candMethods) {
  700. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  701. if (cm.name !== sm.name) continue;
  702. const key = `${sm.id}>${cm.id}`;
  703. if (seen.has(key)) continue;
  704. seen.add(key);
  705. edges.push({
  706. source: sm.id,
  707. target: cm.id,
  708. kind: 'calls',
  709. line: sm.startLine,
  710. provenance: 'heuristic',
  711. metadata: {
  712. synthesizedBy: 'go-grpc-stub-impl',
  713. via: cm.name,
  714. registeredAt: `${cm.filePath}:${cm.startLine}`,
  715. },
  716. });
  717. added++;
  718. }
  719. }
  720. }
  721. }
  722. return edges;
  723. }
  724. /**
  725. * Phase 5: React JSX child rendering. A component that returns `<Child .../>`
  726. * mounts Child — React calls it — but JSX instantiation isn't a static call edge,
  727. * so a render tree (App.render → StaticCanvas → renderStaticScene) breaks at the
  728. * JSX hop. Link parent → each capitalized JSX child it renders. File-oriented
  729. * (read each JSX file once). Precision gate: the child name must resolve to a
  730. * component/function/class node — TS generics like `Array<Foo>` resolve to a type
  731. * (or nothing) and are dropped.
  732. */
  733. function reactJsxChildEdges(ctx: ResolutionContext): Edge[] {
  734. const edges: Edge[] = [];
  735. const seen = new Set<string>();
  736. const PARENT_KINDS = new Set(['method', 'function', 'component']);
  737. for (const file of ctx.getAllFiles()) {
  738. const content = ctx.readFile(file);
  739. if (!content || (!content.includes('</') && !content.includes('/>'))) continue; // JSX-file gate
  740. const parents = ctx.getNodesInFile(file).filter((n) => PARENT_KINDS.has(n.kind));
  741. for (const parent of parents) {
  742. const src = sliceLines(content, parent.startLine, parent.endLine);
  743. if (!src || (!src.includes('</') && !src.includes('/>'))) continue;
  744. const names = new Set<string>();
  745. JSX_TAG_RE.lastIndex = 0;
  746. let m: RegExpExecArray | null;
  747. while ((m = JSX_TAG_RE.exec(src))) names.add(m[1]!);
  748. let added = 0;
  749. for (const name of names) {
  750. if (added >= MAX_JSX_CHILDREN) break;
  751. const child = ctx.getNodesByName(name).find(
  752. (n) => n.kind === 'component' || n.kind === 'function' || n.kind === 'class'
  753. );
  754. if (!child || child.id === parent.id) continue;
  755. const key = `${parent.id}>${child.id}`;
  756. if (seen.has(key)) continue;
  757. seen.add(key);
  758. edges.push({
  759. source: parent.id, target: child.id, kind: 'calls', line: parent.startLine,
  760. provenance: 'heuristic',
  761. metadata: { synthesizedBy: 'jsx-render', via: name },
  762. });
  763. added++;
  764. }
  765. }
  766. }
  767. return edges;
  768. }
  769. /**
  770. * Phase 6: Vue SFC templates. The `.vue` extractor only parses `<script>`, so
  771. * template usage is invisible — child components and event handlers used ONLY in
  772. * the template have no edge to them. PascalCase children (`<VPNav/>`) are already
  773. * caught by reactJsxChildEdges (which scans the SFC component node), so this adds
  774. * the two Vue-specific shapes:
  775. * - kebab-case children: `<el-button>` → `ElButton` component (renders).
  776. * - event bindings: `@click="onClick"` / `v-on:submit="save"` → handler method.
  777. * Scoped to the `<template>` block of `.vue` files; resolution gate (kebab→
  778. * component, handler→function/method) keeps precision; inline arrows / `$emit`
  779. * skipped.
  780. */
  781. function vueTemplateEdges(ctx: ResolutionContext): Edge[] {
  782. const edges: Edge[] = [];
  783. const seen = new Set<string>();
  784. const COMPONENT_KINDS = new Set(['component', 'function', 'class']);
  785. const HANDLER_KINDS = new Set(['method', 'function']);
  786. // A composable's returned member may be a fn (`function close(){}`) or an
  787. // arrow assigned to a const (`const close = () => {}`).
  788. const RETURN_KINDS = new Set(['method', 'function', 'variable', 'constant']);
  789. // Nuxt auto-imports nested components by a DIRECTORY-PREFIXED name —
  790. // `components/media/Card.vue` is used as `<MediaCard/>`, not `<Card/>` — but
  791. // the component node is named by basename (`Card`), so a direct tag match
  792. // misses it (flat components match by basename and don't need this). Map each
  793. // nested component's Nuxt name → node so those template usages resolve.
  794. const nuxtComponents = new Map<string, Node>();
  795. for (const c of ctx.getNodesByKind('component')) {
  796. const nn = nuxtComponentName(c.filePath);
  797. if (nn && !nuxtComponents.has(nn)) nuxtComponents.set(nn, c);
  798. }
  799. for (const file of ctx.getAllFiles()) {
  800. if (!file.endsWith('.vue')) continue;
  801. const content = ctx.readFile(file);
  802. const tpl = content && content.match(/<template[^>]*>([\s\S]*)<\/template>/i)?.[1];
  803. if (!tpl) continue;
  804. const comp = ctx.getNodesInFile(file).find((n) => n.kind === 'component');
  805. if (!comp) continue;
  806. // Composable-destructure map: alias → { composable, key }. Lets us resolve a
  807. // template handler that isn't a local function but a destructured composable
  808. // return (`@click="closeSidebar"` ← `const { close: closeSidebar } = useSidebarControl()`).
  809. const script = content.match(/<script[^>]*>([\s\S]*?)<\/script>/i)?.[1] ?? '';
  810. const destructured = new Map<string, { composable: string; key: string }>();
  811. VUE_DESTRUCTURE_RE.lastIndex = 0;
  812. let dm: RegExpExecArray | null;
  813. while ((dm = VUE_DESTRUCTURE_RE.exec(script))) {
  814. if (!/^use[A-Z]/.test(dm[2]!)) continue; // composables / hooks only
  815. for (const part of dm[1]!.split(',')) {
  816. const pm = part.trim().match(/^(\w+)\s*(?::\s*(\w+))?$/); // key | key: alias
  817. if (pm) destructured.set(pm[2] || pm[1]!, { composable: dm[2]!, key: pm[1]! });
  818. }
  819. }
  820. let added = 0;
  821. const addEdge = (target: Node | undefined, meta: Record<string, unknown>) => {
  822. if (added >= MAX_JSX_CHILDREN || !target || target.id === comp.id) return;
  823. const k = `${comp.id}>${target.id}>${meta.synthesizedBy}`;
  824. if (seen.has(k)) return;
  825. seen.add(k);
  826. edges.push({ source: comp.id, target: target.id, kind: 'calls', line: comp.startLine, provenance: 'heuristic', metadata: meta });
  827. added++;
  828. };
  829. // Prefer a target in THIS SFC (handlers live in the same file's script) —
  830. // avoids cross-file mis-match when a name repeats across a monorepo.
  831. const resolve = (name: string, kinds: Set<string>): Node | undefined => {
  832. const matches = ctx.getNodesByName(name).filter((n) => kinds.has(n.kind));
  833. return matches.find((n) => n.filePath === file) ?? matches[0];
  834. };
  835. let m: RegExpExecArray | null;
  836. VUE_KEBAB_RE.lastIndex = 0;
  837. while ((m = VUE_KEBAB_RE.exec(tpl))) {
  838. const tag = kebabToPascal(m[1]!);
  839. addEdge(resolve(tag, COMPONENT_KINDS) ?? nuxtComponents.get(tag), { synthesizedBy: 'jsx-render', via: m[1] });
  840. }
  841. // PascalCase component tags. Try a direct name match first (flat components
  842. // and explicit registrations), then the Nuxt dir-prefixed auto-import name
  843. // (`<MediaCard>` → components/media/Card.vue). Built-ins match neither → no edge.
  844. VUE_PASCAL_RE.lastIndex = 0;
  845. while ((m = VUE_PASCAL_RE.exec(tpl))) {
  846. const tag = m[1]!;
  847. addEdge(resolve(tag, COMPONENT_KINDS) ?? nuxtComponents.get(tag), { synthesizedBy: 'jsx-render', via: tag });
  848. }
  849. VUE_HANDLER_RE.lastIndex = 0;
  850. while ((m = VUE_HANDLER_RE.exec(tpl))) {
  851. const event = m[1]!;
  852. const expr = m[2]!.trim();
  853. if (expr.includes('=>') || expr.startsWith('$')) continue; // inline arrow / $emit
  854. const name = expr.match(/^([A-Za-z_]\w*)/)?.[1];
  855. if (!name) continue;
  856. const direct = resolve(name, HANDLER_KINDS);
  857. if (direct) { addEdge(direct, { synthesizedBy: 'vue-handler', event }); continue; }
  858. // Composable-destructure handler → resolve to the composable's returned fn.
  859. const d = destructured.get(name);
  860. if (!d) continue;
  861. const composable = resolve(d.composable, HANDLER_KINDS);
  862. // Resolve to the SPECIFIC returned member (e.g. `close`) defined in the
  863. // composable's file. No fallback to the composable itself — the component
  864. // already has a static `useX()` call edge, so that would just be redundant
  865. // and less precise.
  866. const keyFn = composable
  867. ? ctx.getNodesByName(d.key).find((n) => RETURN_KINDS.has(n.kind) && n.filePath === composable.filePath)
  868. : undefined;
  869. if (keyFn) addEdge(keyFn, { synthesizedBy: 'vue-handler', event, via: d.composable });
  870. }
  871. }
  872. return edges;
  873. }
  874. /**
  875. * React Native cross-language event channel (Phase 3 of the mixed-iOS/RN
  876. * bridging effort). Same shape as `eventEmitterEdges` but cross-language:
  877. *
  878. * Native (ObjC, on RCTEventEmitter subclass):
  879. * [self sendEventWithName:@"locationUpdate" body:@{...}];
  880. *
  881. * Native (Java/Kotlin, via the JS module dispatcher):
  882. * emitter.emit("locationUpdate", body);
  883. * reactContext.getJSModule(RCTDeviceEventEmitter.class).emit("locationUpdate", body);
  884. *
  885. * JS (subscriber):
  886. * new NativeEventEmitter(NativeModules.Geo).addListener("locationUpdate", handler);
  887. * DeviceEventEmitter.addListener("locationUpdate", handler);
  888. *
  889. * Synthesize: native dispatch site → JS handler, keyed by the literal
  890. * event name. Only matches NAMED handlers (the existing `ON_RE` named-
  891. * capture form). Inline arrow handlers like `addListener('x', d => …)`
  892. * aren't named at extraction time and would need link-through-body
  893. * support; matches the deliberate scope of the in-language synthesizer.
  894. *
  895. * Provenance `'heuristic'`, synthesizedBy `'rn-event-channel'`.
  896. */
  897. // ObjC's `[self sendEventWithName:@"X" body:...]` shape (bracket syntax,
  898. // `@` string literals).
  899. const RN_OBJC_SEND_RE = /\bsendEventWithName\s*:\s*@"([^"]+)"/g;
  900. // Swift's `sendEvent(withName: "X", body: ...)` shape — same RCTEventEmitter
  901. // method, different call syntax. Both Objective-C and Swift subclass
  902. // RCTEventEmitter so this catches the Swift-side equivalent emission sites
  903. // (e.g. RNFusedLocation.swift's `sendEvent(withName: "geolocationDidChange",
  904. // body: locationData)`).
  905. const RN_SWIFT_SEND_RE = /\bsendEvent\s*\(\s*withName\s*:\s*"([^"]+)"/g;
  906. // JVM-side emitter calls: `emitter.emit("X", body)`. Matches both Java
  907. // and Kotlin syntax because the call form is identical. Restricted to
  908. // JVM source files in the consumer so we don't re-process JS emits
  909. // (which `eventEmitterEdges` already handles).
  910. const RN_JVM_EMIT_RE = /\.emit\s*\(\s*"([^"]+)"\s*,/g;
  911. // Custom `sendEvent(reactContext, "X", body)` wrapper — extremely common
  912. // (react-native-device-info and many libs wrap `DeviceEventManagerModule…emit`
  913. // behind a helper whose `.emit(eventName, …)` uses a VARIABLE, so RN_JVM_EMIT_RE
  914. // misses it; the literal lives in the wrapper CALL instead). Captures the first
  915. // string literal inside a `sendEvent(...)` call. `[^;{}]*?` keeps it on one
  916. // statement and stops at a block boundary, so the wrapper DEFINITION (whose `(`
  917. // is followed by `… ) {`) never matches. Multi-line tolerant. (java/kotlin/swift)
  918. const RN_NATIVE_SENDEVENT_RE = /\bsendEvent\s*\([^;{}]*?"([^"]+)"/g;
  919. function rnEventEdges(ctx: ResolutionContext): Edge[] {
  920. // Native dispatchers (source = the native method whose body sends the
  921. // event) and JS handlers (target = the function/method registered as
  922. // the listener) keyed by event name.
  923. const nativeDispatchersByEvent = new Map<string, Set<string>>();
  924. const jsHandlersByEvent = new Map<string, Map<string, string>>();
  925. for (const file of ctx.getAllFiles()) {
  926. const content = ctx.readFile(file);
  927. if (!content) continue;
  928. const nodesInFile = ctx.getNodesInFile(file);
  929. const lineOf = (idx: number) => content.slice(0, idx).split('\n').length;
  930. const addDispatcher = (event: string, line: number) => {
  931. const disp = enclosingFn(nodesInFile, line);
  932. if (!disp) return;
  933. const set = nativeDispatchersByEvent.get(event) ?? new Set<string>();
  934. set.add(disp.id);
  935. nativeDispatchersByEvent.set(event, set);
  936. };
  937. // ObjC side: `sendEventWithName:@"X"` only fires inside `.m`/`.mm`
  938. // files (RCTEventEmitter subclasses).
  939. if (file.endsWith('.m') || file.endsWith('.mm')) {
  940. RN_OBJC_SEND_RE.lastIndex = 0;
  941. let m: RegExpExecArray | null;
  942. while ((m = RN_OBJC_SEND_RE.exec(content))) {
  943. if (m[1]) addDispatcher(m[1], lineOf(m.index));
  944. }
  945. }
  946. // Swift side: same RCTEventEmitter method, parens/named-args syntax.
  947. if (file.endsWith('.swift')) {
  948. RN_SWIFT_SEND_RE.lastIndex = 0;
  949. let m: RegExpExecArray | null;
  950. while ((m = RN_SWIFT_SEND_RE.exec(content))) {
  951. if (m[1]) addDispatcher(m[1], lineOf(m.index));
  952. }
  953. RN_NATIVE_SENDEVENT_RE.lastIndex = 0;
  954. while ((m = RN_NATIVE_SENDEVENT_RE.exec(content))) {
  955. if (m[1]) addDispatcher(m[1], lineOf(m.index));
  956. }
  957. }
  958. // JVM side: `.emit("X", …)` in Java/Kotlin, plus the common
  959. // `sendEvent(ctx, "X", body)` wrapper. (We pattern-match anywhere in the
  960. // file; the JS in-language path uses a separate emitter object pattern and
  961. // is already handled by eventEmitterEdges.)
  962. if (file.endsWith('.java') || file.endsWith('.kt')) {
  963. let m: RegExpExecArray | null;
  964. RN_JVM_EMIT_RE.lastIndex = 0;
  965. while ((m = RN_JVM_EMIT_RE.exec(content))) {
  966. if (m[1]) addDispatcher(m[1], lineOf(m.index));
  967. }
  968. RN_NATIVE_SENDEVENT_RE.lastIndex = 0;
  969. while ((m = RN_NATIVE_SENDEVENT_RE.exec(content))) {
  970. if (m[1]) addDispatcher(m[1], lineOf(m.index));
  971. }
  972. }
  973. // JS subscribers (.addListener("X", handler)). Restrict to JS-family
  974. // files so a native file's `addListener:` (the ObjC method) doesn't
  975. // get mistaken for a JS subscription — they're entirely different
  976. // things despite sharing a name.
  977. if (
  978. file.endsWith('.js') ||
  979. file.endsWith('.jsx') ||
  980. file.endsWith('.ts') ||
  981. file.endsWith('.tsx') ||
  982. file.endsWith('.mjs') ||
  983. file.endsWith('.cjs')
  984. ) {
  985. // Match BOTH the named-handler form (`.addListener('x', fn)`) and
  986. // an unnamed-handler form (`.addListener('x', listener)` where
  987. // `listener` is a parameter — common in RN wrapper APIs like
  988. // RNFirebase's `messaging().onMessageReceived(listener)`). For the
  989. // unnamed case we attribute the subscription to the ENCLOSING JS
  990. // function (the abstraction layer), giving a reachability-correct
  991. // hop even when the actual user-side handler lives one call up.
  992. const ADDLISTENER_ANY = /\.(?:on|once|addListener)\(\s*['"]([^'"]+)['"]\s*,\s*([A-Za-z_][\w.]*)/g;
  993. ADDLISTENER_ANY.lastIndex = 0;
  994. let m: RegExpExecArray | null;
  995. while ((m = ADDLISTENER_ANY.exec(content))) {
  996. const event = m[1];
  997. const arg = m[2];
  998. if (!event || !arg) continue;
  999. const bareName = arg.includes('.') ? arg.slice(arg.lastIndexOf('.') + 1) : arg;
  1000. // Try a named-symbol match first (matches the in-language semantic).
  1001. const namedHandler = ctx
  1002. .getNodesByName(bareName)
  1003. .find((n) => n.kind === 'function' || n.kind === 'method');
  1004. let targetId: string | null = namedHandler?.id ?? null;
  1005. if (!targetId) {
  1006. // Fall back to the enclosing function — the subscribe-wrapper
  1007. // pattern means the event fires THROUGH this function on its
  1008. // way to user code. Reachability-correct attribution.
  1009. const enclosing = enclosingFn(nodesInFile, lineOf(m.index));
  1010. targetId = enclosing?.id ?? null;
  1011. }
  1012. if (!targetId) {
  1013. // Broader fallback for JS object-literal API shape
  1014. // (`const Foo = { watchX(...) { … addListener(...) … } }`):
  1015. // method shorthand inside an object literal isn't extracted
  1016. // as a method node, so enclosingFn returns null. Attribute to
  1017. // the smallest enclosing `constant` / `variable` node — that's
  1018. // the API surface a downstream caller would `import` and
  1019. // invoke. Reachability-correct.
  1020. const line = lineOf(m.index);
  1021. let smallest: typeof nodesInFile[number] | null = null;
  1022. for (const n of nodesInFile) {
  1023. if (n.kind !== 'constant' && n.kind !== 'variable') continue;
  1024. const end = n.endLine ?? n.startLine;
  1025. if (n.startLine <= line && end >= line) {
  1026. if (!smallest || n.startLine >= smallest.startLine) smallest = n;
  1027. }
  1028. }
  1029. targetId = smallest?.id ?? null;
  1030. }
  1031. if (!targetId) continue;
  1032. const map = jsHandlersByEvent.get(event) ?? new Map<string, string>();
  1033. map.set(targetId, `${file}:${lineOf(m.index)}`);
  1034. jsHandlersByEvent.set(event, map);
  1035. }
  1036. }
  1037. }
  1038. const edges: Edge[] = [];
  1039. const seen = new Set<string>();
  1040. for (const [event, dispatchers] of nativeDispatchersByEvent) {
  1041. const handlers = jsHandlersByEvent.get(event);
  1042. if (!handlers) continue;
  1043. // Same fan-out guard as the in-language channel: generic event names
  1044. // (e.g. 'change', 'error', 'data') with many handlers/dispatchers
  1045. // can't be matched precisely without receiver-type info.
  1046. if (dispatchers.size > EVENT_FANOUT_CAP || handlers.size > EVENT_FANOUT_CAP) continue;
  1047. for (const d of dispatchers) {
  1048. for (const [h, registeredAt] of handlers) {
  1049. if (d === h) continue;
  1050. const key = `${d}>${h}`;
  1051. if (seen.has(key)) continue;
  1052. seen.add(key);
  1053. edges.push({
  1054. source: d,
  1055. target: h,
  1056. kind: 'calls',
  1057. provenance: 'heuristic',
  1058. metadata: { synthesizedBy: 'rn-event-channel', event, registeredAt },
  1059. });
  1060. }
  1061. }
  1062. }
  1063. return edges;
  1064. }
  1065. /**
  1066. * Phase 6 — React Native Fabric/Codegen view component bridge.
  1067. *
  1068. * The Fabric framework extractor (`frameworks/fabric.ts`) emits
  1069. * `component` nodes named after the JS-visible component (e.g.
  1070. * `RNSScreenStack`) from each `codegenNativeComponent<Props>('Name')`
  1071. * spec declaration. The native implementation lives in an ObjC++/.mm or
  1072. * Kotlin/Java class whose name follows one of RN's conventions:
  1073. *
  1074. * - Exact: `RNSScreenStack`
  1075. * - With suffix: `RNSScreenStackView`, `RNSScreenStackViewManager`,
  1076. * `RNSScreenStackComponentView`, `RNSScreenStackManager`
  1077. *
  1078. * This synthesizer walks every Fabric component node and looks for a
  1079. * native class matching one of those names; when found, emits a
  1080. * `calls` edge `component → native class` (provenance `'heuristic'`,
  1081. * `synthesizedBy:'fabric-native-impl'`) so trace from JSX usage of the
  1082. * component continues into native.
  1083. *
  1084. * The convention-based suffix lookup is precise: there's no name
  1085. * collision in RN view-manager codebases by design (Codegen output would
  1086. * conflict otherwise).
  1087. */
  1088. const FABRIC_NATIVE_SUFFIXES = ['', 'View', 'ViewManager', 'ComponentView', 'Manager'];
  1089. /**
  1090. * Expo Modules cross-platform pairing. An Expo Module exposes the SAME
  1091. * JS-visible method (`AsyncFunction("getBatteryLevelAsync")`) from BOTH an iOS
  1092. * (Swift) and an Android (Kotlin) implementation. A JS callsite name-resolves to
  1093. * only ONE of them, so the other platform's impl looked like nothing called it
  1094. * (and editing it showed no blast radius). Link the iOS and Android impls of the
  1095. * same `<module>.<method>` to each other (both directions), so a JS call that
  1096. * reaches one platform reaches the other, and editing either surfaces the JS
  1097. * caller. The Expo method nodes are id-prefixed `expo-module:` and qualified
  1098. * `<file>::<module>.<method>` by the framework extractor.
  1099. */
  1100. function expoCrossPlatformEdges(queries: QueryBuilder): Edge[] {
  1101. const edges: Edge[] = [];
  1102. const seen = new Set<string>();
  1103. const byKey = new Map<string, Node[]>();
  1104. for (const m of queries.getNodesByKind('method')) {
  1105. if (!m.id.startsWith('expo-module:')) continue;
  1106. const key = m.qualifiedName.split('::').pop(); // `<module>.<method>`
  1107. if (!key) continue;
  1108. const arr = byKey.get(key);
  1109. if (arr) arr.push(m);
  1110. else byKey.set(key, [m]);
  1111. }
  1112. for (const group of byKey.values()) {
  1113. if (group.length < 2) continue;
  1114. for (const a of group) {
  1115. for (const b of group) {
  1116. if (a.id === b.id || a.language === b.language) continue; // cross-platform only
  1117. const key = `${a.id}>${b.id}`;
  1118. if (seen.has(key)) continue;
  1119. seen.add(key);
  1120. edges.push({
  1121. source: a.id,
  1122. target: b.id,
  1123. kind: 'calls',
  1124. line: a.startLine,
  1125. provenance: 'heuristic',
  1126. metadata: { synthesizedBy: 'expo-cross-platform', via: a.name },
  1127. });
  1128. }
  1129. }
  1130. }
  1131. return edges;
  1132. }
  1133. /**
  1134. * Classic React Native NativeModules cross-platform pairing. A native module
  1135. * method (`@ReactMethod` on Android, `RCT_EXPORT_METHOD` on iOS) is implemented
  1136. * on BOTH platforms, but a JS callsite name-resolves to only ONE — so the other
  1137. * platform's impl looked like nothing called it. A native method that HAS a JS
  1138. * caller is a confirmed bridge method; link it to the same-named native method
  1139. * in another language (the other platform's impl) so a JS call reaching one
  1140. * platform reaches the other, and editing either surfaces the JS caller.
  1141. *
  1142. * Names are normalized to the first selector keyword (`getFreeDiskStorage:` →
  1143. * `getFreeDiskStorage`) — that's the JS-visible name, and how the iOS selector
  1144. * lines up with the bare Android method name.
  1145. */
  1146. function rnCrossPlatformEdges(queries: QueryBuilder): Edge[] {
  1147. const edges: Edge[] = [];
  1148. const seen = new Set<string>();
  1149. const NATIVE = new Set(['java', 'kotlin', 'objc', 'cpp']);
  1150. const JS = new Set(['typescript', 'tsx', 'javascript', 'jsx']);
  1151. // RN module INFRASTRUCTURE methods exist on every native module (called by the
  1152. // RN runtime, not user JS), so pairing them by name would cross-link unrelated
  1153. // modules in a multi-module repo. Skip them — they aren't user-facing methods.
  1154. const RN_INFRA = new Set([
  1155. 'addListener', 'removeListeners', 'getConstants', 'constantsToExport', 'getName',
  1156. 'invalidate', 'initialize', 'getDefaultEventTypes', 'supportedEvents',
  1157. 'requiresMainQueueSetup', 'methodQueue',
  1158. ]);
  1159. const norm = (name: string): string => {
  1160. const i = name.indexOf(':');
  1161. return i >= 0 ? name.slice(0, i) : name;
  1162. };
  1163. // Index native methods by their JS-visible (normalized) name. Only names with
  1164. // impls in ≥2 native languages can pair, so the per-method JS-caller check
  1165. // below only runs for genuine cross-platform candidates.
  1166. const byName = new Map<string, Node[]>();
  1167. for (const m of queries.iterateNodesByKind('method')) {
  1168. if (!NATIVE.has(m.language)) continue;
  1169. const key = norm(m.name);
  1170. const arr = byName.get(key);
  1171. if (arr) arr.push(m);
  1172. else byName.set(key, [m]);
  1173. }
  1174. for (const [groupName, group] of byName) {
  1175. if (RN_INFRA.has(groupName)) continue;
  1176. const langs = new Set(group.map((m) => m.language));
  1177. if (langs.size < 2) continue; // single-platform — nothing to pair
  1178. for (const m of group) {
  1179. // Is m a bridge method? (a JS-language `calls` edge points at it)
  1180. const incoming = queries.getIncomingEdges(m.id, ['calls']);
  1181. if (incoming.length === 0) continue;
  1182. const sources = queries.getNodesByIds(incoming.map((e) => e.source));
  1183. const isBridge = incoming.some((e) => {
  1184. const s = sources.get(e.source);
  1185. return !!s && JS.has(s.language);
  1186. });
  1187. if (!isBridge) continue;
  1188. // Link to the other-platform impls (both directions).
  1189. for (const sib of group) {
  1190. if (sib.id === m.id || sib.language === m.language) continue;
  1191. for (const [a, b] of [[m, sib], [sib, m]] as const) {
  1192. const key = `${a.id}>${b.id}`;
  1193. if (seen.has(key)) continue;
  1194. seen.add(key);
  1195. edges.push({
  1196. source: a.id,
  1197. target: b.id,
  1198. kind: 'calls',
  1199. line: a.startLine,
  1200. provenance: 'heuristic',
  1201. metadata: { synthesizedBy: 'rn-cross-platform', via: norm(m.name) },
  1202. });
  1203. }
  1204. }
  1205. }
  1206. }
  1207. return edges;
  1208. }
  1209. function fabricNativeImplEdges(ctx: ResolutionContext): Edge[] {
  1210. const edges: Edge[] = [];
  1211. const seen = new Set<string>();
  1212. // The Fabric extractor IDs are prefixed `fabric-component:` so we can
  1213. // filter to just those without iterating all `component` nodes.
  1214. const components = ctx.getNodesByKind('component').filter((n) => n.id.startsWith('fabric-component:'));
  1215. if (components.length === 0) return edges;
  1216. // Pre-index native classes by name for O(1) lookup.
  1217. const nativeClassesByName = new Map<string, Node[]>();
  1218. for (const n of ctx.getNodesByKind('class')) {
  1219. if (n.language !== 'objc' && n.language !== 'kotlin' && n.language !== 'java' && n.language !== 'cpp') continue;
  1220. const arr = nativeClassesByName.get(n.name);
  1221. if (arr) arr.push(n);
  1222. else nativeClassesByName.set(n.name, [n]);
  1223. }
  1224. for (const component of components) {
  1225. for (const suffix of FABRIC_NATIVE_SUFFIXES) {
  1226. const candidate = component.name + suffix;
  1227. const matches = nativeClassesByName.get(candidate);
  1228. if (!matches || matches.length === 0) continue;
  1229. // Link the component node to every matching native class (iOS +
  1230. // Android each have one).
  1231. for (const native of matches) {
  1232. const key = `${component.id}>${native.id}`;
  1233. if (seen.has(key)) continue;
  1234. seen.add(key);
  1235. edges.push({
  1236. source: component.id,
  1237. target: native.id,
  1238. kind: 'calls',
  1239. provenance: 'heuristic',
  1240. metadata: {
  1241. synthesizedBy: 'fabric-native-impl',
  1242. viaSuffix: suffix || '(exact)',
  1243. componentName: component.name,
  1244. },
  1245. });
  1246. }
  1247. }
  1248. }
  1249. return edges;
  1250. }
  1251. /**
  1252. * MyBatis: link a Java mapper interface method to the XML statement that holds
  1253. * its SQL. The XML extractor (`src/extraction/mybatis-extractor.ts`) qualifies
  1254. * each `<select|insert|update|delete|sql id="X">` as `<namespace>::<id>` where
  1255. * `<namespace>` is the Java FQN of the mapper interface. A Java method's
  1256. * qualifiedName ends with `<ClassName>::<methodName>`, so we suffix-match the
  1257. * last two segments of the XML qualified name to find a unique Java method by
  1258. * `<ClassName>::<methodName>` (`ClassName` = last dotted segment of the XML
  1259. * namespace). Cross-mapper `<include refid="other.X">` references go through
  1260. * the normal qualified-name resolver — only the Java↔XML bridge is synthetic.
  1261. *
  1262. * Precision over recall: ambiguous mappers (multiple Java classes with the
  1263. * same simple name) are dropped. We need-not bridge by package because Java
  1264. * mapper interfaces are typically uniquely named within a project.
  1265. */
  1266. function mybatisJavaXmlEdges(queries: QueryBuilder): Edge[] {
  1267. const edges: Edge[] = [];
  1268. const seen = new Set<string>();
  1269. // Index Java methods by `<ClassName>::<methodName>` for O(1) lookup.
  1270. const javaIndex = new Map<string, Node[]>();
  1271. for (const m of queries.iterateNodesByKind('method')) {
  1272. if (m.language !== 'java' && m.language !== 'kotlin') continue;
  1273. const parts = m.qualifiedName.split('::');
  1274. const last = parts[parts.length - 1];
  1275. const cls = parts[parts.length - 2];
  1276. if (!last || !cls) continue;
  1277. const key = `${cls}::${last}`;
  1278. const arr = javaIndex.get(key);
  1279. if (arr) arr.push(m); else javaIndex.set(key, [m]);
  1280. }
  1281. for (const xml of queries.iterateNodesByKind('method')) {
  1282. if (xml.language !== 'xml') continue;
  1283. // Qualified name: `<namespace>::<id>`. Extract the simple class name.
  1284. const colonIdx = xml.qualifiedName.lastIndexOf('::');
  1285. if (colonIdx < 0) continue;
  1286. const namespace = xml.qualifiedName.slice(0, colonIdx);
  1287. const id = xml.qualifiedName.slice(colonIdx + 2);
  1288. if (!namespace || !id) continue;
  1289. const dotIdx = namespace.lastIndexOf('.');
  1290. const className = dotIdx >= 0 ? namespace.slice(dotIdx + 1) : namespace;
  1291. const candidates = javaIndex.get(`${className}::${id}`);
  1292. if (!candidates || candidates.length === 0) continue;
  1293. // Drop ambiguous matches (multiple same-name classes); the user can
  1294. // disambiguate by adding the package-suffix match in a future enhancement.
  1295. if (candidates.length > 1) continue;
  1296. const java = candidates[0]!;
  1297. const key = `${java.id}>${xml.id}`;
  1298. if (seen.has(key)) continue;
  1299. seen.add(key);
  1300. edges.push({
  1301. source: java.id,
  1302. target: xml.id,
  1303. kind: 'calls',
  1304. line: java.startLine,
  1305. provenance: 'heuristic',
  1306. metadata: {
  1307. synthesizedBy: 'mybatis-java-xml',
  1308. via: `${className}.${id}`,
  1309. registeredAt: `${xml.filePath}:${xml.startLine}`,
  1310. },
  1311. });
  1312. }
  1313. return edges;
  1314. }
  1315. /**
  1316. * Gin middleware chain. Gin runs its entire handler chain through one dynamic
  1317. * line in `(*Context).Next`:
  1318. * for c.index < len(c.handlers) { c.handlers[c.index](c); c.index++ }
  1319. * `c.handlers` is a `HandlersChain` (`[]HandlerFunc`) assembled at registration
  1320. * time by `combineHandlers` from the funcs passed to `r.Use(...)` /
  1321. * `r.GET("/path", h...)` / `r.Handle(...)`. Because the call is a computed index
  1322. * into a runtime-built slice, tree-sitter resolves `c.handlers[c.index](c)` to
  1323. * NOTHING — so `callees(Next)` is just the `len()` helper and the flow
  1324. * `ServeHTTP → handleHTTPRequest → Next` dead-ends at the exact symbol the
  1325. * "how do requests flow through the middleware chain" question is about. The
  1326. * agent then re-queries Next and falls back to Read/grep (validated: the gin
  1327. * WITH-arm rabbit-holed on precisely this dead-end).
  1328. *
  1329. * Bridge it: find the chain DISPATCHER (a Go method whose body invokes a
  1330. * `handlers` slice by index) and link it → every HandlerFunc registered via a
  1331. * gin registration call, so `callees(Next)` and `trace(ServeHTTP, <handler>)`
  1332. * connect end-to-end. Named handlers only (`gin.Logger()` → `Logger`,
  1333. * `authMiddleware`); inline closures are anonymous and skipped. Like
  1334. * react-render / interface-impl this is a deliberate over-approximation —
  1335. * reachability-correct (any registered handler CAN run for some route), capped,
  1336. * and gated on the dispatcher existing so it never runs on non-gin Go repos.
  1337. * Provenance `heuristic`, `synthesizedBy:'gin-middleware-chain'`; `registeredAt`
  1338. * is the `.Use`/`.GET` site an agent would otherwise grep for.
  1339. */
  1340. const GIN_DISPATCH_RE = /\.handlers\s*\[[^\]]*\]\s*\(/; // c.handlers[c.index](c)
  1341. const GIN_REG_RE = /\.(?:Use|GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD|Any|Handle)\s*\(/g;
  1342. /** Balanced `(...)` body starting at the '(' index; null if unbalanced. */
  1343. function goBalancedArgs(s: string, openIdx: number): string | null {
  1344. let depth = 0;
  1345. for (let i = openIdx; i < s.length; i++) {
  1346. const c = s[i];
  1347. if (c === '(') depth++;
  1348. else if (c === ')') { depth--; if (depth === 0) return s.slice(openIdx + 1, i); }
  1349. }
  1350. return null;
  1351. }
  1352. /** Split a top-level comma list, respecting nested () [] {}. */
  1353. function goSplitArgs(args: string): string[] {
  1354. const out: string[] = [];
  1355. let depth = 0, cur = '';
  1356. for (const c of args) {
  1357. if (c === '(' || c === '[' || c === '{') { depth++; cur += c; }
  1358. else if (c === ')' || c === ']' || c === '}') { depth--; cur += c; }
  1359. else if (c === ',' && depth === 0) { out.push(cur); cur = ''; }
  1360. else cur += c;
  1361. }
  1362. if (cur.trim()) out.push(cur);
  1363. return out;
  1364. }
  1365. /** Tail ident of a handler arg: `gin.Logger()`→`Logger`, `mw`→`mw`; null for string paths / closures. */
  1366. function goHandlerIdent(expr: string): string | null {
  1367. const cleaned = expr.trim().replace(/\(\s*\)$/, ''); // drop a trailing call ()
  1368. if (!cleaned || cleaned.startsWith('"') || cleaned.startsWith('`') || cleaned.startsWith('func')) return null;
  1369. const m = cleaned.match(/(?:\.|^)([A-Za-z_]\w*)$/);
  1370. return m ? m[1]! : null;
  1371. }
  1372. function ginMiddlewareChainEdges(queries: QueryBuilder, ctx: ResolutionContext): Edge[] {
  1373. // 1. Find the chain dispatcher(s): a Go method that invokes a `handlers` slice by index.
  1374. const dispatchers: Node[] = [];
  1375. for (const n of queries.iterateNodesByKind('method')) {
  1376. if (n.language !== 'go') continue;
  1377. const content = ctx.readFile(n.filePath);
  1378. const src = content && sliceLines(content, n.startLine, n.endLine);
  1379. if (src && GIN_DISPATCH_RE.test(src)) dispatchers.push(n);
  1380. }
  1381. if (dispatchers.length === 0) return []; // not a gin repo — bail
  1382. // 2. Collect handler identifiers registered via gin registration calls
  1383. // (.Use / .GET / … / .Handle). String args (paths/methods) and inline
  1384. // closures are dropped by goHandlerIdent; the rest are HandlerFuncs.
  1385. const registered = new Map<string, string>(); // name → registeredAt (file:line)
  1386. for (const file of ctx.getAllFiles()) {
  1387. if (!file.endsWith('.go')) continue;
  1388. const content = ctx.readFile(file);
  1389. if (!content || (!content.includes('.Use(') && !/\.(?:GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD|Any|Handle)\(/.test(content))) continue;
  1390. const safe = stripCommentsForRegex(content, 'go');
  1391. GIN_REG_RE.lastIndex = 0;
  1392. let m: RegExpExecArray | null;
  1393. while ((m = GIN_REG_RE.exec(safe))) {
  1394. const parenIdx = m.index + m[0].length - 1;
  1395. const argStr = goBalancedArgs(safe, parenIdx);
  1396. if (!argStr) continue;
  1397. const line = safe.slice(0, m.index).split('\n').length;
  1398. for (const arg of goSplitArgs(argStr)) {
  1399. const name = goHandlerIdent(arg);
  1400. if (name && !registered.has(name)) registered.set(name, `${file}:${line}`);
  1401. }
  1402. }
  1403. }
  1404. if (registered.size === 0) return [];
  1405. // 3. Link each dispatcher → each registered handler node (dedup, capped).
  1406. const edges: Edge[] = [];
  1407. const seen = new Set<string>();
  1408. for (const disp of dispatchers) {
  1409. let added = 0;
  1410. for (const [name, registeredAt] of registered) {
  1411. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  1412. const handler = ctx.getNodesByName(name).find(
  1413. (n) => (n.kind === 'function' || n.kind === 'method') && n.language === 'go'
  1414. );
  1415. if (!handler || handler.id === disp.id) continue;
  1416. const key = `${disp.id}>${handler.id}`;
  1417. if (seen.has(key)) continue;
  1418. seen.add(key);
  1419. edges.push({
  1420. source: disp.id, target: handler.id, kind: 'calls', line: disp.startLine,
  1421. provenance: 'heuristic',
  1422. metadata: { synthesizedBy: 'gin-middleware-chain', via: name, registeredAt },
  1423. });
  1424. added++;
  1425. }
  1426. }
  1427. return edges;
  1428. }
  1429. /**
  1430. * SvelteKit file-convention data flow. A route directory's `+page.svelte` (a
  1431. * `component` node) receives its `data` from the sibling `+page.server.{ts,js}`
  1432. * / `+page.{ts,js}` `load` function and posts forms to its `actions` — wired by
  1433. * the framework BY FILE PATH, with no static import between them. So editing a
  1434. * `load` shows no impact on the page it feeds, and the page looks like it has no
  1435. * server-side dependency. Link the page component to its sibling loader's
  1436. * `load` / `actions` (same for `+layout`). The pairing is path-deterministic
  1437. * (same directory, matching `+page`/`+layout` prefix), so it's precise — but
  1438. * it's a framework-convention edge, so provenance stays `heuristic`.
  1439. *
  1440. * Direction: page → load, so `getImpactRadius(load)` surfaces the page (editing
  1441. * a loader's data shows the page it feeds) and the page's dependencies include
  1442. * its loader.
  1443. */
  1444. function svelteKitLoadEdges(ctx: ResolutionContext): Edge[] {
  1445. const edges: Edge[] = [];
  1446. const allFiles = new Set(ctx.getAllFiles());
  1447. const HOOKS = new Set(['load', 'actions']);
  1448. const HOOK_KINDS = new Set(['function', 'method', 'constant', 'variable']);
  1449. for (const file of allFiles) {
  1450. const m = file.match(/(.*\/)(\+(?:page|layout))\.svelte$/);
  1451. if (!m) continue;
  1452. const dir = m[1]!;
  1453. const prefix = m[2]!;
  1454. const page = ctx.getNodesInFile(file).find((n) => n.kind === 'component');
  1455. if (!page) continue;
  1456. for (const ext of ['.server.ts', '.server.js', '.ts', '.js']) {
  1457. const loaderFile = `${dir}${prefix}${ext}`;
  1458. if (!allFiles.has(loaderFile)) continue;
  1459. for (const hook of ctx.getNodesInFile(loaderFile)) {
  1460. if (!HOOK_KINDS.has(hook.kind) || !HOOKS.has(hook.name)) continue;
  1461. edges.push({
  1462. source: page.id,
  1463. target: hook.id,
  1464. kind: 'references',
  1465. line: page.startLine,
  1466. provenance: 'heuristic',
  1467. metadata: {
  1468. synthesizedBy: 'sveltekit-load',
  1469. via: hook.name,
  1470. registeredAt: `${loaderFile}:${hook.startLine ?? 0}`,
  1471. },
  1472. });
  1473. }
  1474. }
  1475. }
  1476. return edges;
  1477. }
  1478. /**
  1479. * Synthesize dispatcher→callback edges (field observers + EventEmitters +
  1480. * React re-render + JSX children + Vue templates + SvelteKit load + RN event
  1481. * channel + Fabric native-impl + MyBatis Java↔XML + Gin middleware chain).
  1482. * Returns the count added. Never throws into indexing — callers wrap in try/catch.
  1483. */
  1484. export function synthesizeCallbackEdges(queries: QueryBuilder, ctx: ResolutionContext): number {
  1485. // Go implicit `implements` edges must be synthesized AND persisted first: the
  1486. // interface-dispatch bridge below reads `implements` edges from the DB, and
  1487. // Go has none statically. (Other languages already have static implements
  1488. // edges from extraction, so they don't need this pre-pass.)
  1489. const goImpl = goImplementsEdges(queries);
  1490. if (goImpl.length > 0) queries.insertEdges(goImpl);
  1491. const fieldEdges = fieldChannelEdges(queries, ctx);
  1492. const closureCollEdges = closureCollectionEdges(queries, ctx);
  1493. const emitterEdges = eventEmitterEdges(ctx);
  1494. const renderEdges = reactRenderEdges(queries, ctx);
  1495. const jsxEdges = reactJsxChildEdges(ctx);
  1496. const vueEdges = vueTemplateEdges(ctx);
  1497. const svelteKitEdges = svelteKitLoadEdges(ctx);
  1498. const flutterEdges = flutterBuildEdges(queries, ctx);
  1499. const cppEdges = cppOverrideEdges(queries);
  1500. const ifaceEdges = interfaceOverrideEdges(queries);
  1501. const kotlinExpectActual = kotlinExpectActualEdges(queries);
  1502. const goGrpcEdges = goGrpcStubImplEdges(queries);
  1503. const rnEventEdgesList = rnEventEdges(ctx);
  1504. const fabricNativeEdges = fabricNativeImplEdges(ctx);
  1505. const expoXPlatEdges = expoCrossPlatformEdges(queries);
  1506. const rnXPlatEdges = rnCrossPlatformEdges(queries);
  1507. const mybatisEdges = mybatisJavaXmlEdges(queries);
  1508. const ginEdges = ginMiddlewareChainEdges(queries, ctx);
  1509. const merged: Edge[] = [];
  1510. const seen = new Set<string>();
  1511. for (const e of [
  1512. ...fieldEdges,
  1513. ...closureCollEdges,
  1514. ...emitterEdges,
  1515. ...renderEdges,
  1516. ...jsxEdges,
  1517. ...vueEdges,
  1518. ...svelteKitEdges,
  1519. ...flutterEdges,
  1520. ...cppEdges,
  1521. ...ifaceEdges,
  1522. ...kotlinExpectActual,
  1523. ...goGrpcEdges,
  1524. ...rnEventEdgesList,
  1525. ...fabricNativeEdges,
  1526. ...expoXPlatEdges,
  1527. ...rnXPlatEdges,
  1528. ...mybatisEdges,
  1529. ...ginEdges,
  1530. ]) {
  1531. const key = `${e.source}>${e.target}`;
  1532. if (seen.has(key)) continue;
  1533. seen.add(key);
  1534. merged.push(e);
  1535. }
  1536. if (merged.length > 0) queries.insertEdges(merged);
  1537. return merged.length + goImpl.length;
  1538. }