react-native-bridge.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  2. import * as fs from 'fs';
  3. import * as os from 'os';
  4. import * as path from 'path';
  5. import type { Node, Language } from '../src/types';
  6. import type { ResolutionContext, UnresolvedRef } from '../src/resolution/types';
  7. import { reactNativeBridgeResolver } from '../src/resolution/frameworks/react-native';
  8. import { CodeGraph } from '../src';
  9. /**
  10. * Mock ResolutionContext for the React Native bridge resolver.
  11. */
  12. function makeContext(nodes: Node[], fileContents: Record<string, string> = {}): ResolutionContext {
  13. const byName = new Map<string, Node[]>();
  14. for (const n of nodes) {
  15. const arr = byName.get(n.name);
  16. if (arr) arr.push(n);
  17. else byName.set(n.name, [n]);
  18. }
  19. // Files = union of node files + any extra fileContents keys (for files that
  20. // have content like .mm bridge declarations but no extracted nodes yet).
  21. const allFiles = new Set<string>(
  22. [...nodes.map((n) => n.filePath), ...Object.keys(fileContents)]
  23. );
  24. return {
  25. getNodesInFile: (fp) => nodes.filter((n) => n.filePath === fp),
  26. getNodesByName: (name) => byName.get(name) ?? [],
  27. getNodesByQualifiedName: () => { throw new Error('not used'); },
  28. getNodesByKind: (kind) => nodes.filter((n) => n.kind === kind),
  29. getNodesByLowerName: () => { throw new Error('not used'); },
  30. fileExists: (fp) => allFiles.has(fp),
  31. readFile: (fp) => fileContents[fp] ?? null,
  32. getProjectRoot: () => '/test',
  33. getAllFiles: () => Array.from(allFiles),
  34. getImportMappings: () => [],
  35. };
  36. }
  37. function method(
  38. name: string,
  39. language: Language,
  40. filePath: string,
  41. startLine = 10
  42. ): Node {
  43. return {
  44. id: `${language}:${filePath}:${name}:${startLine}`,
  45. kind: 'method',
  46. name,
  47. qualifiedName: `${filePath}::${name}`,
  48. filePath,
  49. language,
  50. startLine,
  51. endLine: startLine + 5,
  52. startColumn: 0,
  53. endColumn: 0,
  54. updatedAt: Date.now(),
  55. } as Node;
  56. }
  57. function ref(name: string, language: Language, filePath: string): UnresolvedRef {
  58. return {
  59. fromNodeId: `caller:${filePath}`,
  60. referenceName: name,
  61. referenceKind: 'calls',
  62. line: 1,
  63. column: 0,
  64. filePath,
  65. language,
  66. };
  67. }
  68. describe('React Native bridge resolver', () => {
  69. describe('detect()', () => {
  70. it('returns true when package.json declares react-native', () => {
  71. const ctx = makeContext([], {
  72. 'package.json':
  73. '{"name":"x","dependencies":{"react-native":"^0.73.0"}}',
  74. });
  75. expect(reactNativeBridgeResolver.detect(ctx)).toBe(true);
  76. });
  77. it('returns true when an ObjC file uses RCT_EXPORT_MODULE', () => {
  78. const ctx = makeContext([], {
  79. 'NativeFoo.mm': '@implementation Foo\nRCT_EXPORT_MODULE()\n@end',
  80. });
  81. expect(reactNativeBridgeResolver.detect(ctx)).toBe(true);
  82. });
  83. it('returns true when a TS file uses TurboModuleRegistry', () => {
  84. const ctx = makeContext([], {
  85. 'NativeFoo.ts':
  86. "import { TurboModuleRegistry } from 'react-native';\n" +
  87. "export default TurboModuleRegistry.getEnforcing<Spec>('Foo');",
  88. });
  89. expect(reactNativeBridgeResolver.detect(ctx)).toBe(true);
  90. });
  91. it('returns false when none of the RN signals are present', () => {
  92. const ctx = makeContext([method('hi', 'objc', 'X.m')]);
  93. expect(reactNativeBridgeResolver.detect(ctx)).toBe(false);
  94. });
  95. });
  96. describe('legacy bridge — ObjC side', () => {
  97. it('resolves JS callsite via RCT_EXPORT_METHOD with default module name', () => {
  98. // RCTGeolocation → module name 'Geolocation' (RCT prefix stripped).
  99. const native = method('getCurrentPosition:', 'objc', 'RCTGeolocation.m');
  100. const ctx = makeContext([native], {
  101. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  102. 'RCTGeolocation.m':
  103. '@implementation RCTGeolocation\n' +
  104. 'RCT_EXPORT_MODULE()\n' +
  105. 'RCT_EXPORT_METHOD(getCurrentPosition:(RCTResponseSenderBlock)cb) {}\n' +
  106. '@end',
  107. });
  108. const result = reactNativeBridgeResolver.resolve(
  109. ref('getCurrentPosition', 'javascript', 'App.js'),
  110. ctx
  111. );
  112. expect(result?.targetNodeId).toBe(native.id);
  113. expect(result?.resolvedBy).toBe('framework');
  114. });
  115. it('resolves via explicit module name in RCT_EXPORT_MODULE(name)', () => {
  116. const native = method('startScan:', 'objc', 'Bluetooth.m');
  117. const ctx = makeContext([native], {
  118. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  119. 'Bluetooth.m':
  120. '@implementation BluetoothImpl\n' +
  121. 'RCT_EXPORT_MODULE(BluetoothManager)\n' +
  122. 'RCT_EXPORT_METHOD(startScan:(RCTResponseSenderBlock)cb) {}\n' +
  123. '@end',
  124. });
  125. const result = reactNativeBridgeResolver.resolve(
  126. ref('startScan', 'javascript', 'App.js'),
  127. ctx
  128. );
  129. expect(result?.targetNodeId).toBe(native.id);
  130. });
  131. it('resolves RCT_REMAP_METHOD with JS-name override', () => {
  132. const native = method('doInternalCompute:', 'objc', 'Computer.m');
  133. const ctx = makeContext([native], {
  134. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  135. 'Computer.m':
  136. '@implementation Computer\n' +
  137. 'RCT_EXPORT_MODULE()\n' +
  138. 'RCT_REMAP_METHOD(compute, doInternalCompute:(NSDictionary *)opts) {}\n' +
  139. '@end',
  140. });
  141. const result = reactNativeBridgeResolver.resolve(
  142. ref('compute', 'javascript', 'App.js'),
  143. ctx
  144. );
  145. expect(result?.targetNodeId).toBe(native.id);
  146. });
  147. });
  148. describe('legacy bridge — Java side', () => {
  149. it('resolves @ReactMethod with getName() literal', () => {
  150. const native = method('getCurrentPosition', 'java', 'GeolocationModule.java');
  151. const ctx = makeContext([native], {
  152. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  153. 'GeolocationModule.java':
  154. 'class GeolocationModule extends ReactContextBaseJavaModule {\n' +
  155. ' @Override public String getName() { return "Geolocation"; }\n' +
  156. ' @ReactMethod public void getCurrentPosition(Callback cb) {}\n' +
  157. '}',
  158. });
  159. const result = reactNativeBridgeResolver.resolve(
  160. ref('getCurrentPosition', 'javascript', 'App.js'),
  161. ctx
  162. );
  163. expect(result?.targetNodeId).toBe(native.id);
  164. });
  165. it('resolves Kotlin @ReactMethod fun', () => {
  166. const native = method('startScan', 'kotlin', 'BluetoothModule.kt');
  167. const ctx = makeContext([native], {
  168. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  169. 'BluetoothModule.kt':
  170. 'class BluetoothModule(ctx: ReactApplicationContext) : ReactContextBaseJavaModule(ctx) {\n' +
  171. ' override fun getName(): String = "BluetoothManager"\n' +
  172. ' @ReactMethod fun startScan(cb: Callback) {}\n' +
  173. '}',
  174. });
  175. const result = reactNativeBridgeResolver.resolve(
  176. ref('startScan', 'javascript', 'App.js'),
  177. ctx
  178. );
  179. expect(result?.targetNodeId).toBe(native.id);
  180. });
  181. });
  182. describe('TurboModule spec resolution', () => {
  183. it('matches spec method to native ObjC implementation by name', () => {
  184. // The Spec interface lists `getTotalLength`; ObjC has a method by the
  185. // same first keyword. Bridge matches by name.
  186. const native = method('getTotalLength:', 'objc', 'RNSVGRenderableManager.mm');
  187. const ctx = makeContext([native], {
  188. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  189. 'NativeSvgRenderableModule.ts':
  190. "import { TurboModuleRegistry } from 'react-native';\n" +
  191. 'export interface Spec extends TurboModule {\n' +
  192. ' getTotalLength(tag: number): number;\n' +
  193. ' isPointInFill(tag: number, options?: object): boolean;\n' +
  194. '}\n' +
  195. "export default TurboModuleRegistry.getEnforcing<Spec>('RNSVGRenderableModule');",
  196. });
  197. const result = reactNativeBridgeResolver.resolve(
  198. ref('getTotalLength', 'tsx', 'SvgComponent.tsx'),
  199. ctx
  200. );
  201. expect(result?.targetNodeId).toBe(native.id);
  202. });
  203. it('returns null when spec method has no matching native impl', () => {
  204. const ctx = makeContext([], {
  205. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  206. 'NativeFoo.ts':
  207. "import { TurboModuleRegistry } from 'react-native';\n" +
  208. 'export interface Spec extends TurboModule {\n' +
  209. ' thingThatDoesntExist(): void;\n' +
  210. '}\n' +
  211. "export default TurboModuleRegistry.getEnforcing<Spec>('Foo');",
  212. });
  213. const result = reactNativeBridgeResolver.resolve(
  214. ref('thingThatDoesntExist', 'tsx', 'Caller.tsx'),
  215. ctx
  216. );
  217. expect(result).toBeNull();
  218. });
  219. });
  220. describe('qualified vs bare callsite names', () => {
  221. it('handles bare method name (post receiver-strip)', () => {
  222. const native = method('compute:', 'objc', 'Mod.m');
  223. const ctx = makeContext([native], {
  224. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  225. 'Mod.m':
  226. '@implementation Mod\nRCT_EXPORT_MODULE()\nRCT_EXPORT_METHOD(compute:(NSDictionary *)x) {}\n@end',
  227. });
  228. expect(
  229. reactNativeBridgeResolver.resolve(ref('compute', 'javascript', 'App.js'), ctx)
  230. ).not.toBeNull();
  231. });
  232. it('strips dot prefix on receiver-qualified callsite (NativeModules.Mod.compute → compute)', () => {
  233. const native = method('compute:', 'objc', 'Mod.m');
  234. const ctx = makeContext([native], {
  235. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  236. 'Mod.m':
  237. '@implementation Mod\nRCT_EXPORT_MODULE()\nRCT_EXPORT_METHOD(compute:(NSDictionary *)x) {}\n@end',
  238. });
  239. expect(
  240. reactNativeBridgeResolver.resolve(
  241. ref('NativeModules.Mod.compute', 'javascript', 'App.js'),
  242. ctx
  243. )
  244. ).not.toBeNull();
  245. });
  246. });
  247. it('does not resolve native-language callers (resolver is JS-side only)', () => {
  248. const native = method('compute:', 'objc', 'Mod.m');
  249. const ctx = makeContext([native]);
  250. expect(
  251. reactNativeBridgeResolver.resolve(ref('compute', 'objc', 'OtherMod.m'), ctx)
  252. ).toBeNull();
  253. });
  254. describe('RCTEventEmitter built-ins blocklist', () => {
  255. it('skips addListener / remove (every emitter exposes these — bridging them creates noise)', () => {
  256. // A repo with RCTEventEmitter subclass: defines `addListener:` and
  257. // `remove:` because that's what `[RCTEventEmitter addListener:]`
  258. // requires. JS callers of `.addListener(...)` should NOT resolve
  259. // here — they're hitting the JS-side `NativeEventEmitter`
  260. // abstraction, not the native emitter directly.
  261. const native1 = method('addListener:', 'objc', 'EventEmitter.m');
  262. const native2 = method('remove:', 'objc', 'EventEmitter.m');
  263. const ctx = makeContext([native1, native2], {
  264. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  265. 'EventEmitter.m':
  266. '@implementation EventEmitter\n' +
  267. 'RCT_EXPORT_MODULE()\n' +
  268. 'RCT_EXPORT_METHOD(addListener:(NSString *)eventName) {}\n' +
  269. 'RCT_EXPORT_METHOD(remove:(double)id) {}\n' +
  270. '@end',
  271. });
  272. expect(
  273. reactNativeBridgeResolver.resolve(ref('addListener', 'javascript', 'App.js'), ctx)
  274. ).toBeNull();
  275. expect(
  276. reactNativeBridgeResolver.resolve(ref('remove', 'typescript', 'App.ts'), ctx)
  277. ).toBeNull();
  278. });
  279. });
  280. });
  281. describe('React Native cross-platform pairing — end to end', () => {
  282. let dir: string;
  283. beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rn-xplat-')); });
  284. afterEach(() => { fs.rmSync(dir, { recursive: true, force: true }); });
  285. it('links the Android (@ReactMethod) and iOS (RCT_EXPORT_METHOD) impls of a JS-called method', async () => {
  286. fs.writeFileSync(path.join(dir, 'package.json'), '{"dependencies":{"react-native":"^0.74.0"}}');
  287. fs.writeFileSync(path.join(dir, 'index.ts'),
  288. "import { NativeModules } from 'react-native';\n" +
  289. "export function ping() { return NativeModules.RNThing.uniquePingMethod(); }\n");
  290. fs.writeFileSync(path.join(dir, 'RNThing.java'),
  291. "public class RNThing extends ReactContextBaseJavaModule {\n" +
  292. " @Override public String getName() { return \"RNThing\"; }\n" +
  293. " @ReactMethod public void uniquePingMethod(Callback cb) {}\n}\n");
  294. fs.writeFileSync(path.join(dir, 'RNThing.m'),
  295. "@implementation RNThing\n" +
  296. "RCT_EXPORT_MODULE()\n" +
  297. "RCT_EXPORT_METHOD(uniquePingMethod:(RCTResponseSenderBlock)cb) {}\n@end\n");
  298. const cg = await CodeGraph.init(dir, { silent: true });
  299. await cg.indexAll();
  300. const db = (cg as any).db.db;
  301. // The iOS `RCT_EXPORT_METHOD` is extracted as an ObjC method node (the macro
  302. // parses as a macro-expression, not a method, so it had no node before).
  303. const objc = db.prepare(
  304. "SELECT * FROM nodes WHERE name='uniquePingMethod' AND language='objc' AND id LIKE 'rn-export:%'"
  305. ).all();
  306. expect(objc).toHaveLength(1);
  307. // The Java and ObjC impls of `uniquePingMethod` are linked to each other, so
  308. // a JS call that resolves to one platform reaches the other.
  309. const pair = db.prepare(
  310. `SELECT count(*) c FROM edges e
  311. JOIN nodes s ON s.id=e.source JOIN nodes t ON t.id=e.target
  312. WHERE json_extract(e.metadata,'$.synthesizedBy')='rn-cross-platform'
  313. AND s.name LIKE 'uniquePingMethod%' AND t.name LIKE 'uniquePingMethod%'
  314. AND s.language != t.language`
  315. ).get();
  316. cg.close?.();
  317. expect(pair.c).toBeGreaterThanOrEqual(2); // java<->objc both directions
  318. });
  319. });
  320. // =============================================================================
  321. // Swift modules via RCT_EXTERN_MODULE, and receiver evidence
  322. // =============================================================================
  323. import { parseObjcRNExterns, collectNativeModuleAliases } from '../src/resolution/frameworks/react-native';
  324. function swiftMethod(name: string, owner: string, filePath: string, startLine: number): Node {
  325. return {
  326. id: `swift:${filePath}:${name}:${startLine}`,
  327. kind: 'method',
  328. name,
  329. qualifiedName: `${owner}::${name}`,
  330. filePath,
  331. language: 'swift',
  332. startLine,
  333. endLine: startLine + 4,
  334. startColumn: 0,
  335. endColumn: 0,
  336. updatedAt: Date.now(),
  337. } as Node;
  338. }
  339. const SHIM = `
  340. #import <React/RCTBridgeModule.h>
  341. #import <React/RCTViewManager.h>
  342. @interface RCT_EXTERN_MODULE(CaptureView, RCTViewManager)
  343. RCT_EXTERN_METHOD(syncSettings:(NSDictionary *)settings)
  344. RCT_EXTERN_METHOD(finalizeCaptureSession)
  345. RCT_EXTERN_REMAP_METHOD(pause, pauseInferenceNow)
  346. @end
  347. `;
  348. describe('React Native bridge resolver — RCT_EXTERN (Swift) modules', () => {
  349. const finalize = swiftMethod('finalizeCaptureSession', 'CaptureView', 'ios/CaptureView+ReactBridge.swift', 26);
  350. const sync = swiftMethod('syncSettings', 'CaptureView', 'ios/CaptureView.swift', 40);
  351. const pause = swiftMethod('pauseInferenceNow', 'CaptureView', 'ios/CaptureView.swift', 60);
  352. // Same method name on another Swift type — never the bridge target.
  353. const decoy = swiftMethod('syncSettings', 'CaptureSettings', 'ios/CaptureSettings.swift', 12);
  354. const files = {
  355. 'package.json': '{"name":"app","dependencies":{"react-native":"0.76"}}',
  356. 'ios/CaptureView.m': SHIM,
  357. 'src/components/capture/capture-view.tsx':
  358. "import { NativeModules, NativeEventEmitter } from 'react-native'\n" +
  359. 'export const { CaptureEvents } = NativeModules\n' +
  360. 'export const captureView = NativeModules.CaptureView\n',
  361. };
  362. const ctx = makeContext([finalize, sync, pause, decoy], files);
  363. it('parses the shim: module, class, first keyword, remap', () => {
  364. expect(parseObjcRNExterns(SHIM).map((e) => [e.moduleName, e.className, e.jsName, e.nativeSelectorFirstKw])).toEqual([
  365. ['CaptureView', 'CaptureView', 'syncSettings', 'syncSettings'],
  366. ['CaptureView', 'CaptureView', 'finalizeCaptureSession', 'finalizeCaptureSession'],
  367. ['CaptureView', 'CaptureView', 'pause', 'pauseInferenceNow'],
  368. ]);
  369. const remapped = parseObjcRNExterns('@interface RCT_EXTERN_REMAP_MODULE(Camera, CameraModule, NSObject)\nRCT_EXTERN_METHOD(snap)');
  370. expect(remapped).toEqual([
  371. { moduleName: 'Camera', className: 'CameraModule', jsName: 'snap', nativeSelectorFirstKw: 'snap', line: 2 },
  372. ]);
  373. });
  374. it('collects the local names bound to NativeModules', () => {
  375. const aliases = new Map<string, string>();
  376. const ambiguous = new Set<string>();
  377. collectNativeModuleAliases(
  378. 'const captureView = NativeModules.CaptureView\n' +
  379. 'export const { CaptureEvents, Geo: geolocation } = NativeModules\n' +
  380. 'let typed: Spec = NativeModules.Typed\n',
  381. aliases,
  382. ambiguous
  383. );
  384. // Direct bindings first (one pass), then the destructured ones.
  385. expect([...aliases]).toEqual([
  386. ['captureView', 'CaptureView'],
  387. ['typed', 'Typed'],
  388. ['CaptureEvents', 'CaptureEvents'],
  389. ['geolocation', 'Geo'],
  390. ]);
  391. // The same name bound to two modules is dropped, not guessed.
  392. collectNativeModuleAliases('const captureView = NativeModules.Other', aliases, ambiguous);
  393. expect(aliases.has('captureView')).toBe(false);
  394. expect(ambiguous.has('captureView')).toBe(true);
  395. });
  396. it('detects a project from the RCT_EXTERN_MODULE marker alone', () => {
  397. expect(reactNativeBridgeResolver.detect(makeContext([], { 'ios/CaptureView.m': SHIM }))).toBe(true);
  398. });
  399. it('resolves an aliased receiver to the Swift method of the named class at 0.95', () => {
  400. const r = reactNativeBridgeResolver.resolve(
  401. ref('captureView.finalizeCaptureSession', 'tsx', 'src/hooks/use-review-handlers.ts'),
  402. ctx
  403. );
  404. expect(r?.targetNodeId).toBe(finalize.id);
  405. expect(r?.confidence).toBe(0.95);
  406. expect(r?.metadata).toEqual({ bridge: 'react-native', module: 'CaptureView' });
  407. });
  408. it('resolves NativeModules.Module.method the same way, class-scoped past a same-named decoy', () => {
  409. const r = reactNativeBridgeResolver.resolve(ref('NativeModules.CaptureView.syncSettings', 'tsx', 'src/a.tsx'), ctx);
  410. expect(r?.targetNodeId).toBe(sync.id);
  411. expect(r?.confidence).toBe(0.95);
  412. });
  413. it('follows RCT_EXTERN_REMAP_METHOD to the Swift implementation under the JS name', () => {
  414. const r = reactNativeBridgeResolver.resolve(ref('captureView.pause', 'tsx', 'src/a.tsx'), ctx);
  415. expect(r?.targetNodeId).toBe(pause.id);
  416. });
  417. it('keeps a bare method name at the by-name confidence, and refuses a named module that lacks the method', () => {
  418. const bare = reactNativeBridgeResolver.resolve(ref('syncSettings', 'tsx', 'src/a.tsx'), ctx);
  419. expect(bare?.targetNodeId).toBe(sync.id);
  420. expect(bare?.confidence).toBe(0.6);
  421. expect(reactNativeBridgeResolver.resolve(ref('captureView.nothingHere', 'tsx', 'src/a.tsx'), ctx)).toBeNull();
  422. // A receiver that is NOT a module alias falls back to by-name evidence.
  423. const other = reactNativeBridgeResolver.resolve(ref('somethingElse.syncSettings', 'tsx', 'src/a.tsx'), ctx);
  424. expect(other?.confidence).toBe(0.6);
  425. });
  426. it('never redirects a native caller', () => {
  427. expect(reactNativeBridgeResolver.resolve(ref('captureView.finalizeCaptureSession', 'swift', 'ios/x.swift'), ctx)).toBeNull();
  428. });
  429. });