Python (codegraph-kernel/src/python.rs) and Go (src/go.rs) join the native kernel, mirroring the wasm extractors bug-for-bug. Python: decorated_definition docstrings/decorators (decorates refs only for bare-identifier decorators — the call-kind quirk), function-in-class → method, module-level assignments always extract as variable, from-import per-name binding refs, self.x fn-ref candidates as bare names. Go: receiver methods with Recv::name qualified names + contains edges to the first earlier struct of that name, type_spec struct/interface classification with embedding→extends and interface method nodes, composite-literal instantiates keeping the package qualifier, top-level var/const initializer walks attributed to the declared symbol (#693), 2-hop field chains (#1276), New().Method() re-encode (#645/#608), and the GO_SPEC fn-ref layers. Grammars: tree-sitter-python 0.23.6 + tree-sitter-go 0.23.4 crates, with wasm vendored from the same tags (parser.c sha-matched) — both were 2023-era in tree-sitter-wasms. Gates: extraction sweeps 100% (flask 83/83, django 3,035/3,038 +3 error-file deferrals, gin 99/99, prometheus 978/979 +1); full-init dump-diffs byte-identical on flask (10,833 rows), gin (17,540), django (360,794), and prometheus (213,758); torture fixtures enforced in npm test. DEFAULT_ROUTED now covers typescript/tsx/javascript/jsx/java/ python/go. Full suite: 2,471 tests pass. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
71 lines
3.0 KiB
TypeScript
71 lines
3.0 KiB
TypeScript
/**
|
|
* Grammar-source parity gate (R1, migration plan §3.5).
|
|
*
|
|
* The native kernel compiles grammars from crates.io / vendored sources; the
|
|
* wasm fallback loads grammars from tree-sitter-wasms / src/extraction/wasm.
|
|
* If the two are built from different grammar revisions, a language's graph
|
|
* would depend on WHICH path extracted it — per-language routing (and the
|
|
* kernel-absent fallback) must be graph-neutral.
|
|
*
|
|
* Rather than trusting version metadata, this asserts the grammars are
|
|
* behaviorally identical where extraction can observe them: ABI version and
|
|
* the full node-kind and field tables, compared id by id.
|
|
*
|
|
* Runs wherever a kernel binary is staged (scripts/build-kernel.sh); skips
|
|
* otherwise. CI that builds the kernel sets CODEGRAPH_KERNEL_EXPECT=1 so the
|
|
* skip can't mask a missing build (asserted in kernel-scaffold.test.ts).
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll } from 'vitest';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import type { Language as WasmLanguage } from 'web-tree-sitter';
|
|
import { getKernel, resetKernelForTests } from '../src/extraction/kernel';
|
|
import { initGrammars, loadGrammarsForLanguages, getParser } from '../src/extraction/grammars';
|
|
import type { Language } 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);
|
|
|
|
// Every kernel-capable language. `jsx` shares the javascript grammar on BOTH
|
|
// paths (langs.rs mirrors WASM_GRAMMAR_FILES), so the distinct grammars are:
|
|
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go'];
|
|
|
|
describe.skipIf(!kernelBuilt)('kernel↔wasm grammar parity', () => {
|
|
beforeAll(async () => {
|
|
resetKernelForTests();
|
|
await initGrammars();
|
|
await loadGrammarsForLanguages(GRAMMAR_LANGUAGES);
|
|
});
|
|
|
|
it.each(GRAMMAR_LANGUAGES)('%s: node-kind and field tables are identical', (language) => {
|
|
const kernel = getKernel();
|
|
expect(kernel).not.toBeNull();
|
|
const native = kernel!.grammarInfo(language);
|
|
expect(native, `kernel has no grammar for ${language}`).not.toBeNull();
|
|
|
|
const wasmLang = getParser(language)?.language as WasmLanguage | null | undefined;
|
|
expect(wasmLang, `wasm grammar for ${language} not loaded`).toBeTruthy();
|
|
|
|
expect(native!.abiVersion, 'grammar ABI version').toBe(wasmLang!.abiVersion);
|
|
expect(native!.nodeKindCount, 'node-kind count').toBe(wasmLang!.nodeTypeCount);
|
|
expect(native!.fieldCount, 'field count').toBe(wasmLang!.fieldCount);
|
|
|
|
const wasmKinds: (string | null)[] = [];
|
|
for (let i = 0; i < wasmLang!.nodeTypeCount; i++) wasmKinds.push(wasmLang!.nodeTypeForId(i));
|
|
expect(native!.nodeKinds).toEqual(wasmKinds.map((k) => k ?? ''));
|
|
|
|
// Field ids are 1-based on both sides.
|
|
const wasmFields: (string | null)[] = [];
|
|
for (let i = 1; i <= wasmLang!.fieldCount; i++) wasmFields.push(wasmLang!.fieldNameForId(i));
|
|
expect(native!.fieldNames).toEqual(wasmFields.map((f) => f ?? ''));
|
|
});
|
|
});
|