callback-synthesizer.ts 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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. const REGISTRAR_NAME = /^(on[A-Z]\w*|subscribe|addListener|addEventListener|register|watch|listen|addCallback)$/;
  29. const DISPATCHER_NAME = /(emit|trigger|notify|dispatch|fire|publish|flush)/i;
  30. const MAX_CALLBACKS_PER_CHANNEL = 40;
  31. const EVENT_FANOUT_CAP = 6; // skip events with more handlers/dispatchers than this (too generic without type info)
  32. const ON_RE = /\.(?:on|once|addListener)\(\s*['"]([^'"]+)['"]\s*,\s*(?:function\s+(\w+)|(?:this\.)?(\w+))/g;
  33. const EMIT_RE = /\.(?:emit|fire|dispatchEvent)\(\s*['"]([^'"]+)['"]/g;
  34. const SETSTATE_RE = /this\.setState\s*\(/;
  35. const FLUTTER_SETSTATE_RE = /\bsetState\s*\(/; // Flutter: setState((){…}) / this.setState
  36. const JSX_TAG_RE = /<([A-Z][A-Za-z0-9_]*)[\s/>]/g;
  37. const MAX_JSX_CHILDREN = 30;
  38. // Vue SFC templates: kebab-case child components (<el-button> → ElButton) and
  39. // event bindings (@click="fn" / v-on:click="fn"). PascalCase children (<VPNav/>)
  40. // are already caught by JSX_TAG_RE via the SFC component node.
  41. const VUE_KEBAB_RE = /<([a-z][a-z0-9]*(?:-[a-z0-9]+)+)[\s/>]/g;
  42. const VUE_HANDLER_RE = /(?:@|v-on:)([a-zA-Z][\w-]*)(?:\.[\w]+)*\s*=\s*"([^"]+)"/g;
  43. // Composable/hook destructure: `const { close: closeSidebar } = useSidebarControl()`.
  44. // Captures the destructure body + the called composable; only `use*` calls qualify.
  45. const VUE_DESTRUCTURE_RE = /(?:const|let|var)\s*\{([^}]+)\}\s*=\s*(\w+)\s*\(/g;
  46. function kebabToPascal(s: string): string {
  47. return s.split('-').map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join('');
  48. }
  49. function sliceLines(content: string, startLine?: number, endLine?: number): string | null {
  50. if (!startLine || !endLine) return null;
  51. return content.split('\n').slice(startLine - 1, endLine).join('\n');
  52. }
  53. function registrarField(src: string): string | null {
  54. const m = src.match(/this\.(\w+)\.(?:add|push|set)\(/);
  55. return m ? m[1]! : null;
  56. }
  57. function dispatcherField(src: string): string | null {
  58. const forOf = src.match(/\bof\s+(?:Array\.from\(\s*)?this\.(\w+)/);
  59. if (forOf && /\b\w+\s*\(/.test(src)) return forOf[1]!;
  60. const forEach = src.match(/this\.(\w+)\.forEach\(/);
  61. if (forEach) return forEach[1]!;
  62. return null;
  63. }
  64. const FN_KINDS = new Set(['method', 'function', 'component']);
  65. /** Innermost function/method node whose line range contains `line`. */
  66. function enclosingFn(nodesInFile: Node[], line: number): Node | null {
  67. let best: Node | null = null;
  68. for (const n of nodesInFile) {
  69. if (!FN_KINDS.has(n.kind)) continue;
  70. const end = n.endLine ?? n.startLine;
  71. if (n.startLine <= line && end >= line) {
  72. if (!best || n.startLine >= best.startLine) best = n; // prefer the tightest (latest-starting) encloser
  73. }
  74. }
  75. return best;
  76. }
  77. /** Phase 1: field-backed observer channels (registrar/dispatcher share a store). */
  78. function fieldChannelEdges(queries: QueryBuilder, ctx: ResolutionContext): Edge[] {
  79. const candidates = [...queries.getNodesByKind('method'), ...queries.getNodesByKind('function')];
  80. const registrars: Array<{ node: Node; field: string }> = [];
  81. const dispatchers: Array<{ node: Node; field: string }> = [];
  82. for (const m of candidates) {
  83. const isReg = REGISTRAR_NAME.test(m.name);
  84. const isDisp = DISPATCHER_NAME.test(m.name);
  85. if (!isReg && !isDisp) continue;
  86. const content = ctx.readFile(m.filePath);
  87. const src = content && sliceLines(content, m.startLine, m.endLine);
  88. if (!src) continue;
  89. if (isReg) { const f = registrarField(src); if (f) registrars.push({ node: m, field: f }); }
  90. if (isDisp) { const f = dispatcherField(src); if (f) dispatchers.push({ node: m, field: f }); }
  91. }
  92. const edges: Edge[] = [];
  93. const seen = new Set<string>();
  94. for (const reg of registrars) {
  95. const chDispatchers = dispatchers.filter(
  96. (d) => d.node.filePath === reg.node.filePath && d.field === reg.field
  97. );
  98. if (chDispatchers.length === 0) continue;
  99. const argRe = new RegExp(`${reg.node.name}\\s*\\(\\s*(?:this\\.)?(\\w+)`);
  100. let added = 0;
  101. for (const e of queries.getIncomingEdges(reg.node.id, ['calls'])) {
  102. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  103. if (!e.line) continue;
  104. const caller = queries.getNodeById(e.source);
  105. if (!caller) continue;
  106. const line = ctx.readFile(caller.filePath)?.split('\n')[e.line - 1];
  107. const am = line?.match(argRe);
  108. if (!am) continue;
  109. const fn = ctx.getNodesByName(am[1]!).find((n) => n.kind === 'method' || n.kind === 'function');
  110. if (!fn) continue;
  111. for (const disp of chDispatchers) {
  112. if (disp.node.id === fn.id) continue;
  113. const key = `${disp.node.id}>${fn.id}`;
  114. if (seen.has(key)) continue;
  115. seen.add(key);
  116. edges.push({
  117. source: disp.node.id, target: fn.id, kind: 'calls', line: disp.node.startLine,
  118. provenance: 'heuristic',
  119. metadata: {
  120. synthesizedBy: 'callback', via: reg.node.name, field: reg.field,
  121. // Where the callback was wired up (`scene.onUpdate(this.triggerRender)`).
  122. // This is the #1 thing an agent reads/greps to explain the flow — surface
  123. // it so node/trace/context can show it without a callers() + Read round-trip.
  124. registeredAt: `${caller.filePath}:${e.line}`,
  125. },
  126. });
  127. added++;
  128. }
  129. }
  130. }
  131. return edges;
  132. }
  133. /** Phase 2: string-keyed EventEmitter channels (on('e', fn) ↔ emit('e')). */
  134. function eventEmitterEdges(ctx: ResolutionContext): Edge[] {
  135. const emitsByEvent = new Map<string, Set<string>>(); // event → dispatcher node ids
  136. const handlersByEvent = new Map<string, Map<string, string>>(); // event → handler id → registration site (file:line)
  137. for (const file of ctx.getAllFiles()) {
  138. const content = ctx.readFile(file);
  139. if (!content) continue;
  140. const hasEmit = content.includes('.emit(') || content.includes('.fire(') || content.includes('.dispatchEvent(');
  141. const hasOn = content.includes('.on(') || content.includes('.once(') || content.includes('.addListener(');
  142. if (!hasEmit && !hasOn) continue;
  143. const nodesInFile = ctx.getNodesInFile(file);
  144. const lineOf = (idx: number) => content.slice(0, idx).split('\n').length;
  145. if (hasEmit) {
  146. EMIT_RE.lastIndex = 0;
  147. let m: RegExpExecArray | null;
  148. while ((m = EMIT_RE.exec(content))) {
  149. const disp = enclosingFn(nodesInFile, lineOf(m.index));
  150. if (!disp) continue;
  151. const set = emitsByEvent.get(m[1]!) ?? new Set<string>();
  152. set.add(disp.id); emitsByEvent.set(m[1]!, set);
  153. }
  154. }
  155. if (hasOn) {
  156. ON_RE.lastIndex = 0;
  157. let m: RegExpExecArray | null;
  158. while ((m = ON_RE.exec(content))) {
  159. const handlerName = m[2] || m[3];
  160. if (!handlerName) continue;
  161. const handler = ctx.getNodesByName(handlerName).find((n) => n.kind === 'function' || n.kind === 'method');
  162. if (!handler) continue;
  163. const map = handlersByEvent.get(m[1]!) ?? new Map<string, string>();
  164. map.set(handler.id, `${file}:${lineOf(m.index)}`); handlersByEvent.set(m[1]!, map);
  165. }
  166. }
  167. }
  168. const edges: Edge[] = [];
  169. const seen = new Set<string>();
  170. for (const [event, dispatchers] of emitsByEvent) {
  171. const handlers = handlersByEvent.get(event);
  172. if (!handlers) continue;
  173. // Precision guard: a generic event name with many handlers/dispatchers can't
  174. // be matched without receiver-type info (Phase 3) — skip rather than over-link.
  175. if (dispatchers.size > EVENT_FANOUT_CAP || handlers.size > EVENT_FANOUT_CAP) continue;
  176. for (const d of dispatchers) for (const [h, registeredAt] of handlers) {
  177. if (d === h) continue;
  178. const key = `${d}>${h}`;
  179. if (seen.has(key)) continue;
  180. seen.add(key);
  181. edges.push({ source: d, target: h, kind: 'calls', provenance: 'heuristic', metadata: { synthesizedBy: 'event-emitter', event, registeredAt } });
  182. }
  183. }
  184. return edges;
  185. }
  186. /**
  187. * Phase 4: React class-component re-render. `this.setState(...)` re-runs the
  188. * component's `render()`, but that hop is React-internal — no static edge — so a
  189. * flow like "mutation → setState → canvas repaint" dead-ends at setState even
  190. * though `render → getRenderableElements → …` is fully call-connected after it.
  191. * Bridge it: for each class that has a `render` method, link every sibling method
  192. * whose body calls `this.setState(` → `render`. The setState gate keeps this to
  193. * React class components (a non-React class with a `render` method won't call
  194. * `this.setState`). Over-approximation (all setState methods reach render) is
  195. * accepted — it's reachability-correct, like the callback channels.
  196. */
  197. function reactRenderEdges(queries: QueryBuilder, ctx: ResolutionContext): Edge[] {
  198. const edges: Edge[] = [];
  199. const seen = new Set<string>();
  200. for (const cls of queries.getNodesByKind('class')) {
  201. const children = queries.getOutgoingEdges(cls.id, ['contains'])
  202. .map((e) => queries.getNodeById(e.target))
  203. .filter((n): n is Node => !!n && n.kind === 'method');
  204. const render = children.find((n) => n.name === 'render');
  205. if (!render) continue;
  206. let added = 0;
  207. for (const m of children) {
  208. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  209. if (m.id === render.id) continue;
  210. const content = ctx.readFile(m.filePath);
  211. const src = content && sliceLines(content, m.startLine, m.endLine);
  212. if (!src || !SETSTATE_RE.test(src)) continue;
  213. const key = `${m.id}>${render.id}`;
  214. if (seen.has(key)) continue;
  215. seen.add(key);
  216. edges.push({
  217. source: m.id, target: render.id, kind: 'calls', line: m.startLine,
  218. provenance: 'heuristic',
  219. metadata: { synthesizedBy: 'react-render', via: 'setState', registeredAt: `${render.filePath}:${render.startLine}` },
  220. });
  221. added++;
  222. }
  223. }
  224. return edges;
  225. }
  226. /**
  227. * Phase 4b: Flutter setState → build (the Dart analog of react-render). In a
  228. * StatefulWidget's State class, `setState(() {…})` re-runs `build(context)`, but
  229. * that hop is framework-internal (Flutter calls build), so a flow like
  230. * "onPressed → _increment → setState → rebuilt UI" dead-ends at setState. Bridge
  231. * it: for each Dart class with a `build` method, link every sibling method whose
  232. * body calls `setState(` → `build`. The setState gate + `.dart` file keep this to
  233. * Flutter State classes. Over-approximation accepted (reachability-correct).
  234. */
  235. function flutterBuildEdges(queries: QueryBuilder, ctx: ResolutionContext): Edge[] {
  236. const edges: Edge[] = [];
  237. const seen = new Set<string>();
  238. for (const cls of queries.getNodesByKind('class')) {
  239. const children = queries.getOutgoingEdges(cls.id, ['contains'])
  240. .map((e) => queries.getNodeById(e.target))
  241. .filter((n): n is Node => !!n && n.kind === 'method');
  242. const build = children.find((n) => n.name === 'build');
  243. if (!build || !build.filePath.endsWith('.dart')) continue;
  244. let added = 0;
  245. for (const m of children) {
  246. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  247. if (m.id === build.id) continue;
  248. const content = ctx.readFile(m.filePath);
  249. const src = content && sliceLines(content, m.startLine, m.endLine);
  250. if (!src || !FLUTTER_SETSTATE_RE.test(src)) continue;
  251. const key = `${m.id}>${build.id}`;
  252. if (seen.has(key)) continue;
  253. seen.add(key);
  254. edges.push({
  255. source: m.id, target: build.id, kind: 'calls', line: m.startLine,
  256. provenance: 'heuristic',
  257. metadata: { synthesizedBy: 'flutter-build', via: 'setState', registeredAt: `${build.filePath}:${build.startLine}` },
  258. });
  259. added++;
  260. }
  261. }
  262. return edges;
  263. }
  264. /**
  265. * Phase 4c: C++ virtual override. A call through a base/interface pointer
  266. * (`db->Get(...)`, `iter->Next()`) dispatches at runtime to a subclass override,
  267. * but that hop is a vtable indirection — no static call edge — so a flow stops at
  268. * the abstract base method. Bridge it like react-render: for each C++ class that
  269. * `extends` a base, link each base method → the subclass method of the same name
  270. * (the override), so trace/callees from the interface method reach the
  271. * implementation(s). Over-approximation accepted (reachability-correct); capped
  272. * per class and gated to C++ to avoid touching other languages' dispatch.
  273. */
  274. function cppOverrideEdges(queries: QueryBuilder): Edge[] {
  275. const edges: Edge[] = [];
  276. const seen = new Set<string>();
  277. const methodsOf = (classId: string): Node[] =>
  278. queries
  279. .getOutgoingEdges(classId, ['contains'])
  280. .map((e) => queries.getNodeById(e.target))
  281. .filter((n): n is Node => !!n && n.kind === 'method');
  282. for (const cls of queries.getNodesByKind('class')) {
  283. const subMethods = methodsOf(cls.id).filter((n) => n.language === 'cpp');
  284. if (subMethods.length === 0) continue;
  285. for (const ext of queries.getOutgoingEdges(cls.id, ['extends'])) {
  286. const base = queries.getNodeById(ext.target);
  287. if (!base || base.language !== 'cpp' || base.id === cls.id) continue;
  288. const baseMethods = new Map(methodsOf(base.id).map((m) => [m.name, m]));
  289. let added = 0;
  290. for (const m of subMethods) {
  291. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  292. const bm = baseMethods.get(m.name);
  293. if (!bm || bm.id === m.id) continue;
  294. const key = `${bm.id}>${m.id}`;
  295. if (seen.has(key)) continue;
  296. seen.add(key);
  297. edges.push({
  298. source: bm.id,
  299. target: m.id,
  300. kind: 'calls',
  301. line: bm.startLine,
  302. provenance: 'heuristic',
  303. metadata: { synthesizedBy: 'cpp-override', via: m.name, registeredAt: `${m.filePath}:${m.startLine}` },
  304. });
  305. added++;
  306. }
  307. }
  308. }
  309. return edges;
  310. }
  311. /**
  312. * Phase 5.5: interface / abstract dispatch (Java, Kotlin). A call through an
  313. * injected interface (`@Autowired FooService svc; svc.list()`) or an abstract
  314. * base dispatches at runtime to the implementing class's override — a vtable
  315. * indirection with no static call edge — so a request→service flow stops at the
  316. * interface method. Bridge it like cpp-override: for each class that
  317. * `implements` an interface (or `extends` an abstract base), link each
  318. * base/interface method → the class's same-name method (the override) so
  319. * trace/callees reach the implementation. Over-approximation accepted
  320. * (reachability-correct); capped per class, gated to JVM languages.
  321. */
  322. const IFACE_OVERRIDE_LANGS = new Set(['java', 'kotlin']);
  323. function interfaceOverrideEdges(queries: QueryBuilder): Edge[] {
  324. const edges: Edge[] = [];
  325. const seen = new Set<string>();
  326. const methodsOf = (classId: string): Node[] =>
  327. queries
  328. .getOutgoingEdges(classId, ['contains'])
  329. .map((e) => queries.getNodeById(e.target))
  330. .filter((n): n is Node => !!n && n.kind === 'method');
  331. for (const cls of queries.getNodesByKind('class')) {
  332. const implMethods = methodsOf(cls.id).filter((n) => IFACE_OVERRIDE_LANGS.has(n.language));
  333. if (implMethods.length === 0) continue;
  334. for (const sup of queries.getOutgoingEdges(cls.id, ['implements', 'extends'])) {
  335. const base = queries.getNodeById(sup.target);
  336. if (!base || !IFACE_OVERRIDE_LANGS.has(base.language) || base.id === cls.id) continue;
  337. // Group impl methods by name to handle OVERLOADS: an interface `list()` and
  338. // `list(params)` are distinct nodes and a call may resolve to either, so
  339. // link every base overload → every same-name impl overload (keying by name
  340. // alone would drop all but one and miss the resolved overload).
  341. const implByName = new Map<string, Node[]>();
  342. for (const m of implMethods) {
  343. const arr = implByName.get(m.name);
  344. if (arr) arr.push(m); else implByName.set(m.name, [m]);
  345. }
  346. let added = 0;
  347. for (const bm of methodsOf(base.id)) {
  348. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  349. for (const m of implByName.get(bm.name) ?? []) {
  350. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  351. if (bm.id === m.id) continue;
  352. const key = `${bm.id}>${m.id}`;
  353. if (seen.has(key)) continue;
  354. seen.add(key);
  355. edges.push({
  356. source: bm.id,
  357. target: m.id,
  358. kind: 'calls',
  359. line: bm.startLine,
  360. provenance: 'heuristic',
  361. metadata: { synthesizedBy: 'interface-impl', via: m.name, registeredAt: `${m.filePath}:${m.startLine}` },
  362. });
  363. added++;
  364. }
  365. }
  366. }
  367. }
  368. return edges;
  369. }
  370. /**
  371. * Go gRPC stub → impl bridge. The protoc-gen-go-grpc codegen emits an
  372. * `UnimplementedXxxServer` struct in `*_grpc.pb.go` carrying one method
  373. * per service RPC; the real handler is a hand-written struct in another
  374. * file (`x/bank/keeper/msg_server.go::msgServer.Send` in cosmos-sdk).
  375. * Go's structural typing means no `implements` edge exists for our
  376. * resolver to follow, so `trace("Send","SendCoins")` lands on the
  377. * empty stub and reports "no path" (validated empirically — the cosmos
  378. * Q1 r1 trace failure that drove this work).
  379. *
  380. * Bridge: for each `UnimplementedXxxServer` whose RPC-method names are
  381. * a SUBSET of some other Go struct's method names, emit `calls` edges
  382. * `stub.method → impl.method` (paired by name). Excludes the gRPC
  383. * internal markers `mustEmbedUnimplementedXxxServer` and
  384. * `testEmbeddedByValue`, and skips candidate impls that themselves
  385. * live in a generated file (their `xxxClient` / sibling stubs would
  386. * otherwise look like impls).
  387. *
  388. * Multiple candidates is allowed and capped at MAX_CALLBACKS_PER_CHANNEL —
  389. * a service often has both a production impl and one or more test
  390. * mocks; linking to all preserves trace utility without false-favoring.
  391. *
  392. * Provenance: `heuristic`, `synthesizedBy: 'go-grpc-stub-impl'`. The
  393. * stub's source line is the wiring site shown in the trace trail.
  394. */
  395. function goGrpcStubImplEdges(queries: QueryBuilder): Edge[] {
  396. const edges: Edge[] = [];
  397. const seen = new Set<string>();
  398. const STUB_RE = /^Unimplemented.*Server$/;
  399. // gRPC internal-helper methods that appear on every Unimplemented*Server;
  400. // not part of the service contract, so exclude when computing the RPC-method
  401. // signature used to match impls.
  402. const isInternalMarker = (n: string) => n.startsWith('mustEmbed') || n === 'testEmbeddedByValue';
  403. // Methods directly contained by each Go struct, name-only. Built once.
  404. const methodNamesByStruct = new Map<string, Set<string>>();
  405. const methodNodesByStruct = new Map<string, Node[]>();
  406. const goStructs: Node[] = [];
  407. for (const s of queries.getNodesByKind('struct')) {
  408. if (s.language !== 'go') continue;
  409. goStructs.push(s);
  410. const ms = queries
  411. .getOutgoingEdges(s.id, ['contains'])
  412. .map((e) => queries.getNodeById(e.target))
  413. .filter((n): n is Node => !!n && n.kind === 'method');
  414. methodNodesByStruct.set(s.id, ms);
  415. methodNamesByStruct.set(s.id, new Set(ms.map((m) => m.name)));
  416. }
  417. for (const stub of goStructs) {
  418. if (!STUB_RE.test(stub.name)) continue;
  419. // The stub MUST live in a generated file — that's what tells us this is
  420. // a protoc-emitted scaffold rather than someone naming a struct
  421. // `UnimplementedXxxServer` by hand. Without this gate we'd also bridge
  422. // such hand-written structs and create misleading edges.
  423. if (!isGeneratedFile(stub.filePath)) continue;
  424. const stubMethods = (methodNodesByStruct.get(stub.id) ?? []).filter(
  425. (m) => !isInternalMarker(m.name),
  426. );
  427. if (stubMethods.length === 0) continue;
  428. const stubMethodNames = stubMethods.map((m) => m.name);
  429. for (const cand of goStructs) {
  430. if (cand.id === stub.id) continue;
  431. // Skip generated-file candidates — they're siblings (msgClient,
  432. // UnsafeMsgServer, …) whose method sets coincidentally match.
  433. if (isGeneratedFile(cand.filePath)) continue;
  434. const candNames = methodNamesByStruct.get(cand.id);
  435. if (!candNames) continue;
  436. // Subset: every RPC method must exist on the candidate by name.
  437. // Signature-level match would tighten this further, but name-match
  438. // alone already gives one-to-one pairing in real codebases because
  439. // gRPC method-name sets are highly distinctive (Send + MultiSend +
  440. // UpdateParams + SetSendEnabled is unique to bank's MsgServer).
  441. if (!stubMethodNames.every((n) => candNames.has(n))) continue;
  442. const candMethods = methodNodesByStruct.get(cand.id) ?? [];
  443. let added = 0;
  444. for (const sm of stubMethods) {
  445. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  446. for (const cm of candMethods) {
  447. if (added >= MAX_CALLBACKS_PER_CHANNEL) break;
  448. if (cm.name !== sm.name) continue;
  449. const key = `${sm.id}>${cm.id}`;
  450. if (seen.has(key)) continue;
  451. seen.add(key);
  452. edges.push({
  453. source: sm.id,
  454. target: cm.id,
  455. kind: 'calls',
  456. line: sm.startLine,
  457. provenance: 'heuristic',
  458. metadata: {
  459. synthesizedBy: 'go-grpc-stub-impl',
  460. via: cm.name,
  461. registeredAt: `${cm.filePath}:${cm.startLine}`,
  462. },
  463. });
  464. added++;
  465. }
  466. }
  467. }
  468. }
  469. return edges;
  470. }
  471. /**
  472. * Phase 5: React JSX child rendering. A component that returns `<Child .../>`
  473. * mounts Child — React calls it — but JSX instantiation isn't a static call edge,
  474. * so a render tree (App.render → StaticCanvas → renderStaticScene) breaks at the
  475. * JSX hop. Link parent → each capitalized JSX child it renders. File-oriented
  476. * (read each JSX file once). Precision gate: the child name must resolve to a
  477. * component/function/class node — TS generics like `Array<Foo>` resolve to a type
  478. * (or nothing) and are dropped.
  479. */
  480. function reactJsxChildEdges(ctx: ResolutionContext): Edge[] {
  481. const edges: Edge[] = [];
  482. const seen = new Set<string>();
  483. const PARENT_KINDS = new Set(['method', 'function', 'component']);
  484. for (const file of ctx.getAllFiles()) {
  485. const content = ctx.readFile(file);
  486. if (!content || (!content.includes('</') && !content.includes('/>'))) continue; // JSX-file gate
  487. const parents = ctx.getNodesInFile(file).filter((n) => PARENT_KINDS.has(n.kind));
  488. for (const parent of parents) {
  489. const src = sliceLines(content, parent.startLine, parent.endLine);
  490. if (!src || (!src.includes('</') && !src.includes('/>'))) continue;
  491. const names = new Set<string>();
  492. JSX_TAG_RE.lastIndex = 0;
  493. let m: RegExpExecArray | null;
  494. while ((m = JSX_TAG_RE.exec(src))) names.add(m[1]!);
  495. let added = 0;
  496. for (const name of names) {
  497. if (added >= MAX_JSX_CHILDREN) break;
  498. const child = ctx.getNodesByName(name).find(
  499. (n) => n.kind === 'component' || n.kind === 'function' || n.kind === 'class'
  500. );
  501. if (!child || child.id === parent.id) continue;
  502. const key = `${parent.id}>${child.id}`;
  503. if (seen.has(key)) continue;
  504. seen.add(key);
  505. edges.push({
  506. source: parent.id, target: child.id, kind: 'calls', line: parent.startLine,
  507. provenance: 'heuristic',
  508. metadata: { synthesizedBy: 'jsx-render', via: name },
  509. });
  510. added++;
  511. }
  512. }
  513. }
  514. return edges;
  515. }
  516. /**
  517. * Phase 6: Vue SFC templates. The `.vue` extractor only parses `<script>`, so
  518. * template usage is invisible — child components and event handlers used ONLY in
  519. * the template have no edge to them. PascalCase children (`<VPNav/>`) are already
  520. * caught by reactJsxChildEdges (which scans the SFC component node), so this adds
  521. * the two Vue-specific shapes:
  522. * - kebab-case children: `<el-button>` → `ElButton` component (renders).
  523. * - event bindings: `@click="onClick"` / `v-on:submit="save"` → handler method.
  524. * Scoped to the `<template>` block of `.vue` files; resolution gate (kebab→
  525. * component, handler→function/method) keeps precision; inline arrows / `$emit`
  526. * skipped.
  527. */
  528. function vueTemplateEdges(ctx: ResolutionContext): Edge[] {
  529. const edges: Edge[] = [];
  530. const seen = new Set<string>();
  531. const COMPONENT_KINDS = new Set(['component', 'function', 'class']);
  532. const HANDLER_KINDS = new Set(['method', 'function']);
  533. // A composable's returned member may be a fn (`function close(){}`) or an
  534. // arrow assigned to a const (`const close = () => {}`).
  535. const RETURN_KINDS = new Set(['method', 'function', 'variable', 'constant']);
  536. for (const file of ctx.getAllFiles()) {
  537. if (!file.endsWith('.vue')) continue;
  538. const content = ctx.readFile(file);
  539. const tpl = content && content.match(/<template[^>]*>([\s\S]*)<\/template>/i)?.[1];
  540. if (!tpl) continue;
  541. const comp = ctx.getNodesInFile(file).find((n) => n.kind === 'component');
  542. if (!comp) continue;
  543. // Composable-destructure map: alias → { composable, key }. Lets us resolve a
  544. // template handler that isn't a local function but a destructured composable
  545. // return (`@click="closeSidebar"` ← `const { close: closeSidebar } = useSidebarControl()`).
  546. const script = content.match(/<script[^>]*>([\s\S]*?)<\/script>/i)?.[1] ?? '';
  547. const destructured = new Map<string, { composable: string; key: string }>();
  548. VUE_DESTRUCTURE_RE.lastIndex = 0;
  549. let dm: RegExpExecArray | null;
  550. while ((dm = VUE_DESTRUCTURE_RE.exec(script))) {
  551. if (!/^use[A-Z]/.test(dm[2]!)) continue; // composables / hooks only
  552. for (const part of dm[1]!.split(',')) {
  553. const pm = part.trim().match(/^(\w+)\s*(?::\s*(\w+))?$/); // key | key: alias
  554. if (pm) destructured.set(pm[2] || pm[1]!, { composable: dm[2]!, key: pm[1]! });
  555. }
  556. }
  557. let added = 0;
  558. const addEdge = (target: Node | undefined, meta: Record<string, unknown>) => {
  559. if (added >= MAX_JSX_CHILDREN || !target || target.id === comp.id) return;
  560. const k = `${comp.id}>${target.id}>${meta.synthesizedBy}`;
  561. if (seen.has(k)) return;
  562. seen.add(k);
  563. edges.push({ source: comp.id, target: target.id, kind: 'calls', line: comp.startLine, provenance: 'heuristic', metadata: meta });
  564. added++;
  565. };
  566. // Prefer a target in THIS SFC (handlers live in the same file's script) —
  567. // avoids cross-file mis-match when a name repeats across a monorepo.
  568. const resolve = (name: string, kinds: Set<string>): Node | undefined => {
  569. const matches = ctx.getNodesByName(name).filter((n) => kinds.has(n.kind));
  570. return matches.find((n) => n.filePath === file) ?? matches[0];
  571. };
  572. let m: RegExpExecArray | null;
  573. VUE_KEBAB_RE.lastIndex = 0;
  574. while ((m = VUE_KEBAB_RE.exec(tpl))) addEdge(resolve(kebabToPascal(m[1]!), COMPONENT_KINDS), { synthesizedBy: 'jsx-render', via: m[1] });
  575. VUE_HANDLER_RE.lastIndex = 0;
  576. while ((m = VUE_HANDLER_RE.exec(tpl))) {
  577. const event = m[1]!;
  578. const expr = m[2]!.trim();
  579. if (expr.includes('=>') || expr.startsWith('$')) continue; // inline arrow / $emit
  580. const name = expr.match(/^([A-Za-z_]\w*)/)?.[1];
  581. if (!name) continue;
  582. const direct = resolve(name, HANDLER_KINDS);
  583. if (direct) { addEdge(direct, { synthesizedBy: 'vue-handler', event }); continue; }
  584. // Composable-destructure handler → resolve to the composable's returned fn.
  585. const d = destructured.get(name);
  586. if (!d) continue;
  587. const composable = resolve(d.composable, HANDLER_KINDS);
  588. // Resolve to the SPECIFIC returned member (e.g. `close`) defined in the
  589. // composable's file. No fallback to the composable itself — the component
  590. // already has a static `useX()` call edge, so that would just be redundant
  591. // and less precise.
  592. const keyFn = composable
  593. ? ctx.getNodesByName(d.key).find((n) => RETURN_KINDS.has(n.kind) && n.filePath === composable.filePath)
  594. : undefined;
  595. if (keyFn) addEdge(keyFn, { synthesizedBy: 'vue-handler', event, via: d.composable });
  596. }
  597. }
  598. return edges;
  599. }
  600. /**
  601. * React Native cross-language event channel (Phase 3 of the mixed-iOS/RN
  602. * bridging effort). Same shape as `eventEmitterEdges` but cross-language:
  603. *
  604. * Native (ObjC, on RCTEventEmitter subclass):
  605. * [self sendEventWithName:@"locationUpdate" body:@{...}];
  606. *
  607. * Native (Java/Kotlin, via the JS module dispatcher):
  608. * emitter.emit("locationUpdate", body);
  609. * reactContext.getJSModule(RCTDeviceEventEmitter.class).emit("locationUpdate", body);
  610. *
  611. * JS (subscriber):
  612. * new NativeEventEmitter(NativeModules.Geo).addListener("locationUpdate", handler);
  613. * DeviceEventEmitter.addListener("locationUpdate", handler);
  614. *
  615. * Synthesize: native dispatch site → JS handler, keyed by the literal
  616. * event name. Only matches NAMED handlers (the existing `ON_RE` named-
  617. * capture form). Inline arrow handlers like `addListener('x', d => …)`
  618. * aren't named at extraction time and would need link-through-body
  619. * support; matches the deliberate scope of the in-language synthesizer.
  620. *
  621. * Provenance `'heuristic'`, synthesizedBy `'rn-event-channel'`.
  622. */
  623. // ObjC's `[self sendEventWithName:@"X" body:...]` shape (bracket syntax,
  624. // `@` string literals).
  625. const RN_OBJC_SEND_RE = /\bsendEventWithName\s*:\s*@"([^"]+)"/g;
  626. // Swift's `sendEvent(withName: "X", body: ...)` shape — same RCTEventEmitter
  627. // method, different call syntax. Both Objective-C and Swift subclass
  628. // RCTEventEmitter so this catches the Swift-side equivalent emission sites
  629. // (e.g. RNFusedLocation.swift's `sendEvent(withName: "geolocationDidChange",
  630. // body: locationData)`).
  631. const RN_SWIFT_SEND_RE = /\bsendEvent\s*\(\s*withName\s*:\s*"([^"]+)"/g;
  632. // JVM-side emitter calls: `emitter.emit("X", body)`. Matches both Java
  633. // and Kotlin syntax because the call form is identical. Restricted to
  634. // JVM source files in the consumer so we don't re-process JS emits
  635. // (which `eventEmitterEdges` already handles).
  636. const RN_JVM_EMIT_RE = /\.emit\s*\(\s*"([^"]+)"\s*,/g;
  637. function rnEventEdges(ctx: ResolutionContext): Edge[] {
  638. // Native dispatchers (source = the native method whose body sends the
  639. // event) and JS handlers (target = the function/method registered as
  640. // the listener) keyed by event name.
  641. const nativeDispatchersByEvent = new Map<string, Set<string>>();
  642. const jsHandlersByEvent = new Map<string, Map<string, string>>();
  643. for (const file of ctx.getAllFiles()) {
  644. const content = ctx.readFile(file);
  645. if (!content) continue;
  646. const nodesInFile = ctx.getNodesInFile(file);
  647. const lineOf = (idx: number) => content.slice(0, idx).split('\n').length;
  648. const addDispatcher = (event: string, line: number) => {
  649. const disp = enclosingFn(nodesInFile, line);
  650. if (!disp) return;
  651. const set = nativeDispatchersByEvent.get(event) ?? new Set<string>();
  652. set.add(disp.id);
  653. nativeDispatchersByEvent.set(event, set);
  654. };
  655. // ObjC side: `sendEventWithName:@"X"` only fires inside `.m`/`.mm`
  656. // files (RCTEventEmitter subclasses).
  657. if (file.endsWith('.m') || file.endsWith('.mm')) {
  658. RN_OBJC_SEND_RE.lastIndex = 0;
  659. let m: RegExpExecArray | null;
  660. while ((m = RN_OBJC_SEND_RE.exec(content))) {
  661. if (m[1]) addDispatcher(m[1], lineOf(m.index));
  662. }
  663. }
  664. // Swift side: same RCTEventEmitter method, parens/named-args syntax.
  665. if (file.endsWith('.swift')) {
  666. RN_SWIFT_SEND_RE.lastIndex = 0;
  667. let m: RegExpExecArray | null;
  668. while ((m = RN_SWIFT_SEND_RE.exec(content))) {
  669. if (m[1]) addDispatcher(m[1], lineOf(m.index));
  670. }
  671. }
  672. // JVM side: `.emit("X", …)` in Java/Kotlin. (We pattern-match
  673. // anywhere in the file; the JS in-language path uses a separate
  674. // emitter object pattern and is already handled by eventEmitterEdges.)
  675. if (file.endsWith('.java') || file.endsWith('.kt')) {
  676. RN_JVM_EMIT_RE.lastIndex = 0;
  677. let m: RegExpExecArray | null;
  678. while ((m = RN_JVM_EMIT_RE.exec(content))) {
  679. if (m[1]) addDispatcher(m[1], lineOf(m.index));
  680. }
  681. }
  682. // JS subscribers (.addListener("X", handler)). Restrict to JS-family
  683. // files so a native file's `addListener:` (the ObjC method) doesn't
  684. // get mistaken for a JS subscription — they're entirely different
  685. // things despite sharing a name.
  686. if (
  687. file.endsWith('.js') ||
  688. file.endsWith('.jsx') ||
  689. file.endsWith('.ts') ||
  690. file.endsWith('.tsx') ||
  691. file.endsWith('.mjs') ||
  692. file.endsWith('.cjs')
  693. ) {
  694. // Match BOTH the named-handler form (`.addListener('x', fn)`) and
  695. // an unnamed-handler form (`.addListener('x', listener)` where
  696. // `listener` is a parameter — common in RN wrapper APIs like
  697. // RNFirebase's `messaging().onMessageReceived(listener)`). For the
  698. // unnamed case we attribute the subscription to the ENCLOSING JS
  699. // function (the abstraction layer), giving a reachability-correct
  700. // hop even when the actual user-side handler lives one call up.
  701. const ADDLISTENER_ANY = /\.(?:on|once|addListener)\(\s*['"]([^'"]+)['"]\s*,\s*([A-Za-z_][\w.]*)/g;
  702. ADDLISTENER_ANY.lastIndex = 0;
  703. let m: RegExpExecArray | null;
  704. while ((m = ADDLISTENER_ANY.exec(content))) {
  705. const event = m[1];
  706. const arg = m[2];
  707. if (!event || !arg) continue;
  708. const bareName = arg.includes('.') ? arg.slice(arg.lastIndexOf('.') + 1) : arg;
  709. // Try a named-symbol match first (matches the in-language semantic).
  710. const namedHandler = ctx
  711. .getNodesByName(bareName)
  712. .find((n) => n.kind === 'function' || n.kind === 'method');
  713. let targetId: string | null = namedHandler?.id ?? null;
  714. if (!targetId) {
  715. // Fall back to the enclosing function — the subscribe-wrapper
  716. // pattern means the event fires THROUGH this function on its
  717. // way to user code. Reachability-correct attribution.
  718. const enclosing = enclosingFn(nodesInFile, lineOf(m.index));
  719. targetId = enclosing?.id ?? null;
  720. }
  721. if (!targetId) {
  722. // Broader fallback for JS object-literal API shape
  723. // (`const Foo = { watchX(...) { … addListener(...) … } }`):
  724. // method shorthand inside an object literal isn't extracted
  725. // as a method node, so enclosingFn returns null. Attribute to
  726. // the smallest enclosing `constant` / `variable` node — that's
  727. // the API surface a downstream caller would `import` and
  728. // invoke. Reachability-correct.
  729. const line = lineOf(m.index);
  730. let smallest: typeof nodesInFile[number] | null = null;
  731. for (const n of nodesInFile) {
  732. if (n.kind !== 'constant' && n.kind !== 'variable') continue;
  733. const end = n.endLine ?? n.startLine;
  734. if (n.startLine <= line && end >= line) {
  735. if (!smallest || n.startLine >= smallest.startLine) smallest = n;
  736. }
  737. }
  738. targetId = smallest?.id ?? null;
  739. }
  740. if (!targetId) continue;
  741. const map = jsHandlersByEvent.get(event) ?? new Map<string, string>();
  742. map.set(targetId, `${file}:${lineOf(m.index)}`);
  743. jsHandlersByEvent.set(event, map);
  744. }
  745. }
  746. }
  747. const edges: Edge[] = [];
  748. const seen = new Set<string>();
  749. for (const [event, dispatchers] of nativeDispatchersByEvent) {
  750. const handlers = jsHandlersByEvent.get(event);
  751. if (!handlers) continue;
  752. // Same fan-out guard as the in-language channel: generic event names
  753. // (e.g. 'change', 'error', 'data') with many handlers/dispatchers
  754. // can't be matched precisely without receiver-type info.
  755. if (dispatchers.size > EVENT_FANOUT_CAP || handlers.size > EVENT_FANOUT_CAP) continue;
  756. for (const d of dispatchers) {
  757. for (const [h, registeredAt] of handlers) {
  758. if (d === h) continue;
  759. const key = `${d}>${h}`;
  760. if (seen.has(key)) continue;
  761. seen.add(key);
  762. edges.push({
  763. source: d,
  764. target: h,
  765. kind: 'calls',
  766. provenance: 'heuristic',
  767. metadata: { synthesizedBy: 'rn-event-channel', event, registeredAt },
  768. });
  769. }
  770. }
  771. }
  772. return edges;
  773. }
  774. /**
  775. * Phase 6 — React Native Fabric/Codegen view component bridge.
  776. *
  777. * The Fabric framework extractor (`frameworks/fabric.ts`) emits
  778. * `component` nodes named after the JS-visible component (e.g.
  779. * `RNSScreenStack`) from each `codegenNativeComponent<Props>('Name')`
  780. * spec declaration. The native implementation lives in an ObjC++/.mm or
  781. * Kotlin/Java class whose name follows one of RN's conventions:
  782. *
  783. * - Exact: `RNSScreenStack`
  784. * - With suffix: `RNSScreenStackView`, `RNSScreenStackViewManager`,
  785. * `RNSScreenStackComponentView`, `RNSScreenStackManager`
  786. *
  787. * This synthesizer walks every Fabric component node and looks for a
  788. * native class matching one of those names; when found, emits a
  789. * `calls` edge `component → native class` (provenance `'heuristic'`,
  790. * `synthesizedBy:'fabric-native-impl'`) so trace from JSX usage of the
  791. * component continues into native.
  792. *
  793. * The convention-based suffix lookup is precise: there's no name
  794. * collision in RN view-manager codebases by design (Codegen output would
  795. * conflict otherwise).
  796. */
  797. const FABRIC_NATIVE_SUFFIXES = ['', 'View', 'ViewManager', 'ComponentView', 'Manager'];
  798. function fabricNativeImplEdges(ctx: ResolutionContext): Edge[] {
  799. const edges: Edge[] = [];
  800. const seen = new Set<string>();
  801. // The Fabric extractor IDs are prefixed `fabric-component:` so we can
  802. // filter to just those without iterating all `component` nodes.
  803. const components = ctx.getNodesByKind('component').filter((n) => n.id.startsWith('fabric-component:'));
  804. if (components.length === 0) return edges;
  805. // Pre-index native classes by name for O(1) lookup.
  806. const nativeClassesByName = new Map<string, Node[]>();
  807. for (const n of ctx.getNodesByKind('class')) {
  808. if (n.language !== 'objc' && n.language !== 'kotlin' && n.language !== 'java' && n.language !== 'cpp') continue;
  809. const arr = nativeClassesByName.get(n.name);
  810. if (arr) arr.push(n);
  811. else nativeClassesByName.set(n.name, [n]);
  812. }
  813. for (const component of components) {
  814. for (const suffix of FABRIC_NATIVE_SUFFIXES) {
  815. const candidate = component.name + suffix;
  816. const matches = nativeClassesByName.get(candidate);
  817. if (!matches || matches.length === 0) continue;
  818. // Link the component node to every matching native class (iOS +
  819. // Android each have one).
  820. for (const native of matches) {
  821. const key = `${component.id}>${native.id}`;
  822. if (seen.has(key)) continue;
  823. seen.add(key);
  824. edges.push({
  825. source: component.id,
  826. target: native.id,
  827. kind: 'calls',
  828. provenance: 'heuristic',
  829. metadata: {
  830. synthesizedBy: 'fabric-native-impl',
  831. viaSuffix: suffix || '(exact)',
  832. componentName: component.name,
  833. },
  834. });
  835. }
  836. }
  837. }
  838. return edges;
  839. }
  840. /**
  841. * MyBatis: link a Java mapper interface method to the XML statement that holds
  842. * its SQL. The XML extractor (`src/extraction/mybatis-extractor.ts`) qualifies
  843. * each `<select|insert|update|delete|sql id="X">` as `<namespace>::<id>` where
  844. * `<namespace>` is the Java FQN of the mapper interface. A Java method's
  845. * qualifiedName ends with `<ClassName>::<methodName>`, so we suffix-match the
  846. * last two segments of the XML qualified name to find a unique Java method by
  847. * `<ClassName>::<methodName>` (`ClassName` = last dotted segment of the XML
  848. * namespace). Cross-mapper `<include refid="other.X">` references go through
  849. * the normal qualified-name resolver — only the Java↔XML bridge is synthetic.
  850. *
  851. * Precision over recall: ambiguous mappers (multiple Java classes with the
  852. * same simple name) are dropped. We need-not bridge by package because Java
  853. * mapper interfaces are typically uniquely named within a project.
  854. */
  855. function mybatisJavaXmlEdges(queries: QueryBuilder): Edge[] {
  856. const edges: Edge[] = [];
  857. const seen = new Set<string>();
  858. // Index Java methods by `<ClassName>::<methodName>` for O(1) lookup.
  859. const javaIndex = new Map<string, Node[]>();
  860. for (const m of queries.getNodesByKind('method')) {
  861. if (m.language !== 'java' && m.language !== 'kotlin') continue;
  862. const parts = m.qualifiedName.split('::');
  863. const last = parts[parts.length - 1];
  864. const cls = parts[parts.length - 2];
  865. if (!last || !cls) continue;
  866. const key = `${cls}::${last}`;
  867. const arr = javaIndex.get(key);
  868. if (arr) arr.push(m); else javaIndex.set(key, [m]);
  869. }
  870. for (const xml of queries.getNodesByKind('method')) {
  871. if (xml.language !== 'xml') continue;
  872. // Qualified name: `<namespace>::<id>`. Extract the simple class name.
  873. const colonIdx = xml.qualifiedName.lastIndexOf('::');
  874. if (colonIdx < 0) continue;
  875. const namespace = xml.qualifiedName.slice(0, colonIdx);
  876. const id = xml.qualifiedName.slice(colonIdx + 2);
  877. if (!namespace || !id) continue;
  878. const dotIdx = namespace.lastIndexOf('.');
  879. const className = dotIdx >= 0 ? namespace.slice(dotIdx + 1) : namespace;
  880. const candidates = javaIndex.get(`${className}::${id}`);
  881. if (!candidates || candidates.length === 0) continue;
  882. // Drop ambiguous matches (multiple same-name classes); the user can
  883. // disambiguate by adding the package-suffix match in a future enhancement.
  884. if (candidates.length > 1) continue;
  885. const java = candidates[0]!;
  886. const key = `${java.id}>${xml.id}`;
  887. if (seen.has(key)) continue;
  888. seen.add(key);
  889. edges.push({
  890. source: java.id,
  891. target: xml.id,
  892. kind: 'calls',
  893. line: java.startLine,
  894. provenance: 'heuristic',
  895. metadata: {
  896. synthesizedBy: 'mybatis-java-xml',
  897. via: `${className}.${id}`,
  898. registeredAt: `${xml.filePath}:${xml.startLine}`,
  899. },
  900. });
  901. }
  902. return edges;
  903. }
  904. /**
  905. * Synthesize dispatcher→callback edges (field observers + EventEmitters +
  906. * React re-render + JSX children + Vue templates + RN event channel +
  907. * Fabric native-impl + MyBatis Java↔XML). Returns the count added. Never
  908. * throws into indexing — callers wrap in try/catch.
  909. */
  910. export function synthesizeCallbackEdges(queries: QueryBuilder, ctx: ResolutionContext): number {
  911. const fieldEdges = fieldChannelEdges(queries, ctx);
  912. const emitterEdges = eventEmitterEdges(ctx);
  913. const renderEdges = reactRenderEdges(queries, ctx);
  914. const jsxEdges = reactJsxChildEdges(ctx);
  915. const vueEdges = vueTemplateEdges(ctx);
  916. const flutterEdges = flutterBuildEdges(queries, ctx);
  917. const cppEdges = cppOverrideEdges(queries);
  918. const ifaceEdges = interfaceOverrideEdges(queries);
  919. const goGrpcEdges = goGrpcStubImplEdges(queries);
  920. const rnEventEdgesList = rnEventEdges(ctx);
  921. const fabricNativeEdges = fabricNativeImplEdges(ctx);
  922. const mybatisEdges = mybatisJavaXmlEdges(queries);
  923. const merged: Edge[] = [];
  924. const seen = new Set<string>();
  925. for (const e of [
  926. ...fieldEdges,
  927. ...emitterEdges,
  928. ...renderEdges,
  929. ...jsxEdges,
  930. ...vueEdges,
  931. ...flutterEdges,
  932. ...cppEdges,
  933. ...ifaceEdges,
  934. ...goGrpcEdges,
  935. ...rnEventEdgesList,
  936. ...fabricNativeEdges,
  937. ...mybatisEdges,
  938. ]) {
  939. const key = `${e.source}>${e.target}`;
  940. if (seen.has(key)) continue;
  941. seen.add(key);
  942. merged.push(e);
  943. }
  944. if (merged.length > 0) queries.insertEdges(merged);
  945. return merged.length;
  946. }