Files
codegraph/__tests__/kernel-grammar-parity.test.ts
T
b2f9ab1800 feat(kernel): R7b R walker — rlang module, tree-sitter-r 1.2.0 crate pin, r default-routed (#1383)
R7b batch 4 #1 (docs/design/r-kernel-port-checklist.md is the authoritative
quirk list; survey + probe record therein). The lightest-shared-surface,
heaviest-hook port: languages/r.ts works entirely through the visitNode hook
(every type list empty except callTypes:['call']), so the walker is a file
node + a faithful hook transcription + the generic extractCall + pre-order
recursion — four shared machineries (value-refs, static-member reads, type
annotations, fn-ref capture) are dead by language gates and stay dead.

Grammar prep is the first true no-op of the arc: the crates.io tree-sitter-r
1.2.0 tarball ships parser.c AND scanner.c sha-identical to the r-lib v1.2.0
tag the vendored wasm was built from — crate pin only, no wasm change, no
bump gate; kernel-grammar-parity gains the r row (ABI 14, same-revision).

Preserved bug-for-bug (all probe-pinned): calls "return" on every return(x)
(named node in v1.2.0), the import quintet's silent dynamic-arg consumption
vs class/generic fall-through asymmetry, library(help = pkg) importing the
named arg, class-idiom variable suppression by callee name, chained/right-
assign/precedence-ghost gaps, env$fn body-leak-to-file, raw-text callees
verbatim (pkg::fn, obj$meth, "strfn" quotes kept, (handler) conversion),
duplicate same-(kind,name,line) ids, roxygen dropped entirely, UTF-16
columns/slices.

Gates: parity sweeps first-run 0-diff on AnomalyDetection/dplyr/ggplot2/
shiny (838 files; deferrals exactly 0/0/0/1 — the 1 is the moustache-
template pseudo-R file, both-arm) — kernel-parity.mjs gained lowercased-
extension matching so .R files sweep (matches detectLanguage routing);
full-init dumps byte-identical kernel-vs-wasm on dplyr/ggplot2/shiny;
kernel-r-parity suite (torture fixture + in-memory CRLF + BOM variants +
defer pin + kernel-arm quirk pins); full suite 2,638 green ×2 with
CODEGRAPH_KERNEL_EXPECT=1. DEFAULT_ROUTED += r (16 langs).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-20 18:28:28 -05:00

71 lines
3.1 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', 'c', 'cpp', 'rust', 'csharp', 'ruby', 'php', 'swift', 'kotlin', 'r'];
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 ?? ''));
});
});