mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-09-19 07:34:57 +08:00
958288c492
Closes the cross-language flow gap in mixed iOS codebases. Swift @objc exports + Cocoa-style ObjC selectors now resolve across the language boundary, so trace / callers / callees work for flows like an Objective-C demo VC calling into a Swift framework. Architecture (resolver pattern, per dynamic-dispatch playbook §3a): - src/resolution/swift-objc-bridge.ts — pure name math implementing Apple's auto-bridging rules. `objcSelectorForSwiftMethod` produces the bridged selector for a Swift method declaration (`play(song:)` → `playWithSong:`; `tableView(_:didSelectRowAtIndexPath:)` → `tableView:didSelectRowAtIndexPath:`). `objcSelectorForSwiftInit` handles initializers (`initWith<First>:`). `objcAccessorsForSwiftProperty` produces getter+setter for `@objc` vars (`name` → `name` / `setName:`). `swiftBaseNamesForObjcSelector` does the reverse: given an ObjC selector, returns Swift base name candidates the resolver should look for (`objectForKey:` → `object`; `playWithSong:by:` → `playWithSong` or `play`). Preposition list (`With|For|By|In|On|At|From|To|Of|As`) covers both Swift's @objc export rule and Cocoa's natively-imported selectors. 31 unit tests. - src/resolution/frameworks/swift-objc.ts — framework resolver registered alongside the existing Swift resolvers. Builds a one-time per-context reverse-bridge map (`Swift base name → ObjC method nodes`) on first resolve(); claims selector-shape refs through the pre-filter; routes each ref to the correct direction by source language. 12 integration tests with a mock ResolutionContext. - src/index.ts — re-initialize the resolver after `indexAll()` completes. The resolver's `initialize()` runs at construction (before any files are indexed), so framework resolvers whose `detect()` consults the indexed file list (UIKit/SwiftUI scanning for imports, swift-objc-bridge looking for both Swift and ObjC files) all returned false on that initial pass and silently dropped themselves. Now they see the actual project before resolution runs — fixes a pre-existing, broader issue, not just the bridge. Precision controls: - Generic Cocoa names (`init`, `description`, `hash`, `isEqual`, `copy`, `count`, `add`, `value`, `load`, etc.) are blocklisted from the reverse-bridge map. Bridging them produces noise (every NSObject subclass has `init`), and the regular name-matcher handles them on its own. - Bridge confidence is 0.6 so the regular name-matcher's 1.0 exact match wins on tie — bridge only fires when name-match returns nothing. - ObjC→Swift direction requires the Swift candidate to be `@objc`-exposed (source-window check for `@objc` / `@nonobjc` annotations) — filters out same-named Swift methods that aren't bridged. Charts (small iOS — 205 Swift + 59 ObjC files) validation: - 28 bridge-resolved objc→swift edges; representative samples: - `handleOption:forChartView:` (ObjC demo) → `animate` (Swift) - `handleOption:forChartView:` (ObjC demo) → `notifyDataSetChanged` (Swift) - `setupPieChartView:` (ObjC demo) → `setExtraOffsets` (Swift) - `setDataCount:range:` (ObjC demo) → `setColor` (Swift) - Pure-language baselines unchanged: Swift→Swift=2154, ObjC→ObjC=207. - trace(handleOption:forChartView:, animate) connects across the bridge — the canonical demo→library flow. 902/904 existing tests still pass (2 skipped); +47 new bridge tests. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
190 lines
6.2 KiB
TypeScript
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);
|
|
});
|
|
});
|