Fifth R7b batch-3 port, checklist-first recipe (docs/design/swift-kernel-port-checklist.md, 1,056 lines — the largest of the arc, with a built-extractor-validated emission pin and a childForFieldName truth table). Grammar bump first, validated standalone: tree-sitter-wasms ^0.4.0 (ABI 13) → crate 0.7.3 — with a provenance twist: the wasm is built from the CRATE TARBALL's src/ (alex-pinkus keeps generated files off main and the 0.7.3-with-generated-files tag ships an older ABI-14 generation that can never sha-match; grammar.json rules are JSON-equal; the tarball is byte-for-byte what the kernel's cargo build compiles — table identity by construction). Older crates evaluated and rejected: clean-parse shapes are byte-identical on 0.7.3 (53-line CST battery diff, all inert), so an older pin buys nothing and loses the macro-era wins. Delta = error-set membership (63 old-error files parse clean: swift-testing #expect, #Preview/#GET macros, package access, typed throws — vapor 23.1%→9.3%; 21 NEW-only regressions in 3 probed construct classes) + two gate-found categories: docstring boundaries near #if directives (7 clean files, docstring-field-only — verified mechanically) and array-literal-callee call refs (2 refs, 1 file). Every hunk classified via the error-union rule + parked-ref↔edge ripple pairing. Walker (the arc's biggest) centers on the #1020 DEDICATED property branch: computed properties → property nodes with the getter walked under the property (SwiftUI body), static let/var → constant/variable, stored → field, decorator/type-annotation/@Siblings-attr-arg refs all attached to the ENCLOSING TYPE, stored initializer calls attributed to the class. Preserved bug-for-bug: the never-resolving 'parameter' field (zero param type refs, zero signatures), present-false isAsync, open→internal visibility, everything-is-extends inheritance (first type_identifier per specifier), no instantiates refs ever, subscript reads as `calls arr`, `defer` as `calls defer`, multi-case enum entries minting only the first case, /** */ block docs ignored AND chain-breaking, init/deinit/subscript minting no nodes with visitNode-routed bodies (calls → class, static reads → nothing), multi- segment extension resolveName, sugar extension names, the #selector shapes, and the value_argument label-forward skip. ONE fix found by the sweep (then pinned in the fixture + checklist): the shared `assignment` shadow-prune case is swift-live — declared-then-assigned `let X: T` prunes X as a value-ref target. Gates: sweeps 0-diff Alamofire 89/98, vapor 224/247, swift-nio 407/554 (--max-deferral 0.3 — swift error incidence is 9–27% on BOTH arms, structural; every deferral count matches the survey's table exactly); full-init dumps byte-identical ×3 (31.9k/20.7k/126.3k lines); the Alamofire census reproduces property=348 (the #1020 number) on the kernel arm; kernel-swift-parity suite (206-line torture + CRLF + the #if-between-enum- cases defer fixture) + swift grammar-parity row; full suite 2,626 green ×2 under CODEGRAPH_KERNEL_EXPECT=1. DEFAULT_ROUTED += swift (14 langs). Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
128 lines
5.5 KiB
TypeScript
128 lines
5.5 KiB
TypeScript
/**
|
||
* Kernel↔wasm Swift extraction parity (R7b of the kernel migration).
|
||
*
|
||
* Asserts the native walker (codegraph-kernel/src/swift.rs) produces the SAME
|
||
* ExtractionResult as the wasm TreeSitterExtractor — nodes, edges, and
|
||
* unresolved refs compared as canonicalized multisets — over the checked-in
|
||
* torture fixture (torture.swift: the DEDICATED in-class property branch
|
||
* (#1020 — computed→property with getter walk, static let/var→constant/
|
||
* variable, stored→field, owner-attributed decorator/type/attr-arg refs,
|
||
* observed-property field + class-attributed observer calls), extensions
|
||
* (multi-segment resolveName, sugar `[Proto]` names, where-clauses),
|
||
* everything-is-extends inheritance, the full call matrix (subscript reads,
|
||
* `defer`, optional-chaining receivers, #750 re-encode, literal-set
|
||
* membership quirks, implicit members), positional return types with the
|
||
* nested-generic failure, present-false isAsync, `open`→internal visibility,
|
||
* multi-case enum first-only minting, `/** */` docs ignored-and-chain-
|
||
* breaking, value-ref targets incl. the declared-then-assigned
|
||
* assignment-prune case the swift-nio sweep caught, SWIFT_SPEC fn-refs with
|
||
* the label-forward skip and #selector shapes) and its CRLF variant (derived
|
||
* in-memory — #1329).
|
||
*
|
||
* The full-repo sweep lives in scripts/kernel-parity.mjs (Alamofire/vapor/
|
||
* swift-nio, --max-deferral 0.3 — swift error incidence is structurally
|
||
* 9–27% on BOTH arms); this suite keeps the invariant alive in `npm test`.
|
||
* Skips when no kernel binary is staged; CODEGRAPH_KERNEL_EXPECT=1 turns
|
||
* that into a failure (kernel-scaffold.test.ts).
|
||
*/
|
||
|
||
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
|
||
import * as fs from 'fs';
|
||
import * as path from 'path';
|
||
import { extractFromSource } from '../src/extraction';
|
||
import { initGrammars, loadGrammarsForLanguages } from '../src/extraction/grammars';
|
||
import { tryKernelExtract, resetKernelForTests } from '../src/extraction/kernel';
|
||
import type { ExtractionResult } from '../src/types';
|
||
|
||
const KERNEL_PATH = path.join(
|
||
__dirname,
|
||
'..',
|
||
'codegraph-kernel',
|
||
'prebuilds',
|
||
`${process.platform}-${process.arch}`,
|
||
'codegraph-kernel.node'
|
||
);
|
||
const kernelBuilt = fs.existsSync(KERNEL_PATH);
|
||
|
||
const FIXTURE_DIR = path.join(__dirname, 'fixtures', 'kernel-parity');
|
||
|
||
function canon(result: ExtractionResult): { nodes: string[]; edges: string[]; refs: string[] } {
|
||
return {
|
||
nodes: result.nodes
|
||
.map(({ updatedAt: _u, ...n }) => JSON.stringify(n, Object.keys(n).sort()))
|
||
.sort(),
|
||
edges: result.edges.map((e) => JSON.stringify(e, Object.keys(e).sort())).sort(),
|
||
refs: result.unresolvedReferences
|
||
.map((r) => JSON.stringify(r, Object.keys(r).sort()))
|
||
.sort(),
|
||
};
|
||
}
|
||
|
||
const ENV_KEYS = ['CODEGRAPH_KERNEL', 'CODEGRAPH_KERNEL_LANGS'] as const;
|
||
let savedEnv: Record<string, string | undefined>;
|
||
|
||
describe.skipIf(!kernelBuilt)('kernel Swift extraction parity', () => {
|
||
beforeAll(async () => {
|
||
await initGrammars();
|
||
await loadGrammarsForLanguages(['swift']);
|
||
});
|
||
|
||
beforeEach(() => {
|
||
savedEnv = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
|
||
resetKernelForTests();
|
||
});
|
||
|
||
afterEach(() => {
|
||
for (const k of ENV_KEYS) {
|
||
if (savedEnv[k] === undefined) delete process.env[k];
|
||
else process.env[k] = savedEnv[k];
|
||
}
|
||
resetKernelForTests();
|
||
});
|
||
|
||
function assertParity(filePath: string, source: string, minNodes = 3): void {
|
||
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
||
delete process.env.CODEGRAPH_KERNEL;
|
||
const viaKernel = tryKernelExtract(filePath, source, 'swift');
|
||
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
|
||
|
||
process.env.CODEGRAPH_KERNEL = '0';
|
||
const viaWasm = extractFromSource(filePath, source, 'swift');
|
||
delete process.env.CODEGRAPH_KERNEL;
|
||
|
||
const k = canon(viaKernel!);
|
||
const w = canon(viaWasm);
|
||
expect(k.nodes, `${filePath}: nodes`).toEqual(w.nodes);
|
||
expect(k.edges, `${filePath}: edges`).toEqual(w.edges);
|
||
expect(k.refs, `${filePath}: refs`).toEqual(w.refs);
|
||
expect(viaWasm.nodes.length).toBeGreaterThanOrEqual(minNodes);
|
||
}
|
||
|
||
it('torture fixture: property branch, extensions, call matrix, value refs, fn-refs', () => {
|
||
const file = path.join(FIXTURE_DIR, 'torture.swift');
|
||
assertParity('fixtures/torture.swift', fs.readFileSync(file, 'utf8'), 40);
|
||
});
|
||
|
||
// CRLF variant — the shape every Windows autocrlf checkout has. Derived in
|
||
// memory so no platform or editor can silently normalize it away; pins the
|
||
// JS-multiline-^ docstring semantics for `///` runs (#1329).
|
||
it('torture fixture CRLF parity', () => {
|
||
const file = path.join(FIXTURE_DIR, 'torture.swift');
|
||
const crlf = fs.readFileSync(file, 'utf8').replace(/(?<!\r)\n/g, '\r\n');
|
||
assertParity('fixtures/torture.swift (crlf)', crlf, 40);
|
||
});
|
||
|
||
it('files with parse errors defer to the wasm extractor (recovery is encoding-dependent)', () => {
|
||
// A NEW-only regression construct (`#if` between enum cases — the swift
|
||
// checklist's grammar-bump delta 5) — errors on the 0.7.3 grammar.
|
||
const broken = 'enum E {\n case a\n#if DEBUG\n case b\n#endif\n}\n';
|
||
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
||
delete process.env.CODEGRAPH_KERNEL;
|
||
expect(tryKernelExtract('src/Broken.swift', broken, 'swift')).toBeNull();
|
||
process.env.CODEGRAPH_KERNEL = '0';
|
||
const viaWasm = extractFromSource('src/Broken.swift', broken, 'swift');
|
||
delete process.env.CODEGRAPH_KERNEL;
|
||
expect(viaWasm.nodes.some((n) => n.kind === 'file')).toBe(true);
|
||
});
|
||
});
|