mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-10-17 05:07:37 +08:00
355afc70b1
* fix(csharp): walk field and property initializers as the member Field declarators and property `= initializer` values were skipped by both extractors, so `private readonly ILogger _log = LogManager.GetLogger(typeof(X));` linked nothing, and a method or class used only from an initializer looked unused. - tree-sitter.ts / csharp.rs: walk each C# field declarator whole with its field on the stack, and every property `value:` (not only `=> expr`) with the property on the stack. Calls, instantiations, static reads, lambda bodies and fn-ref candidates (varinit included) belong to the member; the fn-ref scan skips the walked subtrees, so each candidate is captured once. Attributes stay unwalked. - A target-typed `new()` that is the initializer instantiates the declared type (`List<Foo> _items = new();` -> List). In bodies it stays invisible, as before. - name-matcher.ts: a .NET type position never names a `constant` (the kind `const` / `static readonly` fields get). Walking initializers exposed `new Version(5, 18)` binding to a `const string Version` and `static readonly Meter Meter = new(...)` instantiating itself. Kernel/wasm parity: 0 diffs on serilog, Newtonsoft.Json and jellyfin, and full-index dumps byte-identical between the two arms. No EXTRACTION_VERSION bump: #2345 already moved it to 28 this release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(changelog): credit the report and contributor behind the constant change The type-position change here is what issue #2337 reports (`new Station { … }` unresolved beside a `private const string Station`) and the same one-line change as @drakeo338's #2339. Verified on the issue's own three-file repro: the `instantiates` edge to `Demo.Core::Station`, missing on main, is present with this branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
233 lines
9.3 KiB
TypeScript
233 lines
9.3 KiB
TypeScript
/**
|
|
* Kernel↔wasm C# extraction parity (R7b of the kernel migration).
|
|
*
|
|
* Asserts the native walker (codegraph-kernel/src/csharp.rs) produces the
|
|
* SAME ExtractionResult as the wasm TreeSitterExtractor — nodes, edges, and
|
|
* unresolved refs compared as canonicalized multisets — over the checked-in
|
|
* torture fixtures:
|
|
*
|
|
* - Torture.cs — block namespace + nested/second-namespace quirks,
|
|
* base_list shapes, records, properties (incl. the bare-identifier
|
|
* signature loss; accessor bodies and initializers walked as the
|
|
* property), fields/constants (initializers walked as the field),
|
|
* events/operators/indexer/destructor (no nodes, calls → class), ctor
|
|
* initializer hole, explicit interface impl, local functions, the call
|
|
* zoo (raw member-access texts, chained re-encode, `(myDel)(x)` conv,
|
|
* `nameof`), instantiation shapes (incl. invisible body `new()`/`new {}`/
|
|
* arrays), static value reads, C# type refs, fn-ref candidates
|
|
* (`+=` subscription, `this.X` bare-name form, initializer lists),
|
|
* value-ref targets + local shadow prune, preprocessor passthrough.
|
|
* - TortureFileScoped.cs — file-scoped namespace, alias-import quirks,
|
|
* positional records with base args (`BaseDto(Name)` full-text extends),
|
|
* C#12 primary-ctor base args (`(repo)` garbage extends preserved).
|
|
* - TortureTopLevel.cs — top-level statements (zero-emission locals),
|
|
* top-level local function, trailing partial class.
|
|
*
|
|
* CRLF variants are derived in-memory (#1329 docstring semantics). The
|
|
* full-repo sweep lives in scripts/kernel-parity.mjs (serilog /
|
|
* Newtonsoft.Json / jellyfin for the §5 gate); 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 C# extraction parity', () => {
|
|
beforeAll(async () => {
|
|
await initGrammars();
|
|
await loadGrammarsForLanguages(['csharp']);
|
|
});
|
|
|
|
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, 'csharp');
|
|
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
|
|
|
|
process.env.CODEGRAPH_KERNEL = '0';
|
|
const viaWasm = extractFromSource(filePath, source, 'csharp');
|
|
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.cs', minNodes: 40 },
|
|
{ file: 'TortureFileScoped.cs', minNodes: 8 },
|
|
{ file: 'TortureTopLevel.cs', minNodes: 2 },
|
|
];
|
|
|
|
for (const { file, minNodes } of FIXTURES) {
|
|
it(`${file}: namespaces, records, calls, holes, refs`, () => {
|
|
const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
|
|
assertParity(`fixtures/${file}`, src, minNodes);
|
|
});
|
|
|
|
// 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(`${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);
|
|
});
|
|
}
|
|
|
|
// Cheap unit pins for the quirks a future grammar bump would silently move
|
|
// (checklist §fixtures item 6) — parity is the assertion; the wasm arm is
|
|
// the behavior oracle.
|
|
const MICROS: Array<{ name: string; source: string; minNodes: number }> = [
|
|
{
|
|
name: 'alias-import to a qualified target keeps generic args in moduleName',
|
|
source: 'using Coll = System.Collections.Generic.Dictionary<string, int>;\n',
|
|
minNodes: 2,
|
|
},
|
|
{
|
|
name: 'alias-import to a bare identifier captures the ALIAS name',
|
|
source: 'using Short = SomeType;\n',
|
|
minNodes: 2,
|
|
},
|
|
{
|
|
name: 'C#12 primary-ctor base args emit the garbage `(repo)` extends ref',
|
|
source: 'public class Svc(IRepo repo) : Base(repo), IThing { }\n',
|
|
minNodes: 2,
|
|
},
|
|
{
|
|
name: 'enum underlying type emits an extends ref named `byte`',
|
|
source: 'public enum E : byte { A = 1, B }\n',
|
|
minNodes: 4,
|
|
},
|
|
{
|
|
name: 'nameof(...) emits a calls ref named `nameof`',
|
|
source: 'public class C { void M() { var n = nameof(C); } }\n',
|
|
minNodes: 3,
|
|
},
|
|
{
|
|
name: 'this./base. callee prefixes are kept raw',
|
|
source: 'public class C { void M() { this.Run(1); base.Go(); } }\n',
|
|
minNodes: 3,
|
|
},
|
|
{
|
|
name: 'bare-identifier-typed property loses its type in the signature',
|
|
source: 'public class C { public Widget Parent { get; set; } }\n',
|
|
minNodes: 3,
|
|
},
|
|
{
|
|
name: 'bodiless struct mints no node; bodiless record still does',
|
|
source: 'public record Empty;\n',
|
|
minNodes: 2,
|
|
},
|
|
{
|
|
// Accessor bodies, `=> expr` and `= initializer` are walked as the
|
|
// property (calls, instantiations, static reads, fn-ref candidates).
|
|
name: 'property bodies and initializers are walked as the property',
|
|
source: [
|
|
'public class C {',
|
|
' void H(int v) { }',
|
|
' public Action P { get { return Make(H); } set { Register(H); } }',
|
|
' public int Q => Run(H);',
|
|
' public Action R { get; } = Wrap(H);',
|
|
' public int S { get => Calc.Max; init => Store(new Widget()); }',
|
|
'}',
|
|
'',
|
|
].join('\n'),
|
|
minNodes: 7,
|
|
},
|
|
{
|
|
// Each field declarator and each property `= initializer` is walked
|
|
// with its member on the stack — lambdas included — and its fn-ref
|
|
// candidates (varinit too) are the member's, captured once. A
|
|
// target-typed `new()` there instantiates the declared type; attribute
|
|
// arguments stay with the class's candidates-only scan.
|
|
name: 'field and property initializers are walked as the member',
|
|
source: [
|
|
'public class C {',
|
|
' static void H() { }',
|
|
' private readonly ILogger _log = LogManager.GetLogger(typeof(C));',
|
|
' int a = 1, b = Calc.Max(2);',
|
|
' private static readonly List<Action> Table = new() { H };',
|
|
' private Widget? _w = new(1) { Size = Make() };',
|
|
' private global::Ns.Box<int> _g = new(), _h = (new());',
|
|
' private (int, int) _t = new();',
|
|
' Action g = () => Register(H);',
|
|
' Del d = H;',
|
|
' public Widget P { get; } = new();',
|
|
' public Action R { get; } = Wrap(H);',
|
|
' public string S { get; } = Defaults.Name;',
|
|
' [Attr(Make(H))] public int T { get; set; } = 3;',
|
|
'}',
|
|
'',
|
|
].join('\n'),
|
|
minNodes: 17,
|
|
},
|
|
];
|
|
|
|
for (const m of MICROS) {
|
|
it(`micro: ${m.name}`, () => {
|
|
assertParity(`micro/${m.name.replace(/[^a-z0-9]+/gi, '-')}.cs`, m.source, m.minNodes);
|
|
});
|
|
}
|
|
|
|
it('files with parse errors defer to the wasm extractor (recovery is encoding-dependent)', () => {
|
|
const broken = 'class F { void M( { return }} 12 (\n';
|
|
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
expect(tryKernelExtract('src/Broken.cs', broken, 'csharp')).toBeNull();
|
|
process.env.CODEGRAPH_KERNEL = '0';
|
|
const viaWasm = extractFromSource('src/Broken.cs', broken, 'csharp');
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
expect(viaWasm.nodes.some((n) => n.kind === 'file')).toBe(true);
|
|
});
|
|
});
|