mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-09-09 22:33:39 +00:00
Implements the scope-tree spine and position-indexed lookup as pure logic in `gitnexus-shared`. Generalizes the `enclosingFunctions` pattern from closed PR #902 to arbitrary `ScopeKind`s. Three modules under `gitnexus-shared/src/scope-resolution/`: 1. `scope-id.ts` — `makeScopeId({filePath, range, kind})` builds the canonical RFC §2.2 shape `scope:{filePath}#{startLine}:{startCol}-{endLine}:{endCol}:{kind}` and interns the result through a process-local pool so repeated calls with structurally identical inputs return the same string reference. `clearScopeIdInternPool()` exported for test isolation. 2. `scope-tree.ts` — `buildScopeTree(scopes)` validates invariants and returns an immutable `ScopeTree`: - `getScope(id)` / `getParent(id)` / `getChildren(id)` / `getAncestors(id)` - Implements the `ScopeLookup` contract from #916, so `resolveTypeRef` can consume a `ScopeTree` directly (test included). Invariants enforced (throw `ScopeTreeInvariantError` on violation): - Non-Module scopes must have a parent. - Parent must exist in the supplied set. - Parent range STRICTLY contains child range (equal ranges rejected). - Sibling ranges under the same parent do not overlap. Ranges that merely touch at the boundary (`a.end == b.start`) are accepted. - Parent and child live in the same filePath. - Duplicate scope ids are rejected. 3. `position-index.ts` — `buildPositionIndex(scopes)` produces a `PositionIndex` with `atPosition(filePath, line, col)`. Per-file sorted array; binary-search the upper bound of `start ≤ query`, scan backward through the prefix, return the first containing hit. Complexity: `O(log N_file + D)` typical (D = lexical depth ≤ ~10); degrades to `O(N_file)` only under pathological inputs (many scopes starting at the same position). "Innermost wins" falls out of the sort + backward-scan contract because `ScopeTree`'s invariants guarantee that scopes containing a point form an ancestor chain. Types: - `ScopeTree` now exported from `scope-tree.ts`. The Ring 1 opaque placeholder in `types.ts` has been removed; LanguageProvider hooks that previously took `ScopeTree = unknown` now receive the concrete interface (CLI `tsc --noEmit` passes — no existing callers rely on the opaque shape). Tests (39, all passing): - scope-id: canonical shape · all six ScopeKinds encoded · identity equality (same inputs → same reference) · distinguished by filePath / range / kind · purity under repeated calls · intern-pool clear preserves canonical shape. - scope-tree: empty tree · single module · nested Module→Class→Function · multiple siblings input-order preserved · ScopeLookup integration with resolveTypeRef · frozen children and ancestor arrays · all six invariant violations (non-Module orphan, parent-not-found, parent doesn't contain, parent == child, siblings overlap, cross-file parent, duplicate id) · boundary-touching siblings accepted. - position-index: empty · unindexed filePath · before/after-file queries · start/end inclusivity · innermost-wins for nested / co- starting / co-ending / same-line scopes · sibling dispatch · multi- file isolation · size · id-dedup. Combined scope-resolution / model / shadow suite: 190/190 pass. `tsc --noEmit` clean in both `gitnexus-shared` and `gitnexus`. Closes part of #909. Unblocks #917 (`Registry.lookup` needs the scope spine); makes `ScopeLookup` in #916 concrete without API churn.
81 lines
3 KiB
TypeScript
81 lines
3 KiB
TypeScript
/**
|
|
* Unit tests for `makeScopeId` (RFC #909 Ring 2 SHARED #912).
|
|
*
|
|
* Covers canonical shape, determinism across calls, string interning,
|
|
* and that different inputs produce different ids.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { makeScopeId, clearScopeIdInternPool, type Range, type ScopeKind } from 'gitnexus-shared';
|
|
|
|
const r = (startLine: number, startCol: number, endLine: number, endCol: number): Range => ({
|
|
startLine,
|
|
startCol,
|
|
endLine,
|
|
endCol,
|
|
});
|
|
|
|
describe('makeScopeId', () => {
|
|
beforeEach(() => {
|
|
clearScopeIdInternPool();
|
|
});
|
|
|
|
it('produces the canonical RFC §2.2 shape', () => {
|
|
const id = makeScopeId({ filePath: 'src/app.ts', range: r(1, 0, 100, 0), kind: 'Module' });
|
|
expect(id).toBe('scope:src/app.ts#1:0-100:0:Module');
|
|
});
|
|
|
|
it('encodes each ScopeKind verbatim in the id', () => {
|
|
const kinds: readonly ScopeKind[] = [
|
|
'Module',
|
|
'Namespace',
|
|
'Class',
|
|
'Function',
|
|
'Block',
|
|
'Expression',
|
|
];
|
|
for (const kind of kinds) {
|
|
const id = makeScopeId({ filePath: 'f.ts', range: r(1, 0, 2, 0), kind });
|
|
expect(id.endsWith(`:${kind}`)).toBe(true);
|
|
}
|
|
});
|
|
|
|
it('returns the SAME string reference for structurally identical inputs (interned)', () => {
|
|
const a = makeScopeId({ filePath: 'src/a.ts', range: r(5, 4, 10, 2), kind: 'Function' });
|
|
const b = makeScopeId({ filePath: 'src/a.ts', range: r(5, 4, 10, 2), kind: 'Function' });
|
|
expect(a).toBe(b);
|
|
// `Object.is` catches the same reference even for weird strings.
|
|
expect(Object.is(a, b)).toBe(true);
|
|
});
|
|
|
|
it('distinguishes ids that differ only by filePath', () => {
|
|
const a = makeScopeId({ filePath: 'src/a.ts', range: r(1, 0, 2, 0), kind: 'Module' });
|
|
const b = makeScopeId({ filePath: 'src/b.ts', range: r(1, 0, 2, 0), kind: 'Module' });
|
|
expect(a).not.toBe(b);
|
|
});
|
|
|
|
it('distinguishes ids that differ only by range', () => {
|
|
const a = makeScopeId({ filePath: 'f.ts', range: r(1, 0, 2, 0), kind: 'Function' });
|
|
const b = makeScopeId({ filePath: 'f.ts', range: r(1, 0, 3, 0), kind: 'Function' });
|
|
expect(a).not.toBe(b);
|
|
});
|
|
|
|
it('distinguishes ids that differ only by kind', () => {
|
|
const a = makeScopeId({ filePath: 'f.ts', range: r(1, 0, 2, 0), kind: 'Function' });
|
|
const b = makeScopeId({ filePath: 'f.ts', range: r(1, 0, 2, 0), kind: 'Block' });
|
|
expect(a).not.toBe(b);
|
|
});
|
|
|
|
it('is safe to call repeatedly (pure)', () => {
|
|
const inputs = { filePath: 'f.ts', range: r(1, 0, 5, 0), kind: 'Function' as const };
|
|
const ids = Array.from({ length: 10 }, () => makeScopeId(inputs));
|
|
expect(new Set(ids).size).toBe(1);
|
|
});
|
|
|
|
it('clearScopeIdInternPool drops the intern pool without changing id shape', () => {
|
|
const before = makeScopeId({ filePath: 'f.ts', range: r(1, 0, 2, 0), kind: 'Module' });
|
|
clearScopeIdInternPool();
|
|
const after = makeScopeId({ filePath: 'f.ts', range: r(1, 0, 2, 0), kind: 'Module' });
|
|
expect(after).toBe(before); // same string value, canonical-by-construction
|
|
});
|
|
});
|