mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-11-15 04:21:31 +08:00
Kotlin indexes a class property without a signature, and a primary-constructor property (class A(private val repo: Repo)) is no node at all, so a call through either (probe.close() on a lateinit var probe: HttpProbe) found no receiver type and fell to the name-only strategies: the interface's method, or any same-named method elsewhere. A call through a chain (engine.pump.drain()) was emitted as its bare method name, and a local bound to a qualified call (val lease = HardwareLock.tryBegin()) was untyped. tree-sitter-kotlin also predates fun interface: such a declaration parsed as a broken function and, with a doc comment after it, its error recovery swallowed the next declaration too. The declared type is now read from the declaration itself: a property's own lines, the class header for a constructor property, a callee's declared return type for a local bound to a call, an enum entry's enum, each segment of a receiver chain in turn (the chain is kept in the reference name, by the wasm extractor and the native kernel alike), a superclass's property, and an outer function's local inside an anonymous object. A receiver whose type the project does not declare (Regex, Properties, a JDK class) gets no edge instead of a name-only guess, unless the project extends that type. The extractor's preParse blanks the fun of a fun interface, offset-preserving, so the file parses cleanly in both paths. Validation: - kotlin-property-receiver.test.ts: 17 tests, 12 red on main, all green here; kernel-kotlin-parity.test.ts pins the fun interface fixture. - Kernel parity (kotlin), 0 diffs: okhttp 588/619 files byte-identical (main's kernel 578, the rest deferred to wasm on parse errors), mihon 891/940 (887), nowinandroid 340/350 (339): the fun interface files no longer defer. - A/B against main with #1942 (positions only: the extractor now keeps a chain in the reference name): okhttp +299/-257, 2,554 retargeted, 2,158 of them test calls moving from the deprecated okhttp3.mockwebserver.MockWebServer (a 0.65 name guess) to the mockwebserver3.MockWebServer each file imports; mihon +82/-118, 337 retargeted (Preference get/changes/set/register onto the declared Preference); nowinandroid -9, 23 retargeted (properties onto their concrete test doubles); SystemUI's Kotlin +1/-104, 7 retargeted; jextract, click and gin identical. The removed are name guesses on library types (okio sinks, collections' filter and clear, OkHttp bodies, MutableStateFlow.update, Kotlin's apply). 7 right edges are lost where a chain passes through a library wrapper (Lazy.value, StateFlow.value, Pair.first, a ViewBinding's view). - Cost (mihon, median of 3 interleaved indexes): CPU 25.8 s on main with #1942 vs 26.7 s, wall 8.3 vs 8.4 s. Original PR #1933 by @danusha2345. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
6.4 KiB
TypeScript
146 lines
6.4 KiB
TypeScript
/**
|
|
* Kernel↔wasm Kotlin extraction parity (R7b of the kernel migration).
|
|
*
|
|
* Asserts the native walker (codegraph-kernel/src/kotlin.rs — grammar
|
|
* compiled from the vendored fwcd 0.3.8 C sources, the arc's first
|
|
* vendored-grammar-C language) produces the SAME ExtractionResult as the
|
|
* wasm TreeSitterExtractor over the checked-in torture fixture (torture.kt:
|
|
* the property hook's scope classification and its initializer walk (a
|
|
* lambda / SAM / anonymous-object RHS attributing its calls to the property),
|
|
* extension-function receiver QNs
|
|
* (`WidgetK::extend`, the qualified `com::qext` bug) + the owner-contains
|
|
* fallback, expect/actual → node DECORATORS (the KMP synthesizer feed),
|
|
* the bodiless-vs-bodied class header asymmetry, comment-glued
|
|
* import/package extents, KDoc dropped-and-chain-breaking docstrings,
|
|
* `@Marker` decorates vs `@Anno(args)` nothing, zero type-annotation refs,
|
|
* zero instantiates, the #750 capitalized-chain re-encode, paren-then-
|
|
* lambda garbage callees, `${X}`-reads-vs-`$X`-non-reads value refs and the
|
|
* packaged-file target drop) plus a `.kts` script fixture (file-attributed
|
|
* top-level calls), with in-memory CRLF variants (#1329), and two defer
|
|
* fixtures — a `fun interface` file and a PHANTOM error (a one-line class
|
|
* body sets hasError with a complete, ERROR-node-free CST; the kernel
|
|
* trusts the flag).
|
|
*
|
|
* The full-repo sweep lives in scripts/kernel-parity.mjs (okio/okhttp/
|
|
* kotlinx.coroutines — expected deferrals 23/49/51, grammar-inherent).
|
|
* 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 Kotlin extraction parity', () => {
|
|
beforeAll(async () => {
|
|
await initGrammars();
|
|
await loadGrammarsForLanguages(['kotlin']);
|
|
});
|
|
|
|
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, 'kotlin');
|
|
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
|
|
|
|
process.env.CODEGRAPH_KERNEL = '0';
|
|
const viaWasm = extractFromSource(filePath, source, 'kotlin');
|
|
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);
|
|
}
|
|
|
|
const FIXTURES: Array<{ file: string; minNodes: number }> = [
|
|
{ file: 'torture.kt', minNodes: 40 },
|
|
{ file: 'TortureScript.kts', minNodes: 2 },
|
|
];
|
|
|
|
for (const { file, minNodes } of FIXTURES) {
|
|
it(`${file}: hook properties, receivers, decorators, calls, value refs`, () => {
|
|
const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
|
|
assertParity(`fixtures/${file}`, src, minNodes);
|
|
});
|
|
|
|
it(`${file} CRLF parity`, () => {
|
|
const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
|
|
const crlf = src.replace(/(?<!\r)\n/g, '\r\n');
|
|
assertParity(`fixtures/${file} (crlf)`, crlf, minNodes);
|
|
});
|
|
}
|
|
|
|
it('fun-interface files parse on both paths once `fun` is blanked (preParse)', () => {
|
|
// tree-sitter-kotlin has no `fun interface`; the extractor's preParse
|
|
// blanks the `fun` (offset-preserving), so the kernel no longer defers
|
|
// and the declaration after it — here behind a doc comment, the shape
|
|
// whose error recovery used to swallow it — is extracted too.
|
|
const src = 'package p\n\nfun interface Transformer {\n fun transform(x: Int): Int\n}\n\n/** Next. */\ninterface Sink {\n fun put(x: Int)\n}\n\nfun after() { work() }\n';
|
|
assertParity('src/FunIface.kt', src, 4);
|
|
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
const result = tryKernelExtract('src/FunIface.kt', src, 'kotlin')!;
|
|
expect(result.nodes.some((n) => n.kind === 'interface' && n.name === 'Transformer')).toBe(true);
|
|
expect(result.nodes.some((n) => n.kind === 'interface' && n.name === 'Sink')).toBe(true);
|
|
expect(result.nodes.some((n) => n.kind === 'method' && n.qualifiedName.endsWith('Sink::put'))).toBe(true);
|
|
});
|
|
|
|
it('PHANTOM errors defer too — hasError with a complete, ERROR-node-free CST', () => {
|
|
const src = 'abstract class A { abstract fun i(): Int }\n';
|
|
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
expect(tryKernelExtract('src/Phantom.kt', src, 'kotlin')).toBeNull();
|
|
process.env.CODEGRAPH_KERNEL = '0';
|
|
const viaWasm = extractFromSource('src/Phantom.kt', src, 'kotlin');
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
expect(viaWasm.nodes.some((n) => n.kind === 'class' && n.name === 'A')).toBe(true);
|
|
});
|
|
});
|