混合 iOS / React Native / Expo 橋接

呼叫路徑跨語言邊界——不再是「Swift↔ObjC 斷在 static parsing」
8 種邊界 · 全部標註 provenance:'heuristic' + synthesizedBy channel

問題

真實的 iOS / React Native codebase 橫跨多種語言——Swift caller 呼叫被自動橋接的 Objective-C selector、JS 檔經由 RN bridge 呼叫 native module、JSX component 委派給 native view manager。純 tree-sitter 抽取在每個語言邊界就斷了。CodeGraph 把它們接起來,讓 codegraph_explore 端到端連通這些流——call path 與 blast radius 跨過邊界,而不是停在邊界。

8 種邊界

邊界JS / Swift 側Native 側做法
Swift → ObjCSwift obj.foo(bar:)ObjC selector -fooWithBar:@objc auto-bridging 規則(含 init/property/protocol 形式)+ Cocoa preposition 前綴(With/For/By/In/On/At…)
ObjC → SwiftObjC [obj fooWithBar:]Swift @objc func foo(bar:)Reverse-bridge 名稱候選;從原始碼驗證 @objc 暴露
RN legacy bridgeJS NativeModules.X.fn(...)ObjC RCT_EXPORT_METHOD/RCT_REMAP_METHOD · Java/Kotlin @ReactMethod解析 macro/annotation 宣告,建 JS-name → native-method map
RN TurboModulesJS import M from './NativeM'; M.fn(...)符合 Codegen spec 的 native impl把 Native<X>.ts spec interface 當 ground truth
RN native → JS eventsJS new NativeEventEmitter(...).addListener('e', cb)ObjC [self sendEventWithName:@"e" body:...] · Swift sendEvent(withName: "e", ...) · Java/Kotlin .emit("e", ...)依字面 event name 合成跨語言事件通道
Expo ModulesJS requireNativeModule('X').fn(...)Swift / Kotlin Module { Name("X"); AsyncFunction("fn") { ... } }解析 Expo DSL 字面值;synthetic method 節點用既有 name-match 解析
Fabric view componentsJSX <MyView prop={v}/>TS Codegen spec + native impl classSpec → component 節點;慣例名稱+suffix(View/ComponentView/Manager/ViewManager)橋到 native
Legacy Paper view managersJSX <MyView prop={v}/>ObjC RCT_EXPORT_VIEW_PROPERTY · Java/Kotlin @ReactProp與 Fabric 相同——Paper 時代宣告也產 component + property 節點

可追溯性

每個 bridge 產出的邊都標 provenance:'heuristic',且 metadata.synthesizedBy: 設為穩定 channel 名(如 swift-objc-bridge、rn-event-channel、fabric-native-impl、expo-module-extract)——agent 一眼就能看出某個 hop 是怎麼進圖的。

已在真實 codebase 驗證

BridgeSmallMediumLarge
Swift ↔ ObjCChartsrealm-swiftWikipedia-iOS
RN legacy bridgeAsyncStoragereact-native-svgreact-native-firebase
RN native → JS eventsRNGeolocation—react-native-firebase
Expo Modulesexpo-hapticsexpo-cameraexpo SDK sweep(7 packages)
Fabric / Paper viewsreact-native-segmented-controlreact-native-screensreact-native-skia
看完這頁你應該能說出:為什麼 tree-sitter 在語言邊界斷流、Swift↔ObjC 與 RN bridge 各自靠什麼規則跨界、以及 synthesizedBy channel 對「agent 判斷 hop 可信度」的價值。

🎓 教學解說

跨語言橋接是 CodeGraph 的「殺手級功能」——它能追蹤跨越語言邊界的呼叫路徑,這是純靜態分析工具做不到的。

橋接的兩種策略:

Worked Example:追蹤 React Native 的 Native Module

RN 橋接實測
# 步驟 1:搜尋 Native Module
codegraph query NativeModules --kind import
# Search Results for "NativeModules":
#   import      NativeModules
#     src/modules/DeviceInfo.ts:1

# 步驟 2:查看 JS 側的呼叫
codegraph callers DeviceInfo.getDeviceId
# Callers of "DeviceInfo.getDeviceId" (3):
#   method      getDeviceId
#     src/modules/DeviceInfo.ts:15
#   file        App.tsx
#     src/App.tsx:45

# 步驟 3:查看 Native 側的實作
codegraph node RCT_EXPORT_METHOD
# 查找 ObjC 側的 @ReactMethod 宣告

# 步驟 4:查看跨語言事件通道
codegraph query NativeEventEmitter --kind import
# 找到 JS → Native 的事件監聽

關鍵觀察:從 JS 的 NativeModules.X.fn(...) 到 ObjC 的 RCT_EXPORT_METHOD,整個呼叫鏈一次可見。這在大型 RN 專案中特別有價值。

常見錯誤與診斷

症狀原因解法
跨語言連結未出現命名不符合慣例確認命名符合 @objc / @ReactMethod 慣例
heuristic 連結不準確名稱衝突或非標準寫法用 codegraph node 手動驗證
RN bridge 未解析使用 legacy bridge 而非 TurboModules確認 bridge 類型,檢查 synthesizedBy channel

練習:驗收清單


🔍 進階真實情境 Worked Example:大型 Swift/ObjC 混血 app 的跨語言影響評估

前面的範例是追蹤單一 RN Native Module。這裡是大型原生 app:Swift 呼叫被自動橋接的 ObjC selector,你要改一個 legacy ObjC method,先評估「誰會透過 Swift 被炸到」。

跨語言影響評估工作流
# ① 找出該 selector 的 ObjC 定義
codegraph query "sendMessageWithBody" --kind method

# ② 看它的 callers——會包含「透過 Swift→ObjC bridge」進來的邊
codegraph callers sendMessageWithBody --limit 50
# 注意 provenance 標記:heuristic 邊 = Swift 側橋接進來

# ③ 驗證 heuristic 邊的來源 channel
codegraph explore "sendMessageWithBody 的呼叫者與橋接來源"

# ④ 評估影響半徑(含跨語言 hop)
codegraph impact sendMessageWithBody --depth 2

# ⑤ 對「可疑」的邊用手動確認
codegraph node "src/legacy/MessageSender.m:88"   # 帶行號讀原始檔

為什麼走這條路徑:純 tree-sitter 在 Swift↔ObjC 邊界就斷了——Swift obj.foo(bar:) 與 ObjC selector -fooWithBar: 需要 @objc auto-bridging 規則(含 Cocoa preposition 前綴)才能接起來。改 ObjC method 若不看 Swift 側的 callers,影響評估就是殘缺的。先看帶 provenance 的邊、再手動抽查,是對 heuristic 邊的正確態度。

🔬 深入原理擴充

內部架構:8 種邊界的合成機制。Swift↔ObjC 靠名稱規則(@objc 暴露 + preposition 前綴 With/For/By/In/On/At…)跨界;RN legacy bridge 解析 RCT_EXPORT_METHOD/@ReactMethod 的 macro/annotation;TurboModules 把 Native<X>.ts spec interface 當 ground truth;native→JS events 依字面 event name 合成跨語言通道;Expo Modules 解析 DSL 字面值;Fabric/Paper view components 用慣例名 + suffix(View/ComponentView/Manager/ViewManager)橋到 native。每個合成邊都標 provenance:'heuristic' 與穩定 metadata.synthesizedBy channel——agent 一眼能看出這個 hop 的來源。

大家以為分析正確但其實有誤:以為 heuristic 邊與 AST 邊「同等可信」。實際上它們是推論——Swift foo(bar:) 橋到 -fooWithBar: 靠的是命名規則,命名不合慣例就斷,名稱衝突就可能連錯。可追溯性(provenance + synthesizedBy)不是裝飾,是讓你能審查每個推論。另一個誤解:以為橋接萬能——event name 若是變數("evt" + id)而不是字面值,通道就合成不出來,這是靜態分析的誠實邊界。

🩺 診斷式疑難排解表

症狀可能原因解決方案
Swift→ObjC 連結斷掉沒 @objc 暴露,或 selector 命名不合 preposition 慣例確認 @objc 已加;檢查方法名的 preposition 前綴
RN 事件通道沒進圖event name 是變數(非字面值)這屬解析極限;手動補節點或文件說明
TurboModule 沒連到 native implCodegen spec 不符 ground truth確認 Native<X>.ts spec interface 完整且命名一致
heuristic 邊指向錯 target名稱衝突或非標準寫法codegraph node 手動驗證;用 synthesizedBy 追蹤來源 channel
Fabric component 沒對到 native慣例 suffix 命名不符檢查 View/ComponentView/Manager/ViewManager 命名規則

🧩 進階挑戰題

  1. Swift obj.foo(bar:) 要橋到 ObjC 的 -fooWithBar:。列出 resolver 需要做的名稱轉換步驟,並說明為什麼 Cocoa preposition 前綴(With/For/By…)是規則的必要部分。
  2. 為什麼每個 bridge 產出的邊要標 provenance:'heuristic'?如果 agent 把 heuristic 邊當 AST 邊同等信任,會發生什麼具體問題?
  3. 設計一個新 bridge channel(例如新增 `rn-event-channel` 的合成邊)的驗證流程:要在哪些真實 repo 上驗證、驗證什麼,才算可信出貨?

🏗️ 專案級端到端 Worked Example:大型 RN app 的跨語言影響評估與重構規劃

一個 50+ native module 的 React Native app,要重構 legacy bridge 到 TurboModules,先評估「哪些 JS 呼叫會被影響」:

RN 重構影響評估
# ① 索引整個 RN app
codegraph init

# ② 找出所有 legacy bridge 的 NativeModules 呼叫
codegraph query "NativeModules" --kind import --limit 50
# → 找到 23 個 NativeModules import

# ③ 對每個 module,追蹤 JS→Native 的完整路徑
codegraph callers NativeModules.DeviceInfo.getDeviceId
# JS 側 → bridge → ObjC @ReactMethod

codegraph callers NativeModules.Payment.charge
# JS 側 → bridge → Swift @ReactMethod

# ④ 量化遷移影響(哪些 JS 呼叫會被 TurboModules 改變)
for module in DeviceInfo Payment Analytics PushNotification; do
  codegraph impact "NativeModules.$module" --depth 2 --json
done | jq '.totalAffected' | paste -sd+ | bc
# → 87 個受影響符號

# ⑤ 找出 Swift↔ObjC 的 heuristic 邊,手動驗證
codegraph callers sendMessageWithBody --limit 30
# 注意 provenance:'heuristic' 標記

# ⑥ 產出重構計畫
#    ——哪些 module 可以安全遷移(heuristic 邊少)
#    ——哪些 module 需要額外人工驗證(heuristic 邊多)

關鍵觀察:heuristic 邊不是「錯」,是「推論」——Swift foo(bar:) 橋到 ObjC -fooWithBar: 靠命名規則,命名不合慣例就斷。可追溯性(provenance + synthesizedBy)是讓你能審查每個推論的關鍵。

⚡ 效能 / 品質 / 安全深度

面向考量實務建議
效能跨語言 hop 的查詢深度每多一個語言邊界 hop,blast radius 就多一層;--depth 要依實際需求調整
品質heuristic 邊的可信度每個 bridge 產出的邊都標 provenance:'heuristic' + synthesizedBy channel;用 codegraph node 手動抽查
安全跨語言邊界可能洩漏 native API 結構圖譜在本機;synthesizedBy channel 只顯示連結方式,不含 API key 或敏感參數

📄 文件間比較對照表

面向本文(iOS/RN 橋接)相關文差異說明
8 種邊界Swift↔ObjC、RN bridge、TurboModules、Expo、Fabric語言支援橋接講「跨語言連結」,語言支援講「單語言解析深度」
heuristicprovenance + synthesizedBy 可追溯性運作原理橋接講 heuristic 邊的產生,運作原理講 resolution 階段的合成機制
RN bridgelegacy bridge vs TurboModules vs Expo框架路由橋接講 JS↔Native 連結,框架路由講 URL→handler 連結
Fabric/Paperview component 的慣例名稱橋接架構總覽橋接講 React Native 特定行為,架構頁講 resolution 層整體設計

✅ 進階互動式檢核清單

- [ ] 能解釋為什麼 tree-sitter 在語言邊界斷流(Swift foo(bar:) vs ObjC -fooWithBar:) - [ ] 能區分 Swift↔ObjC 規則導向 vs RN bridge convention 導向的橋接方式 - [ ] 能用 synthesizedBy channel 追蹤每個 heuristic 邊的來源 - [ ] 能評估「legacy bridge → TurboModules」遷移的影響範圍 - [ ] 能解釋 event name 是變數(非字面值)時為什麼通道合成不出來 - [ ] 能用手動 codegraph node 驗證 heuristic 邊的正確性