Files
codegraph/__tests__/swift-objc-bridge.test.ts
T
Colby MchenryandGitHub 4d1a2b3c4d feat(resolution): mixed iOS / React Native / Expo cross-language bridging (#430)
Implements the design from `docs/design/mixed-ios-and-react-native-bridging.md`.
Closes the cross-language flow gap so `trace` / `callers` / `callees` / `impact` connect end-to-end across language boundaries in real iOS, React Native, and Expo codebases.

## Bridges shipped

| Boundary | Mechanism | Real-codebase validation |
|---|---|---|
| **Swift ↔ Objective-C** | Resolver applying Apple's @objc auto-bridging name math + Cocoa preposition prefixes | Charts (S, 269) · realm-swift (M, 369) · wikipedia-ios (L, 1734) |
| **React Native legacy bridge** | Resolver parsing `RCT_EXPORT_MODULE` / `RCT_EXPORT_METHOD` / `RCT_REMAP_METHOD` (ObjC) + `@ReactMethod` (Java/Kotlin) | AsyncStorage (S, ~60) · react-native-svg (M, ~700) · react-native-firebase (L, ~1100) |
| **React Native TurboModules** | Resolver treating `Native<X>.ts` spec interface as ground truth | via RNSvg + RNFirebase subsets |
| **Native → JS events** | Synthesizer matching native `sendEventWithName:`/`emit(...)` to JS `addListener('e', handler)` keyed by literal event name; falls back to enclosing constant/variable for wrapper-API parameter handlers | RNGeolocation (S) · RNFirebase (L) |
| **Expo Modules** | Framework extract synthesizes `method` nodes from Swift/Kotlin `Module { Name("X"); Function("y") { ... } }` DSL | expo-haptics (S, 14) · expo-camera (M, 72) · ExpoSweep (L, 332, 7 packages) |
| **Fabric + legacy Paper view components** | Extract `component` + `property` nodes from Codegen `codegenNativeComponent<Props>('Name', ...)` specs AND legacy `RCT_EXPORT_VIEW_PROPERTY` / `@ReactProp` macros, then synthesize component → native class by name+suffix convention | react-native-segmented-control (S, legacy) · react-native-screens (M, Codegen) · react-native-skia (L, hybrid monorepo) |

## Bug fixes surfaced along the way

- `tree-sitter.ts` message_expression — multi-keyword ObjC call sites now reconstruct `a🅱️` selectors so they resolve to multi-part method definitions (gap discovered post-#165; 0 → 84 call edges to `GET:parameters:...` style methods on AFNetworking).
- `src/index.ts` resolver lifecycle — `indexAll()` now re-initializes the resolver after extraction so framework `detect()` sees the populated index. Pre-existing latent bug that affected UIKit and SwiftUI resolvers too.
- `src/extraction/index.ts` `buildDetectionContext` — added `listDirectories` so framework detect() can probe monorepo subpackages uniformly (fix needed for react-native-skia detection).

## Regression check on 5 control repos

| Repo | Result |
|---|---|
| Express (small JS) |  unchanged — 266 routes, express framework detected |
| Excalidraw (medium TS/React) |  9284 nodes (CLAUDE.md baseline ~9290); canonical `trace(mutateElement, renderStaticScene)` returns the flow |
| Django realworld (Python) |  django framework detected, 16 routes |
| Spring petclinic (Java) |  spring framework detected, 17 routes |
| Texture (pure ObjC, large) |  exactly matches #165 baseline: 4702 methods, 894 classes, 808/808 file coverage, 913 multi-keyword selectors, 55 protocols, 1036 properties |

## Tests

928 passing (+87 net new bridge tests across the 5 channels); 2 pre-existing skips. The mcp-staleness-banner / watcher parallel flakiness is unchanged by this work (different test fails each run, all pass in isolation; pre-existing on main).

## Documentation

- README: new 'Mixed iOS / React Native / Expo bridging' section with the per-boundary table and validation-corpus links.
- CHANGELOG `[Unreleased]`: full entry per bridge with measurements.
- `docs/design/mixed-ios-and-react-native-bridging.md`: the design doc (§8 measurements filled in across §8a-§8g).
- `docs/design/dynamic-dispatch-coverage-playbook.md` §6 coverage matrix: six new rows.
- `.claude/skills/agent-eval/corpus.json`: four new sections covering 15 real GitHub repos for the eval harness.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-05-26 02:14:00 -05:00

190 lines
6.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
objcSelectorForSwiftMethod,
objcSelectorForSwiftInit,
objcAccessorsForSwiftProperty,
swiftBaseNamesForObjcSelector,
detectExplicitObjcName,
isObjcExposed,
} from '../src/resolution/swift-objc-bridge';
describe('Swift → ObjC selector bridging (auto-name rules)', () => {
describe('objcSelectorForSwiftMethod', () => {
it('no parameters → bare base name', () => {
expect(objcSelectorForSwiftMethod('play', [])).toBe('play');
});
it('single _ param → base + ":"', () => {
expect(objcSelectorForSwiftMethod('play', ['_'])).toBe('play:');
expect(objcSelectorForSwiftMethod('play', [null])).toBe('play:');
});
it('single labeled param → "baseWithLabel:"', () => {
expect(objcSelectorForSwiftMethod('play', ['song'])).toBe('playWithSong:');
});
it('multi-param with leading _ → "base:label2:..."', () => {
expect(objcSelectorForSwiftMethod('play', ['_', 'by'])).toBe('play:by:');
expect(
objcSelectorForSwiftMethod('tableView', ['_', 'didSelectRowAtIndexPath'])
).toBe('tableView:didSelectRowAtIndexPath:');
});
it('multi-param with leading explicit label → "baseWithFirst:rest:"', () => {
expect(objcSelectorForSwiftMethod('play', ['song', 'by'])).toBe(
'playWithSong:by:'
);
});
it('@objc(custom:) overrides the rule literally', () => {
expect(
objcSelectorForSwiftMethod('whateverName', ['ignored'], 'custom:')
).toBe('custom:');
});
it('returns null on empty base name', () => {
expect(objcSelectorForSwiftMethod('', [])).toBeNull();
});
});
describe('objcSelectorForSwiftInit', () => {
it('init() → "init"', () => {
expect(objcSelectorForSwiftInit([], [])).toBe('init');
});
it('init(name:) → "initWithName:"', () => {
expect(objcSelectorForSwiftInit(['name'], ['name'])).toBe('initWithName:');
});
it('init(name:, age:) → "initWithName:age:"', () => {
expect(objcSelectorForSwiftInit(['name', 'age'], ['name', 'age'])).toBe(
'initWithName:age:'
);
});
it('init(_ name:) uses internal name → "initWithName:"', () => {
expect(objcSelectorForSwiftInit(['_'], ['name'])).toBe('initWithName:');
});
it('@objc(custom) override on init', () => {
expect(objcSelectorForSwiftInit(['name'], ['name'], 'custom:')).toBe(
'custom:'
);
});
});
describe('objcAccessorsForSwiftProperty', () => {
it('getter = name, setter = setName:', () => {
expect(objcAccessorsForSwiftProperty('name')).toEqual({
getter: 'name',
setter: 'setName:',
});
});
it('camelCase → set capitalizes first', () => {
expect(objcAccessorsForSwiftProperty('isReady')).toEqual({
getter: 'isReady',
setter: 'setIsReady:',
});
});
it('explicit @objc(custom) overrides getter name', () => {
expect(objcAccessorsForSwiftProperty('name', 'displayName')).toEqual({
getter: 'displayName',
setter: 'setDisplayName:',
});
});
});
});
describe('ObjC selector → Swift base name candidates (reverse map)', () => {
it('bare no-colon selector → itself', () => {
expect(swiftBaseNamesForObjcSelector('play')).toEqual(['play']);
});
it('"play:" → ["play"]', () => {
expect(swiftBaseNamesForObjcSelector('play:')).toEqual(['play']);
});
it('"playWithSong:" → ["playWithSong", "play"]', () => {
expect(swiftBaseNamesForObjcSelector('playWithSong:').sort()).toEqual(
['play', 'playWithSong'].sort()
);
});
it('Cocoa-style "objectForKey:" → includes "object"', () => {
expect(swiftBaseNamesForObjcSelector('objectForKey:')).toContain('object');
});
it('Cocoa-style "stringWithFormat:" → includes "string"', () => {
expect(swiftBaseNamesForObjcSelector('stringWithFormat:')).toContain('string');
});
it('Cocoa-style "imageNamed:inBundle:" → first keyword has no preposition, falls through', () => {
// First keyword is `imageNamed` — no With/For/By in it, so candidates is
// just the raw keyword. (`Named` is not in our preposition list — keep
// it that way, otherwise we over-match on perfectly normal verbs.)
expect(swiftBaseNamesForObjcSelector('imageNamed:inBundle:')).toEqual(['imageNamed']);
});
it('"play:by:" → ["play"]', () => {
expect(swiftBaseNamesForObjcSelector('play:by:')).toEqual(['play']);
});
it('"playWithSong:by:" → ["playWithSong", "play"]', () => {
expect(swiftBaseNamesForObjcSelector('playWithSong:by:').sort()).toEqual(
['play', 'playWithSong'].sort()
);
});
it('"initWithName:" → includes "init"', () => {
expect(swiftBaseNamesForObjcSelector('initWithName:')).toContain('init');
});
it('"initWithName:age:" → includes "init"', () => {
expect(swiftBaseNamesForObjcSelector('initWithName:age:')).toContain('init');
});
it('"setName:" → includes the property name "name"', () => {
expect(swiftBaseNamesForObjcSelector('setName:')).toContain('name');
});
it('"tableView:didSelectRowAtIndexPath:" → ["tableView"]', () => {
expect(
swiftBaseNamesForObjcSelector('tableView:didSelectRowAtIndexPath:')
).toEqual(['tableView']);
});
});
describe('Source-window attribute detection', () => {
it('detects literal @objc(custom)', () => {
expect(detectExplicitObjcName(' @objc(custom:)\n func foo() {}')).toBe(
'custom:'
);
});
it('returns null for plain @objc', () => {
expect(detectExplicitObjcName('@objc func foo() {}')).toBeNull();
});
it('returns null when no @objc at all', () => {
expect(detectExplicitObjcName('public func foo() {}')).toBeNull();
});
it('isObjcExposed true for @objc', () => {
expect(isObjcExposed('@objc func foo() {}')).toBe(true);
});
it('isObjcExposed true for @objc(custom)', () => {
expect(isObjcExposed('@objc(custom:) func foo() {}')).toBe(true);
});
it('isObjcExposed false for no annotation', () => {
expect(isObjcExposed('public func foo() {}')).toBe(false);
});
it('@nonobjc opts out even if @objc also present (e.g. inside @objcMembers class)', () => {
expect(isObjcExposed('@nonobjc @objc func foo() {}')).toBe(false);
});
});