1
0

react-native-bridge.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { describe, it, expect } from 'vitest';
  2. import type { Node, Language } from '../src/types';
  3. import type { ResolutionContext, UnresolvedRef } from '../src/resolution/types';
  4. import { reactNativeBridgeResolver } from '../src/resolution/frameworks/react-native';
  5. /**
  6. * Mock ResolutionContext for the React Native bridge resolver.
  7. */
  8. function makeContext(nodes: Node[], fileContents: Record<string, string> = {}): ResolutionContext {
  9. const byName = new Map<string, Node[]>();
  10. for (const n of nodes) {
  11. const arr = byName.get(n.name);
  12. if (arr) arr.push(n);
  13. else byName.set(n.name, [n]);
  14. }
  15. // Files = union of node files + any extra fileContents keys (for files that
  16. // have content like .mm bridge declarations but no extracted nodes yet).
  17. const allFiles = new Set<string>(
  18. [...nodes.map((n) => n.filePath), ...Object.keys(fileContents)]
  19. );
  20. return {
  21. getNodesInFile: (fp) => nodes.filter((n) => n.filePath === fp),
  22. getNodesByName: (name) => byName.get(name) ?? [],
  23. getNodesByQualifiedName: () => { throw new Error('not used'); },
  24. getNodesByKind: (kind) => nodes.filter((n) => n.kind === kind),
  25. getNodesByLowerName: () => { throw new Error('not used'); },
  26. fileExists: (fp) => allFiles.has(fp),
  27. readFile: (fp) => fileContents[fp] ?? null,
  28. getProjectRoot: () => '/test',
  29. getAllFiles: () => Array.from(allFiles),
  30. getImportMappings: () => [],
  31. };
  32. }
  33. function method(
  34. name: string,
  35. language: Language,
  36. filePath: string,
  37. startLine = 10
  38. ): Node {
  39. return {
  40. id: `${language}:${filePath}:${name}:${startLine}`,
  41. kind: 'method',
  42. name,
  43. qualifiedName: `${filePath}::${name}`,
  44. filePath,
  45. language,
  46. startLine,
  47. endLine: startLine + 5,
  48. startColumn: 0,
  49. endColumn: 0,
  50. updatedAt: Date.now(),
  51. } as Node;
  52. }
  53. function ref(name: string, language: Language, filePath: string): UnresolvedRef {
  54. return {
  55. fromNodeId: `caller:${filePath}`,
  56. referenceName: name,
  57. referenceKind: 'calls',
  58. line: 1,
  59. column: 0,
  60. filePath,
  61. language,
  62. };
  63. }
  64. describe('React Native bridge resolver', () => {
  65. describe('detect()', () => {
  66. it('returns true when package.json declares react-native', () => {
  67. const ctx = makeContext([], {
  68. 'package.json':
  69. '{"name":"x","dependencies":{"react-native":"^0.73.0"}}',
  70. });
  71. expect(reactNativeBridgeResolver.detect(ctx)).toBe(true);
  72. });
  73. it('returns true when an ObjC file uses RCT_EXPORT_MODULE', () => {
  74. const ctx = makeContext([], {
  75. 'NativeFoo.mm': '@implementation Foo\nRCT_EXPORT_MODULE()\n@end',
  76. });
  77. expect(reactNativeBridgeResolver.detect(ctx)).toBe(true);
  78. });
  79. it('returns true when a TS file uses TurboModuleRegistry', () => {
  80. const ctx = makeContext([], {
  81. 'NativeFoo.ts':
  82. "import { TurboModuleRegistry } from 'react-native';\n" +
  83. "export default TurboModuleRegistry.getEnforcing<Spec>('Foo');",
  84. });
  85. expect(reactNativeBridgeResolver.detect(ctx)).toBe(true);
  86. });
  87. it('returns false when none of the RN signals are present', () => {
  88. const ctx = makeContext([method('hi', 'objc', 'X.m')]);
  89. expect(reactNativeBridgeResolver.detect(ctx)).toBe(false);
  90. });
  91. });
  92. describe('legacy bridge — ObjC side', () => {
  93. it('resolves JS callsite via RCT_EXPORT_METHOD with default module name', () => {
  94. // RCTGeolocation → module name 'Geolocation' (RCT prefix stripped).
  95. const native = method('getCurrentPosition:', 'objc', 'RCTGeolocation.m');
  96. const ctx = makeContext([native], {
  97. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  98. 'RCTGeolocation.m':
  99. '@implementation RCTGeolocation\n' +
  100. 'RCT_EXPORT_MODULE()\n' +
  101. 'RCT_EXPORT_METHOD(getCurrentPosition:(RCTResponseSenderBlock)cb) {}\n' +
  102. '@end',
  103. });
  104. const result = reactNativeBridgeResolver.resolve(
  105. ref('getCurrentPosition', 'javascript', 'App.js'),
  106. ctx
  107. );
  108. expect(result?.targetNodeId).toBe(native.id);
  109. expect(result?.resolvedBy).toBe('framework');
  110. });
  111. it('resolves via explicit module name in RCT_EXPORT_MODULE(name)', () => {
  112. const native = method('startScan:', 'objc', 'Bluetooth.m');
  113. const ctx = makeContext([native], {
  114. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  115. 'Bluetooth.m':
  116. '@implementation BluetoothImpl\n' +
  117. 'RCT_EXPORT_MODULE(BluetoothManager)\n' +
  118. 'RCT_EXPORT_METHOD(startScan:(RCTResponseSenderBlock)cb) {}\n' +
  119. '@end',
  120. });
  121. const result = reactNativeBridgeResolver.resolve(
  122. ref('startScan', 'javascript', 'App.js'),
  123. ctx
  124. );
  125. expect(result?.targetNodeId).toBe(native.id);
  126. });
  127. it('resolves RCT_REMAP_METHOD with JS-name override', () => {
  128. const native = method('doInternalCompute:', 'objc', 'Computer.m');
  129. const ctx = makeContext([native], {
  130. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  131. 'Computer.m':
  132. '@implementation Computer\n' +
  133. 'RCT_EXPORT_MODULE()\n' +
  134. 'RCT_REMAP_METHOD(compute, doInternalCompute:(NSDictionary *)opts) {}\n' +
  135. '@end',
  136. });
  137. const result = reactNativeBridgeResolver.resolve(
  138. ref('compute', 'javascript', 'App.js'),
  139. ctx
  140. );
  141. expect(result?.targetNodeId).toBe(native.id);
  142. });
  143. });
  144. describe('legacy bridge — Java side', () => {
  145. it('resolves @ReactMethod with getName() literal', () => {
  146. const native = method('getCurrentPosition', 'java', 'GeolocationModule.java');
  147. const ctx = makeContext([native], {
  148. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  149. 'GeolocationModule.java':
  150. 'class GeolocationModule extends ReactContextBaseJavaModule {\n' +
  151. ' @Override public String getName() { return "Geolocation"; }\n' +
  152. ' @ReactMethod public void getCurrentPosition(Callback cb) {}\n' +
  153. '}',
  154. });
  155. const result = reactNativeBridgeResolver.resolve(
  156. ref('getCurrentPosition', 'javascript', 'App.js'),
  157. ctx
  158. );
  159. expect(result?.targetNodeId).toBe(native.id);
  160. });
  161. it('resolves Kotlin @ReactMethod fun', () => {
  162. const native = method('startScan', 'kotlin', 'BluetoothModule.kt');
  163. const ctx = makeContext([native], {
  164. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  165. 'BluetoothModule.kt':
  166. 'class BluetoothModule(ctx: ReactApplicationContext) : ReactContextBaseJavaModule(ctx) {\n' +
  167. ' override fun getName(): String = "BluetoothManager"\n' +
  168. ' @ReactMethod fun startScan(cb: Callback) {}\n' +
  169. '}',
  170. });
  171. const result = reactNativeBridgeResolver.resolve(
  172. ref('startScan', 'javascript', 'App.js'),
  173. ctx
  174. );
  175. expect(result?.targetNodeId).toBe(native.id);
  176. });
  177. });
  178. describe('TurboModule spec resolution', () => {
  179. it('matches spec method to native ObjC implementation by name', () => {
  180. // The Spec interface lists `getTotalLength`; ObjC has a method by the
  181. // same first keyword. Bridge matches by name.
  182. const native = method('getTotalLength:', 'objc', 'RNSVGRenderableManager.mm');
  183. const ctx = makeContext([native], {
  184. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  185. 'NativeSvgRenderableModule.ts':
  186. "import { TurboModuleRegistry } from 'react-native';\n" +
  187. 'export interface Spec extends TurboModule {\n' +
  188. ' getTotalLength(tag: number): number;\n' +
  189. ' isPointInFill(tag: number, options?: object): boolean;\n' +
  190. '}\n' +
  191. "export default TurboModuleRegistry.getEnforcing<Spec>('RNSVGRenderableModule');",
  192. });
  193. const result = reactNativeBridgeResolver.resolve(
  194. ref('getTotalLength', 'tsx', 'SvgComponent.tsx'),
  195. ctx
  196. );
  197. expect(result?.targetNodeId).toBe(native.id);
  198. });
  199. it('returns null when spec method has no matching native impl', () => {
  200. const ctx = makeContext([], {
  201. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  202. 'NativeFoo.ts':
  203. "import { TurboModuleRegistry } from 'react-native';\n" +
  204. 'export interface Spec extends TurboModule {\n' +
  205. ' thingThatDoesntExist(): void;\n' +
  206. '}\n' +
  207. "export default TurboModuleRegistry.getEnforcing<Spec>('Foo');",
  208. });
  209. const result = reactNativeBridgeResolver.resolve(
  210. ref('thingThatDoesntExist', 'tsx', 'Caller.tsx'),
  211. ctx
  212. );
  213. expect(result).toBeNull();
  214. });
  215. });
  216. describe('qualified vs bare callsite names', () => {
  217. it('handles bare method name (post receiver-strip)', () => {
  218. const native = method('compute:', 'objc', 'Mod.m');
  219. const ctx = makeContext([native], {
  220. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  221. 'Mod.m':
  222. '@implementation Mod\nRCT_EXPORT_MODULE()\nRCT_EXPORT_METHOD(compute:(NSDictionary *)x) {}\n@end',
  223. });
  224. expect(
  225. reactNativeBridgeResolver.resolve(ref('compute', 'javascript', 'App.js'), ctx)
  226. ).not.toBeNull();
  227. });
  228. it('strips dot prefix on receiver-qualified callsite (NativeModules.Mod.compute → compute)', () => {
  229. const native = method('compute:', 'objc', 'Mod.m');
  230. const ctx = makeContext([native], {
  231. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  232. 'Mod.m':
  233. '@implementation Mod\nRCT_EXPORT_MODULE()\nRCT_EXPORT_METHOD(compute:(NSDictionary *)x) {}\n@end',
  234. });
  235. expect(
  236. reactNativeBridgeResolver.resolve(
  237. ref('NativeModules.Mod.compute', 'javascript', 'App.js'),
  238. ctx
  239. )
  240. ).not.toBeNull();
  241. });
  242. });
  243. it('does not resolve native-language callers (resolver is JS-side only)', () => {
  244. const native = method('compute:', 'objc', 'Mod.m');
  245. const ctx = makeContext([native]);
  246. expect(
  247. reactNativeBridgeResolver.resolve(ref('compute', 'objc', 'OtherMod.m'), ctx)
  248. ).toBeNull();
  249. });
  250. describe('RCTEventEmitter built-ins blocklist', () => {
  251. it('skips addListener / remove (every emitter exposes these — bridging them creates noise)', () => {
  252. // A repo with RCTEventEmitter subclass: defines `addListener:` and
  253. // `remove:` because that's what `[RCTEventEmitter addListener:]`
  254. // requires. JS callers of `.addListener(...)` should NOT resolve
  255. // here — they're hitting the JS-side `NativeEventEmitter`
  256. // abstraction, not the native emitter directly.
  257. const native1 = method('addListener:', 'objc', 'EventEmitter.m');
  258. const native2 = method('remove:', 'objc', 'EventEmitter.m');
  259. const ctx = makeContext([native1, native2], {
  260. 'package.json': '{"dependencies":{"react-native":"^0.73"}}',
  261. 'EventEmitter.m':
  262. '@implementation EventEmitter\n' +
  263. 'RCT_EXPORT_MODULE()\n' +
  264. 'RCT_EXPORT_METHOD(addListener:(NSString *)eventName) {}\n' +
  265. 'RCT_EXPORT_METHOD(remove:(double)id) {}\n' +
  266. '@end',
  267. });
  268. expect(
  269. reactNativeBridgeResolver.resolve(ref('addListener', 'javascript', 'App.js'), ctx)
  270. ).toBeNull();
  271. expect(
  272. reactNativeBridgeResolver.resolve(ref('remove', 'typescript', 'App.ts'), ctx)
  273. ).toBeNull();
  274. });
  275. });
  276. });