1
0

react-native-bridge.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. });