Files
codegraph/__tests__/swift-label-overload.test.ts
Colby MchenryandClaude Opus 5.5 31c5153d62 fix(swift): an overloaded call reaches the overload its argument labels spell (#2238)
retargetSelfOverload for Swift matches the call's argument labels against
each same-owner overload's external parameter labels (defaulted and variadic
ones optional), instead of the argument count alone.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 03:02:05 +00:00

63 lines
2.2 KiB
TypeScript

/**
* Swift overloads by argument label as much as by count: Alamofire's
* `self.tableView(tableView, numberOfRowsInSection: section)` inside
* `tableView(_:titleForHeaderInSection:)` is the other `tableView`, and
* `cancel(optionallyProducingResumeData:)` is not `cancel(producingResumeData:)`.
* A call reaches the overload whose labels it spells, defaulted ones optional.
*/
import { describe, it, expect, afterAll, beforeAll } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { CodeGraph } from '../src';
let root = '';
let cg: CodeGraph;
beforeAll(async () => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-swift-labels-'));
const files: Record<string, string> = {
'Sources/TableController.swift': `class TableController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if self.tableView(tableView, numberOfRowsInSection: section) == 0 { return nil }
return "x"
}
func cancel(producingResumeData shouldProduce: Bool) -> Self {
return cancel(optionallyProducingResumeData: nil)
}
private func cancel(optionallyProducingResumeData handler: ((Data?) -> Void)?, retry: Bool = false) -> Self {
return self
}
}
`,
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
});
afterAll(() => {
cg?.close();
if (root) fs.rmSync(root, { recursive: true, force: true });
});
const targetLineOfCallAt = (line: number) => {
const ids = cg.getNodesInFile('Sources/TableController.swift').map((n) => n.id);
return cg.getOutgoingEdgesFrom(ids).filter((e) => e.kind === 'calls' && e.line === line).map((e) => cg.getNode(e.target)!.startLine);
};
describe('Swift overloads by label', () => {
it('reach the overload whose labels the call spells', () => {
expect(targetLineOfCallAt(7)).toEqual([2]);
expect(targetLineOfCallAt(12)).toEqual([15]);
});
});