mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-09-10 22:43:40 +00:00
Some checks are pending
CodeQL / Analyze (python) (push) Waiting to run
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
* docs(plans): add provider-hook value-refs plan (#2437) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(plans): deepen #2437 plan to USES + property-dispatch design Design revised after prior-art research (Kythe ref vs ref/call, Joern METHOD_REF, Feldthaus field-based call graphs, CodeQL impliedReceiverStep): registration sites emit reference-class USES, invocation is recovered by a field-based property-dispatch pass synthesizing CALLS at member-call sites. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(scope-resolution): model provider-hook value references (#2437) Functions referenced as object-literal property values (provider hooks like emitScopeCaptures: emitCppScopeCaptures) previously produced no edge at all, so impact/context reported a false-safe 0 upstream dependents. Two coordinated halves, per prior art (Kythe ref vs ref/call, Joern METHOD_REF, Feldthaus ICSE'13 field-based call graphs, CodeQL impliedReceiverStep): - Registration -> USES: new ReferenceKind 'value-ref'; TS/JS queries capture pair values and shorthand properties (with @reference.property-key); emitted as a reference-class USES edge, reason 'scope-resolution: value-ref'. Resolution is callable-gated so plain values emit nothing. - Dispatch -> CALLS: new shared pass emitPropertyDispatchCalls synthesizes CALLS (reason 'property-dispatch', confidence 0.7, per-key fan-out cap 32 calibrated on this repo's 16-provider hook tables) from member-call sites to every function registered under the same property key. Deviation from plan: the pass owns value-ref resolution entirely via the post-finalize findCallableBindingInScope walker — the shared registries only see pre-finalize local bindings, so imported hooks (the c-cpp.ts case) were unresolvable through lookupForSite; Reference.propertyKey passthrough dropped as unnecessary. SCHEMA_BUMP 13 -> 14: ParsedFile gains value-ref sites + propertyKey. Verified end-to-end: impact(emitCppScopeCaptures, upstream) now reports 8 impacted / HIGH with extractParsedFile (true dispatch caller) at d=1 via property-dispatch and the c-cpp.ts registration via USES. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(scope-resolution): cover value-ref registration and property dispatch (#2437) Integration: same-file/cross-file/aliased/shorthand registrations emit USES; non-callable and destructuring values emit nothing; dispatch sites gain property-dispatch CALLS (incl. JS twins and per-language partitioning); fan-out-capped keys are dropped entirely; factory-call values unchanged. Unit: capture-shape pins for @reference.value-ref + @reference.property-key. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(scope-resolution): surface dropped property-dispatch keys in stats (#2437) Review finding: skippedKeys was returned but discarded — a hook table larger than the fan-out cap silently reopened the #2437 gap for those keys. Log dropped keys and fold value-ref USES + dispatch CALLS into referenceEdgesEmitted. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(plans): add callable reference-flow implementation plan * fix(scope-resolution): close property-dispatch review gaps * feat(scope-resolution): add callable flow facts * feat(scope-resolution): resolve callable value flow * feat(scope-resolution): resolve callable references across providers * fix: harden callable reference flow resolution * fix(scope-resolution): preserve callable binding semantics * docs(plans): add pr-2522-review-fixes plan Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(storage): bump INCREMENTAL_SCHEMA_VERSION for callable-value-flow edges Callable-value-flow CALLS/USES edges (#2437) can connect two files whose content did not change, but the incremental write set only covers changed files — a top-up against a pre-v7 index would silently omit the new edges for every unchanged file pair, indefinitely. Force the one-time full re-analyze (review finding 1, #2522). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(storage): sanitize callable-flow sites per-site at load, log drops The load-time validator rejected the WHOLE ParsedFile when one site was malformed or over-bound, with no logging — and C++ legitimately emits empty-string parameterTypes entries ('' = unknown, the ReferenceSite.argumentTypes convention) for cv-only/ERROR-recovered types, so real repos fell into a permanent, silent warm-cache-miss reparse loop through the #1983-sensitive main-thread path (review finding 7, #2522). Now: '' entries are valid in type arrays; a malformed/over-bound site drops only itself (counted, warned once per load); only non-array garbage — evidence the serialization itself is untrustworthy — rejects the file. Deviation from plan §6 wording: validator-side tolerance replaces emit-side clamps — smaller diff, same asymmetry closed at the single chokepoint. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(scope-resolution): keep declarations in the union for reassigned callable cells The binding-lookup suppression for fact-constrained cells was wholesale: reassigning a declared function through its own name (greet = other; greet()) deferred the call to the solver, which then refused the lexical lookup that resolves the declaration — an unresolvable RHS yielded zero CALLS for a call that resolved pre-flow (review finding 8, #2522). Suppression now applies only to cells bound by FORMAL facts — its actual purpose (a parameter whose grammar emits no declaration binding must not adopt a same-named outer function). Copy/alias/store/load destinations keep their declaration as an inclusion seed (Andersen-style union). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(scope-resolution): count forfeited deferred sites in the budget-bailout warning On work-budget exhaustion the deferred invoke sites end the run with zero CALLS — free-call fallback and reference emission already skipped them — but the warning said 'ordinary graph emission remains untouched', which is false for exactly those sites. The warning context now carries the unresolved deferred-site count and the comment states the real cost (review finding: budget-bailout honesty, #2522). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(scope-resolution): surface dropped property-dispatch keys in stats and warn payload The over-cap warning carried only a count; the dropped key NAMES were discarded and RunScopeResolutionStats had no field, so the PR-body claim 'includes them in resolver statistics' was unimplemented (review finding, #2522; reviewer ask on the fan-out cap). The warn payload now names up to 20 dropped keys and the stats carry propertyDispatchSkippedKeys. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor(scope-resolution): drop producer-less ownerQualifiedName from formal sites No capture emitter anywhere produces @callable-flow.owner-qualified-name — the solver branch consuming it was unreachable in production, yet the field was typed, parsed, validated, and unit-tested with hand-built input (review finding 16, #2522; YAGNI). Re-add with a real producer if C++ qualified member declarators ever need it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor(scope-resolution): drop dead callable-flow knobs CallableFlowPassingMode 'callable-object' had no producer and no consumer distinguishing it, and CallableFlowCaptureOptions.extractCallArguments had no language providing it (unlike its live sibling extractCallCallee) — review finding 17, #2522 (YAGNI). The invocation-kind 'callable-object' is a different, live concept and stays. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(ingestion): bind subscripted callable cells to the container, not the index terminalIdentifier iterates children in reverse, so tbl[i] = handler seeded the INDEX variable's cell (polluting a same-named formal) and tbl[i](7) looked up the callee under i in a different scope — no join, no CALLS edge for the classic function-pointer-array dispatch (review finding 12, #2522). Subscript nodes now recurse into their container field only, in both bindingIdentifier and terminalIdentifier, across the fielded grammars (C/C++/JS/TS/Python/Go/Java). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(ingestion): make cross-function file-scope callable bindings resolvable Two stacked gaps killed the canonical C callback-registration pattern (fp assigned in init(), called in run()) — the exact #2437 false-safe this PR exists to fix (review finding H1, #2522): 1. isVisibleValueBinding only consulted assignment regions and formals, so a call in a function OTHER than the assigning one emitted no invoke fact. A declared callable-typed binding is now a value binding wherever its declaration is visible (visibleCallableSignature). 2. The C scope query had no @declaration.variable pattern for function- pointer declarators — void (*fp)(int); created no scope-tree binding, so the seed (init) and invoke (run) cells canonicalized to different keys and never joined. Both bare and initialized forms now bind. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(c): detect variadic parameters via the named variadic_parameter node tree-sitter-c materializes '...' as a named variadic_parameter node; the anonymous-token checks never matched, so variadic function-pointer signatures were emitted with a wrong fixed arity and no '...' sentinel (review finding, #2522). C++ is unaffected ('...' stays an anonymous token there); the token checks remain for such grammars. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(ingestion): emit invoke facts for field-stored callable member calls The C ops-vtable pattern (o->run = handler; o->run(1)) captured the store but never the call — the member path in emitCallFacts bailed for languages without protocol methods, and the value-binding index recorded the member store under the OBJECT's name ('o'), not the member's ('run') (review finding 11/M3, #2522). Member destinations now also record their terminal member name, and a member call whose name-cell has a visible store emits an indirect invoke — gated on the store so plain accessor calls (map.get) stay inert. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(cpp): disambiguate (obj->*ptr)() ERROR recovery by token order tree-sitter-cpp groups the recovered '->*' two ways depending on error-recovery cost (identifier lengths): [identifier, ERROR '->*m'] or [ERROR 'obj->*', identifier]. The recovery assumed the first shape, so the second silently swapped receiver/member and dropped the call site — the committed test passed only by name luck (review finding H2, #2522). The identifier's position relative to '->*' inside the ERROR now decides roles. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(cpp): class members are never file-local in hasFileLocalCallableLinkage The name-keyed file-local set is populated from every static declaration, so an in-class 'static void make();' (external linkage — in-class static means no-instance) and any member sharing a name with a static free function were over-marked, refusing legitimate cross-file declaration/definition joins (review finding 13/M2, #2522). Method and Constructor defs now bypass the name-set, per the hook's own linkage-only contract. Deviation from plan step 13: the regression is a unit-level contract pin rather than an end-to-end join test — C++ merges out-of-line member definitions onto the member node by qualified identity, so the graph shape cannot discriminate the join refusal for members. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(cpp): classify parameter passing mode from the declarator chain only A whole-subtree scan for reference_declarator inverted copy vs alias: void reg(void (*cb)(int& out)) marked the by-value pointer cb as 'reference' because of the NESTED parameter's int&, making the solver back-propagate formal targets into every caller's argument cell — alias semantics for a copy (review finding 14/M5, #2522). The chain walk never descends into nested parameter lists; a reference anywhere ON the chain (int& x, void (*&cb)(int)) still aliases. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(ruby): bare identifiers are calls, not callable references Ruby parses a receiver-less zero-arg method call identically to a variable read, so 'action = process' — which CALLS process and stores its return — seeded action with the callable and minted a wrong CALLS edge from any dispatch through it, confirmed end-to-end (review finding 15/HIGH, #2522). New provider knob bareNamesAreCalls: a bare name that is not a provably local value binding and not an explicit reference form (method(:x), lambda/proc) emits no flow fact, on both the assignment and argument paths. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(go): pair multi-value := positionally instead of cross-wiring The shared field fallback took the FIRST LHS identifier and the LAST RHS identifier of Go's expression_list pair, cross-wiring 'a, b := f, g' and synthesizing a garbage comma-joined qualified name — the real relationships were silently dropped (review finding 16, #2522). extractAssignment may now return multiple pairs; Go pairs list entries positionally and emits nothing for a length mismatch (multi-return call RHS). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(java): drop get/test from callableProtocolMethods 'get' and 'test' collide with ubiquitous non-functional-interface APIs (Map/List/Optional/Future.get), so every ordinary container access emitted a spurious callable-object invoke fact — high-volume misleading graph facts with a cross-wiring risk on receiver-name reuse (review finding 17, #2522). Supplier.get/Predicate.test dispatch is deliberately traded away until the check can gate on the receiver's declared type. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(rust): pin the qualified-name no-degrade guard as a hard invariant Rust's scoped_identifier callable-reference capture over-includes unit enum variants and associated constants (Shape::Square seeds as if callable); they stay edge-free only because resolveSeedCandidates refuses to degrade an unresolved qualified name to a simple-name lookup (review finding 18, #2522). Capture-side type filtering would false-negative on tuple-variant constructors, so the guard IS the contract: documented as a hard invariant (Go's mis-shaped multi-value forms also rely on it) and pinned end-to-end. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(php): remove nonexistent optional_parameter node type tree-sitter-php has no 'optional_parameter' — defaults ride on simple_parameter — so the entry was dead weight the #1920 literal gate does not cover for capture-option Sets (review finding 19, #2522). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(cobol): detect procedure pointers on fixed-format sources Two stacked defects made the feature a no-op on classic sequence-numbered fixed format (review finding 20/H3, #2522): 1. parseDataItemClauses' USAGE alternation knew POINTER but not PROCEDURE-POINTER/FUNCTION-POINTER, so the dataItems filter was dead. 2. The raw-line fallback scanned UNCLEANED text, where the sequence number satisfied the leading digits and the LEVEL NUMBER got captured as the pointer name. It now scans preprocessed lines and requires a letter- initial name (COBOL data names must contain a letter). 161 COBOL preprocessor/copy-expander tests stay green; free-format matrix case unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(cobol): skip comment lines in SET seed/copy scans A commented-out SET (indicator-column '*'/'/' or free-format '*>') produced a live seed and a false CALLS edge from dead code (review finding 21/M1, #2522). The scan now skips indicator-column comment lines and strips inline '*>' tails before matching. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(architecture): document callable-flow-only mode and skipped-key reporting The Callable-value flow section omitted scopeResolutionEdgeMode: 'callable-flow-only' — a real emit-pipeline branch that suppresses all ordinary emission for standalone providers (review finding 22, #2522) — and predated the skipped-key names/stats surfacing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(scope-resolution): correct value-ref resolution attribution and stale pdg-gating comments The value-ref contract comment claimed MethodRegistry resolution — the mechanism is the post-finalize findCallableBindingInScope walker owned by emitPropertyDispatchCalls (resolveReferenceSites skips these sites). Three 'only under --pdg' calleeIdSink comments were falsified by the #2437 gating change (callee-id-sink.ts's header was updated; these copies were missed). Review finding 23, #2522. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(ingestion): direct unit coverage for synthesizeCallableFlowCaptures The 1,100-line shared synthesizer had no test naming it — only downstream consumers were covered (review finding 24, #2522). Pins seed/invoke/ formal/argument emission, subscript container binding, store-gated member invokes, produced-value guards, and the bareNamesAreCalls knob over a minimal options object so assertions target the synthesizer's own semantics. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(resolvers): deepen shallow-language coverage; fix Kotlin/Swift reassignment gaps it exposed Adds the COBOL SET x TO y copy-branch scenario and conditional-assignment scenarios for Kotlin, C#, Swift, and Dart (10 languages previously had one generic case each — review finding 25, #2522). The new scenarios exposed two real capture gaps, fixed here: - tree-sitter-kotlin's 'assignment' node is fieldless, so nested reassignments (chosen = ::target inside a block) produced no flow facts; Kotlin's extractAssignment now decomposes it positionally. - tree-sitter-swift fields its assignment as target:/result:, neither in the shared fallback's field lists; both added. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(infra): literal-validation gate for callable-capture option Sets The #1920 gate validates query literals and exported configs but not the module-private *_CALLABLE_CAPTURE_OPTIONS Sets consumed by the shared synthesizer — a typo'd node type silently captures nothing (PHP shipped a dead 'optional_parameter'; review finding 26, #2522). Every <key>NodeTypes Set literal is now validated against its language's grammar; name-carrying sets (callableProtocolMethods, memberPointerOperators) are deliberately outside the contract. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(storage): centralize corrupt-fixture casts into makeStoreEntry The callable-flow store tests scattered 'as unknown as' double-casts per fixture (review finding 27, #2522; standing no-as-any rule). One typed helper now owns the single controlled escape hatch for building malformed serialization-boundary payloads. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore(bench): refresh capture fingerprints after review fixes python-scope: the committed baseline (8d5c3699) never matched this branch's code — CI's benchmarks arm was red on the PR head (review finding 2/HIGH, #2522); regenerated (a99e69ab), scaling 1.04 in budget. scope-capture: ruby/cpp/swift/java/kotlin drifted from the review-fix commits (bare-name suppression, passing modes + ->* recovery, assignment fields, protocol narrowing, positional assignment); all 14 languages re-verified PASS with ratios <= 1.18 against the 1.5 budget. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore(docs): untrack docs/plans working documents docs/ is gitignored (local working docs); the plan files were force-added past the ignore. Untracked from the index only — they stay on disk. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(golden): regenerate captures goldens after callable-flow review fixes The per-language digest guards (csharp/go/php/python/ruby/rust/swift) locked the pre-fix capture output; the review-fix series intentionally changed it — store-gated member invokes, subscript container binding, Ruby bare-name suppression, Swift assignment fields, positional pairing. Regenerated with UPDATE_GOLDEN=1; clean verification run 59/59; all other parity/golden guards (pipeline-graph, spring-route, python parity) pass untouched at 33/33. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(ingestion): prototypes are callees, not callable value cells The cross-function visibility fix indexed EVERY signature-bearing declaration as a value binding — including plain function/method prototypes (void f(int);). Every call to a declared function then became an indirect invoke, and with emitCanonicalInvokeReference (C/C++) minted a free-call reference that resolved through the registry, bypassing the precise passes' two-phase/ambiguity/subobject suppression — eight phantom CALLS edges in the cpp resolver suite on CI. Only declarations whose binding identifier sits under a pointer/ parenthesized declarator (callable-typed variables like void (*fp)(int);) create value cells now. cpp resolver suite 331/331; callable-value-flow + C/C++ suites 181/181 (the cross-function fp regression still passes); cpp fingerprint rebaselined, both bench gates PASS. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
793 lines
28 KiB
TypeScript
793 lines
28 KiB
TypeScript
/**
|
|
* Unit tests for `scope-extractor.extract` — the 5-pass driver
|
|
* (RFC §5.3; Ring 2 PKG #919).
|
|
*
|
|
* Tests are organized by pass so a regression localizes to the pass it
|
|
* broke. A `MockProvider` emits synthetic `CaptureMatch[]` with no real
|
|
* AST; the extractor is pure given those captures.
|
|
*/
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
import type {
|
|
Capture,
|
|
CaptureMatch,
|
|
ParsedImport,
|
|
ParsedTypeBinding,
|
|
ReferenceKind,
|
|
Scope,
|
|
ScopeKind,
|
|
SymbolDefinition,
|
|
} from 'gitnexus-shared';
|
|
import {
|
|
extract,
|
|
selectNodeBearingDef,
|
|
type ScopeExtractorHooks,
|
|
} from '../../../src/core/ingestion/scope-extractor.js';
|
|
|
|
// ─── Synthetic-capture helpers ──────────────────────────────────────────────
|
|
|
|
const cap = (
|
|
name: string,
|
|
startLine: number,
|
|
startCol: number,
|
|
endLine: number,
|
|
endCol: number,
|
|
text = '',
|
|
): Capture => ({
|
|
name,
|
|
range: { startLine, startCol, endLine, endCol },
|
|
text,
|
|
});
|
|
|
|
const scopeMatch = (
|
|
kind: Lowercase<ScopeKind>,
|
|
startLine: number,
|
|
startCol: number,
|
|
endLine: number,
|
|
endCol: number,
|
|
): CaptureMatch => ({
|
|
[`@scope.${kind}`]: cap(`@scope.${kind}`, startLine, startCol, endLine, endCol),
|
|
});
|
|
|
|
const declMatch = (
|
|
kindStr: string,
|
|
name: string,
|
|
startLine: number,
|
|
startCol: number,
|
|
endLine: number,
|
|
endCol: number,
|
|
extras: Record<string, Capture> = {},
|
|
): CaptureMatch => ({
|
|
[`@declaration.${kindStr}`]: cap(`@declaration.${kindStr}`, startLine, startCol, endLine, endCol),
|
|
'@declaration.name': cap('@declaration.name', startLine, startCol, endLine, endCol, name),
|
|
...extras,
|
|
});
|
|
|
|
const importMatch = (
|
|
startLine: number,
|
|
startCol: number,
|
|
endLine: number,
|
|
endCol: number,
|
|
): CaptureMatch => ({
|
|
'@import.statement': cap('@import.statement', startLine, startCol, endLine, endCol),
|
|
});
|
|
|
|
const typeBindingMatch = (
|
|
startLine: number,
|
|
startCol: number,
|
|
endLine: number,
|
|
endCol: number,
|
|
): CaptureMatch => ({
|
|
'@type-binding.parameter': cap('@type-binding.parameter', startLine, startCol, endLine, endCol),
|
|
});
|
|
|
|
const refMatch = (
|
|
suffix: string,
|
|
name: string,
|
|
startLine: number,
|
|
startCol: number,
|
|
endLine: number,
|
|
endCol: number,
|
|
extras: Record<string, Capture> = {},
|
|
): CaptureMatch => ({
|
|
[`@reference.${suffix}`]: cap(`@reference.${suffix}`, startLine, startCol, endLine, endCol),
|
|
'@reference.name': cap('@reference.name', startLine, startCol, endLine, endCol, name),
|
|
...extras,
|
|
});
|
|
|
|
// ─── MockProvider ───────────────────────────────────────────────────────────
|
|
//
|
|
// The extractor declares its dependency on a narrow `ScopeExtractorHooks`
|
|
// surface — not the full `LanguageProvider`. Tests implement exactly that
|
|
// surface, so adding a new hook to `extract()` that's not in
|
|
// `ScopeExtractorHooks` is a compile error, not a silent test pass.
|
|
|
|
function mockProvider(hooks: Partial<ScopeExtractorHooks> = {}): ScopeExtractorHooks {
|
|
return hooks;
|
|
}
|
|
|
|
// ─── §Pass 1: scope tree construction ──────────────────────────────────────
|
|
|
|
describe('Pass 1: scope tree', () => {
|
|
it('creates a single Module scope from one @scope.module match', () => {
|
|
const result = extract([scopeMatch('module', 1, 0, 100, 0)], 'a.ts', mockProvider());
|
|
expect(result.scopes).toHaveLength(1);
|
|
expect(result.scopes[0]!.kind).toBe('Module');
|
|
expect(result.scopes[0]!.parent).toBeNull();
|
|
expect(result.moduleScope).toBe(result.scopes[0]!.id);
|
|
});
|
|
|
|
it('synthesizes a single empty Module scope when the provider emits no captures', () => {
|
|
const result = extract([], 'empty.py', mockProvider());
|
|
expect(result.scopes).toHaveLength(1);
|
|
expect(result.scopes[0]!).toMatchObject({
|
|
kind: 'Module',
|
|
parent: null,
|
|
range: { startLine: 0, startCol: 0, endLine: 0, endCol: 0 },
|
|
});
|
|
expect(result.moduleScope).toBe(result.scopes[0]!.id);
|
|
});
|
|
|
|
it('nests Class under Module when the class range is contained in the module range', () => {
|
|
const result = extract(
|
|
[scopeMatch('module', 1, 0, 100, 0), scopeMatch('class', 5, 0, 50, 0)],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
expect(result.scopes).toHaveLength(2);
|
|
const cls = result.scopes.find((s) => s.kind === 'Class')!;
|
|
const mod = result.scopes.find((s) => s.kind === 'Module')!;
|
|
expect(cls.parent).toBe(mod.id);
|
|
});
|
|
|
|
it('nests Method under Class, Class under Module — deep nesting', () => {
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('class', 5, 0, 50, 0),
|
|
scopeMatch('function', 10, 2, 30, 2),
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
const mod = result.scopes.find((s) => s.kind === 'Module')!;
|
|
const cls = result.scopes.find((s) => s.kind === 'Class')!;
|
|
const fn = result.scopes.find((s) => s.kind === 'Function')!;
|
|
expect(cls.parent).toBe(mod.id);
|
|
expect(fn.parent).toBe(cls.id);
|
|
});
|
|
|
|
it('places non-nested siblings at the same level under the module', () => {
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('function', 10, 0, 20, 0),
|
|
scopeMatch('function', 30, 0, 40, 0),
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
const mod = result.scopes.find((s) => s.kind === 'Module')!;
|
|
const fns = result.scopes.filter((s) => s.kind === 'Function');
|
|
expect(fns).toHaveLength(2);
|
|
for (const fn of fns) expect(fn.parent).toBe(mod.id);
|
|
});
|
|
|
|
it('uses `provider.resolveScopeKind` to override the default kind from the suffix', () => {
|
|
// Provider upgrades a `@scope.block` to `Expression` for a comprehension-
|
|
// style use case.
|
|
const result = extract(
|
|
[scopeMatch('module', 1, 0, 100, 0), scopeMatch('block', 10, 0, 15, 0)],
|
|
'a.ts',
|
|
mockProvider({
|
|
resolveScopeKind: (match) => (match['@scope.block'] !== undefined ? 'Expression' : null),
|
|
}),
|
|
);
|
|
expect(result.scopes.find((s) => s.kind === 'Expression')).toBeDefined();
|
|
});
|
|
|
|
it('throws ScopeTreeInvariantError when siblings overlap (provider bug)', () => {
|
|
expect(() =>
|
|
extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('function', 10, 0, 20, 0),
|
|
scopeMatch('function', 15, 0, 25, 0), // overlaps
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
),
|
|
).toThrow(/overlap/i);
|
|
});
|
|
|
|
it('synthesizes a Module scope and re-parents orphan Function when no Module is present', () => {
|
|
const result = extract([scopeMatch('function', 1, 0, 10, 0)], 'a.ts', mockProvider());
|
|
const moduleScope = result.scopes.find((s) => s.kind === 'Module');
|
|
expect(moduleScope).toBeDefined();
|
|
const fnScope = result.scopes.find((s) => s.kind === 'Function');
|
|
expect(fnScope).toBeDefined();
|
|
expect(fnScope!.parent).toBe(moduleScope!.id);
|
|
});
|
|
});
|
|
|
|
// ─── §Pass 2: declarations + local bindings ────────────────────────────────
|
|
|
|
describe('Pass 2: declarations + local bindings', () => {
|
|
it('routes one multi-topic match through both scope and declaration passes', () => {
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
{
|
|
'@scope.function': cap('@scope.function', 5, 0, 20, 0, 'render'),
|
|
'@declaration.function': cap('@declaration.function', 5, 0, 20, 0, 'render'),
|
|
'@declaration.name': cap('@declaration.name', 5, 0, 5, 6, 'render'),
|
|
},
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
|
|
expect(result.scopes.some((scope) => scope.kind === 'Function')).toBe(true);
|
|
expect(result.localDefs).toHaveLength(1);
|
|
expect(result.localDefs[0]!.qualifiedName).toBe('render');
|
|
});
|
|
|
|
it('attaches a Class declaration to its enclosing Module scope', () => {
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('class', 5, 0, 50, 0),
|
|
declMatch('class', 'User', 5, 6, 5, 10),
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
// The declaration sits at line 5 → innermost scope is Class (at 5:0..50:0).
|
|
const cls = result.scopes.find((s) => s.kind === 'Class')!;
|
|
expect(cls.ownedDefs).toHaveLength(1);
|
|
expect(cls.ownedDefs[0]!.type).toBe('Class');
|
|
expect(cls.ownedDefs[0]!.qualifiedName).toBe('User');
|
|
expect(cls.bindings.get('User')).toBeDefined();
|
|
expect(cls.bindings.get('User')![0]!.origin).toBe('local');
|
|
});
|
|
|
|
it('records the declaration in `localDefs` as well', () => {
|
|
const result = extract(
|
|
[scopeMatch('module', 1, 0, 100, 0), declMatch('function', 'render', 5, 0, 5, 6)],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
expect(result.localDefs).toHaveLength(1);
|
|
expect(result.localDefs[0]!.type).toBe('Function');
|
|
});
|
|
|
|
it('honors `provider.bindingScopeFor` to hoist a binding to an outer scope', () => {
|
|
// Treat every declaration as hoisted to the module scope.
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('function', 10, 0, 30, 0),
|
|
declMatch('variable', 'x', 15, 4, 15, 5),
|
|
],
|
|
'a.ts',
|
|
mockProvider({
|
|
bindingScopeFor: (_match, _innermost, scopeTree) => {
|
|
for (const s of scopeTree.byId.values()) if (s.kind === 'Module') return s.id;
|
|
return null;
|
|
},
|
|
}),
|
|
);
|
|
const mod = result.scopes.find((s) => s.kind === 'Module')!;
|
|
const fn = result.scopes.find((s) => s.kind === 'Function')!;
|
|
// Binding hoisted to module; function scope's bindings empty for 'x'.
|
|
expect(mod.bindings.get('x')).toBeDefined();
|
|
expect(fn.bindings.get('x')).toBeUndefined();
|
|
// `ownedDefs` stays structural (innermost = function).
|
|
expect(fn.ownedDefs).toHaveLength(1);
|
|
});
|
|
|
|
it('ignores declarations with unknown kind suffixes', () => {
|
|
const result = extract(
|
|
[scopeMatch('module', 1, 0, 100, 0), declMatch('mystery', 'x', 5, 0, 5, 1)],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
expect(result.localDefs).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
// ─── §Pass 3: imports ──────────────────────────────────────────────────────
|
|
|
|
describe('Pass 3: raw imports', () => {
|
|
it('collects imports via `provider.interpretImport`', () => {
|
|
const named: ParsedImport = {
|
|
kind: 'named',
|
|
localName: 'User',
|
|
importedName: 'User',
|
|
targetRaw: './models',
|
|
};
|
|
const result = extract(
|
|
[scopeMatch('module', 1, 0, 100, 0), importMatch(3, 0, 3, 30)],
|
|
'a.ts',
|
|
mockProvider({
|
|
interpretImport: () => named,
|
|
}),
|
|
);
|
|
expect(result.parsedImports).toEqual([named]);
|
|
});
|
|
|
|
it('drops imports when `interpretImport` returns null', () => {
|
|
const result = extract(
|
|
[scopeMatch('module', 1, 0, 100, 0), importMatch(3, 0, 3, 30)],
|
|
'a.ts',
|
|
mockProvider({
|
|
interpretImport: () => null,
|
|
}),
|
|
);
|
|
expect(result.parsedImports).toEqual([]);
|
|
});
|
|
|
|
it('emits no imports when the provider does not implement `interpretImport`', () => {
|
|
const result = extract(
|
|
[scopeMatch('module', 1, 0, 100, 0), importMatch(3, 0, 3, 30)],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
expect(result.parsedImports).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// ─── §Pass 4: type bindings ───────────────────────────────────────────────
|
|
|
|
describe('Pass 4: type bindings', () => {
|
|
it('attaches a parameter-annotation TypeRef to the innermost scope', () => {
|
|
const parsed: ParsedTypeBinding = {
|
|
boundName: 'user',
|
|
rawTypeName: 'User',
|
|
source: 'parameter-annotation',
|
|
};
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('function', 5, 0, 20, 0),
|
|
typeBindingMatch(6, 4, 6, 14),
|
|
],
|
|
'a.ts',
|
|
mockProvider({
|
|
interpretTypeBinding: () => parsed,
|
|
}),
|
|
);
|
|
const fn = result.scopes.find((s) => s.kind === 'Function')!;
|
|
const tb = fn.typeBindings.get('user');
|
|
expect(tb).toBeDefined();
|
|
expect(tb!.rawName).toBe('User');
|
|
expect(tb!.source).toBe('parameter-annotation');
|
|
expect(tb!.declaredAtScope).toBe(fn.id);
|
|
});
|
|
|
|
it('skips type-binding matches when the provider returns null', () => {
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('function', 5, 0, 20, 0),
|
|
typeBindingMatch(6, 4, 6, 14),
|
|
],
|
|
'a.ts',
|
|
mockProvider({
|
|
interpretTypeBinding: () => null,
|
|
}),
|
|
);
|
|
const fn = result.scopes.find((s) => s.kind === 'Function')!;
|
|
expect(fn.typeBindings.size).toBe(0);
|
|
});
|
|
});
|
|
|
|
// ─── §Pass 5: reference sites ─────────────────────────────────────────────
|
|
|
|
describe('Pass 5: reference sites', () => {
|
|
it('emits a call reference with the innermost scope anchor', () => {
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('function', 5, 0, 20, 0),
|
|
refMatch('call.free', 'print', 10, 4, 10, 9),
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
const fn = result.scopes.find((s) => s.kind === 'Function')!;
|
|
expect(result.referenceSites).toHaveLength(1);
|
|
expect(result.referenceSites[0]!.name).toBe('print');
|
|
expect(result.referenceSites[0]!.kind).toBe('call');
|
|
expect(result.referenceSites[0]!.callForm).toBe('free');
|
|
expect(result.referenceSites[0]!.inScope).toBe(fn.id);
|
|
});
|
|
|
|
it('classifies member calls via the `@reference.call.member` sub-tag', () => {
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
refMatch('call.member', 'save', 3, 4, 3, 8, {
|
|
'@reference.receiver': cap('@reference.receiver', 3, 0, 3, 4, 'user'),
|
|
}),
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
expect(result.referenceSites[0]!.callForm).toBe('member');
|
|
expect(result.referenceSites[0]!.explicitReceiver).toEqual({ name: 'user' });
|
|
});
|
|
|
|
it('falls back to `provider.classifyCallForm` when the anchor has no sub-tag', () => {
|
|
const result = extract(
|
|
[scopeMatch('module', 1, 0, 100, 0), refMatch('call', 'foo', 3, 0, 3, 3)],
|
|
'a.ts',
|
|
mockProvider({
|
|
classifyCallForm: () => 'member',
|
|
}),
|
|
);
|
|
expect(result.referenceSites[0]!.callForm).toBe('member');
|
|
});
|
|
|
|
it('recognizes all reference kinds (call, read, write, inherits, type, import_use)', () => {
|
|
const kindsToEmit: Array<[string, ReferenceKind]> = [
|
|
['call.free', 'call'],
|
|
['read', 'read'],
|
|
['write', 'write'],
|
|
['inherits', 'inherits'],
|
|
['type', 'type-reference'],
|
|
['import_use', 'import-use'],
|
|
];
|
|
const matches = [
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
...kindsToEmit.map(([suffix], i) => refMatch(suffix, `ref${i}`, 10 + i, 0, 10 + i, 5)),
|
|
];
|
|
const result = extract(matches, 'a.ts', mockProvider());
|
|
expect(result.referenceSites.map((s) => s.kind)).toEqual(kindsToEmit.map(([, kind]) => kind));
|
|
});
|
|
|
|
it('picks the call anchor over a wider-ranged @reference.receiver (regression for KNOWN_SUB_TAGS exclusion)', () => {
|
|
// Regression for the bug fixed before commit: a member call like
|
|
// `user.save()` where the receiver capture (`user`) spans MORE source
|
|
// than the call anchor (`save`). The broadest-range anchor heuristic
|
|
// would have picked the receiver — `anchorCaptureFor` must exclude
|
|
// known sub-tags (`@reference.receiver`, `@reference.name`, etc.) to
|
|
// route the match as a `call` reference.
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
{
|
|
// Receiver spans columns 0-10 (wider).
|
|
'@reference.receiver': cap('@reference.receiver', 3, 0, 3, 10, 'longUserName'),
|
|
// Call name spans columns 11-15 (narrower).
|
|
'@reference.name': cap('@reference.name', 3, 11, 3, 15, 'save'),
|
|
// The anchor — call.member — spans 0-17 (full expression). In the
|
|
// buggy behavior the receiver would have tied-or-won. Even here,
|
|
// the fix guarantees we pick the call anchor, never the sub-tag.
|
|
'@reference.call.member': cap('@reference.call.member', 3, 0, 3, 17),
|
|
},
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
expect(result.referenceSites).toHaveLength(1);
|
|
expect(result.referenceSites[0]!.name).toBe('save'); // NOT 'longUserName'
|
|
expect(result.referenceSites[0]!.kind).toBe('call');
|
|
expect(result.referenceSites[0]!.callForm).toBe('member');
|
|
expect(result.referenceSites[0]!.explicitReceiver).toEqual({ name: 'longUserName' });
|
|
});
|
|
|
|
it('parses arity from @reference.arity when present', () => {
|
|
const result = extract(
|
|
[
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
refMatch('call.free', 'foo', 3, 0, 3, 3, {
|
|
'@reference.arity': cap('@reference.arity', 3, 0, 3, 0, '2'),
|
|
}),
|
|
],
|
|
'a.ts',
|
|
mockProvider(),
|
|
);
|
|
expect(result.referenceSites[0]!.arity).toBe(2);
|
|
});
|
|
});
|
|
|
|
// ─── §Pass 6: callable-value-flow facts ───────────────────────────────────
|
|
|
|
describe('Pass 6: callable-value-flow facts', () => {
|
|
it('omits callableFlowSites when the provider emits no flow captures', () => {
|
|
const result = extract([scopeMatch('module', 1, 0, 100, 0)], 'a.ts', mockProvider());
|
|
expect(result.callableFlowSites).toBeUndefined();
|
|
});
|
|
|
|
it('materializes every normalized fact shape with lexical scopes and JSON-safe metadata', () => {
|
|
const matches: CaptureMatch[] = [
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('function', 10, 0, 60, 0),
|
|
{
|
|
'@callable-flow.seed': cap('@callable-flow.seed', 20, 2, 20, 20),
|
|
'@callable-flow.destination': cap('@callable-flow.destination', 20, 2, 20, 4, 'fp'),
|
|
'@callable-flow.target': cap('@callable-flow.target', 20, 8, 20, 14, 'target'),
|
|
'@callable-flow.target-name': cap('@callable-flow.target-name', 20, 8, 20, 14, 'target'),
|
|
'@callable-flow.target-qualified-name': cap(
|
|
'@callable-flow.target-qualified-name',
|
|
20,
|
|
8,
|
|
20,
|
|
14,
|
|
'Ns.target',
|
|
),
|
|
'@callable-flow.expected-arity': cap('@callable-flow.expected-arity', 20, 2, 20, 2, '1'),
|
|
'@callable-flow.expected-types': cap(
|
|
'@callable-flow.expected-types',
|
|
20,
|
|
2,
|
|
20,
|
|
2,
|
|
'["int"]',
|
|
),
|
|
'@callable-flow.expected-type-classes': cap(
|
|
'@callable-flow.expected-type-classes',
|
|
20,
|
|
2,
|
|
20,
|
|
2,
|
|
'[{"base":"int","cv":"none","indirection":"value","pointerDepth":0}]',
|
|
),
|
|
},
|
|
{
|
|
'@callable-flow.copy': cap('@callable-flow.copy', 21, 2, 21, 10),
|
|
'@callable-flow.source': cap('@callable-flow.source', 21, 8, 21, 10, 'fp'),
|
|
'@callable-flow.destination': cap('@callable-flow.destination', 21, 2, 21, 5, 'fp2'),
|
|
},
|
|
{
|
|
'@callable-flow.alias': cap('@callable-flow.alias', 22, 2, 22, 10),
|
|
'@callable-flow.source': cap('@callable-flow.source', 22, 8, 22, 10, 'fp'),
|
|
'@callable-flow.destination': cap('@callable-flow.destination', 22, 2, 22, 5, 'ref'),
|
|
},
|
|
{
|
|
'@callable-flow.address': cap('@callable-flow.address', 23, 2, 23, 12),
|
|
'@callable-flow.source': cap('@callable-flow.source', 23, 9, 23, 11, 'fp'),
|
|
'@callable-flow.destination': cap('@callable-flow.destination', 23, 2, 23, 6, 'slot'),
|
|
},
|
|
{
|
|
'@callable-flow.store': cap('@callable-flow.store', 24, 2, 24, 14),
|
|
'@callable-flow.source': cap('@callable-flow.source', 24, 10, 24, 14, 'next'),
|
|
'@callable-flow.pointer': cap('@callable-flow.pointer', 24, 3, 24, 7, 'slot'),
|
|
'@callable-flow.pointer-indirection': cap(
|
|
'@callable-flow.pointer-indirection',
|
|
24,
|
|
3,
|
|
24,
|
|
3,
|
|
'1',
|
|
),
|
|
},
|
|
{
|
|
'@callable-flow.load': cap('@callable-flow.load', 25, 2, 25, 14),
|
|
'@callable-flow.pointer': cap('@callable-flow.pointer', 25, 10, 25, 14, 'slot'),
|
|
'@callable-flow.destination': cap('@callable-flow.destination', 25, 2, 25, 5, 'out'),
|
|
},
|
|
{
|
|
'@callable-flow.formal': cap('@callable-flow.formal', 10, 0, 60, 0),
|
|
'@callable-flow.owner': cap('@callable-flow.owner', 10, 0, 60, 0, 'invoke'),
|
|
'@callable-flow.binding': cap('@callable-flow.binding', 10, 15, 10, 17, 'cb'),
|
|
'@callable-flow.parameter-index': cap(
|
|
'@callable-flow.parameter-index',
|
|
10,
|
|
15,
|
|
10,
|
|
15,
|
|
'0',
|
|
),
|
|
'@callable-flow.passing-mode': cap(
|
|
'@callable-flow.passing-mode',
|
|
10,
|
|
15,
|
|
10,
|
|
15,
|
|
'reference',
|
|
),
|
|
},
|
|
{
|
|
'@callable-flow.argument': cap('@callable-flow.argument', 30, 2, 30, 12),
|
|
'@callable-flow.source': cap('@callable-flow.source', 30, 9, 30, 11, 'fp'),
|
|
'@callable-flow.parameter-index': cap('@callable-flow.parameter-index', 30, 9, 30, 9, '0'),
|
|
'@callable-flow.direct-callee-name': cap(
|
|
'@callable-flow.direct-callee-name',
|
|
30,
|
|
2,
|
|
30,
|
|
8,
|
|
'invoke',
|
|
),
|
|
},
|
|
{
|
|
'@callable-flow.invoke': cap('@callable-flow.invoke', 40, 2, 40, 14),
|
|
'@callable-flow.callee': cap('@callable-flow.callee', 40, 8, 40, 10, 'cb'),
|
|
'@callable-flow.receiver': cap('@callable-flow.receiver', 40, 3, 40, 6, 'obj'),
|
|
'@callable-flow.invocation-kind': cap(
|
|
'@callable-flow.invocation-kind',
|
|
40,
|
|
2,
|
|
40,
|
|
2,
|
|
'member-pointer',
|
|
),
|
|
'@callable-flow.arity': cap('@callable-flow.arity', 40, 2, 40, 2, '0'),
|
|
},
|
|
// Malformed facts are ignored defensively.
|
|
{ '@callable-flow.seed': cap('@callable-flow.seed', 50, 2, 50, 8) },
|
|
];
|
|
|
|
const result = extract(matches, 'a.ts', mockProvider());
|
|
const sites = result.callableFlowSites!;
|
|
expect(sites.map((site) => site.kind)).toEqual([
|
|
'seed',
|
|
'copy',
|
|
'alias',
|
|
'address',
|
|
'store',
|
|
'load',
|
|
'formal',
|
|
'argument',
|
|
'invoke',
|
|
]);
|
|
const fnScope = result.scopes.find((scope) => scope.kind === 'Function')!;
|
|
expect(sites[0]).toMatchObject({
|
|
destination: { name: 'fp', inScope: fnScope.id, indirection: 0 },
|
|
targetName: 'target',
|
|
targetQualifiedName: 'Ns.target',
|
|
expectedSignature: { parameterCount: 1, parameterTypes: ['int'] },
|
|
});
|
|
expect(sites[4]).toMatchObject({ pointer: { name: 'slot', indirection: 1 } });
|
|
expect(sites[6]).toMatchObject({
|
|
ownerName: 'invoke',
|
|
parameterIndex: 0,
|
|
passingMode: 'reference',
|
|
binding: { name: 'cb', inScope: fnScope.id },
|
|
});
|
|
expect(sites[7]).toMatchObject({ directCalleeName: 'invoke' });
|
|
expect(sites[8]).toMatchObject({
|
|
invocationKind: 'member-pointer',
|
|
arity: 0,
|
|
callee: { name: 'cb' },
|
|
receiver: { name: 'obj' },
|
|
});
|
|
expect(JSON.parse(JSON.stringify(sites))).toEqual(sites);
|
|
});
|
|
});
|
|
|
|
// ─── §End-to-end fixture ──────────────────────────────────────────────────
|
|
|
|
describe('end-to-end fixture (all 5 passes together)', () => {
|
|
it('produces a well-formed ParsedFile from a representative multi-pass input', () => {
|
|
const matches: CaptureMatch[] = [
|
|
// Pass 1: nested scopes
|
|
scopeMatch('module', 1, 0, 100, 0),
|
|
scopeMatch('class', 5, 0, 50, 0),
|
|
scopeMatch('function', 10, 2, 40, 2),
|
|
// Pass 2: declarations
|
|
declMatch('class', 'User', 5, 6, 5, 10),
|
|
declMatch('method', 'save', 10, 2, 10, 6),
|
|
declMatch('field', 'count', 7, 2, 7, 7),
|
|
// Pass 3: import
|
|
importMatch(3, 0, 3, 30),
|
|
// Pass 4: type binding
|
|
typeBindingMatch(10, 14, 10, 18),
|
|
// Pass 5: references
|
|
refMatch('call.member', 'log', 20, 4, 20, 7, {
|
|
'@reference.receiver': cap('@reference.receiver', 20, 0, 20, 4, 'self'),
|
|
}),
|
|
refMatch('read', 'count', 25, 4, 25, 9),
|
|
];
|
|
|
|
const parsedImport: ParsedImport = {
|
|
kind: 'named',
|
|
localName: 'Logger',
|
|
importedName: 'Logger',
|
|
targetRaw: './logger',
|
|
};
|
|
const parsedTypeBinding: ParsedTypeBinding = {
|
|
boundName: 'name',
|
|
rawTypeName: 'string',
|
|
source: 'parameter-annotation',
|
|
};
|
|
|
|
const result = extract(
|
|
matches,
|
|
'user.ts',
|
|
mockProvider({
|
|
interpretImport: () => parsedImport,
|
|
interpretTypeBinding: () => parsedTypeBinding,
|
|
}),
|
|
);
|
|
|
|
// Three scopes, properly nested.
|
|
expect(result.scopes).toHaveLength(3);
|
|
const kinds = result.scopes.map((s: Scope) => s.kind);
|
|
expect(kinds).toEqual(expect.arrayContaining(['Module', 'Class', 'Function']));
|
|
|
|
// Declarations landed on the correct scopes.
|
|
const cls = result.scopes.find((s) => s.kind === 'Class')!;
|
|
const fn = result.scopes.find((s) => s.kind === 'Function')!;
|
|
expect(cls.ownedDefs.map((d) => d.qualifiedName).sort()).toEqual(['User', 'count'].sort());
|
|
expect(fn.ownedDefs.map((d) => d.qualifiedName)).toEqual(['save']);
|
|
|
|
// Local bindings present.
|
|
expect(cls.bindings.get('User')).toBeDefined();
|
|
expect(cls.bindings.get('count')).toBeDefined();
|
|
expect(fn.bindings.get('save')).toBeDefined();
|
|
|
|
// Import collected.
|
|
expect(result.parsedImports).toEqual([parsedImport]);
|
|
|
|
// Type binding attached to function scope.
|
|
expect(fn.typeBindings.get('name')?.rawName).toBe('string');
|
|
|
|
// References emitted.
|
|
expect(result.referenceSites).toHaveLength(2);
|
|
expect(result.referenceSites.map((r) => r.kind)).toEqual(['call', 'read']);
|
|
|
|
// `localDefs` is the union across scopes.
|
|
expect(result.localDefs).toHaveLength(3);
|
|
expect(result.localDefs.map((d) => d.type).sort()).toEqual(
|
|
['Class', 'Method', 'Property'].sort(),
|
|
);
|
|
|
|
// Module scope id matches the ParsedFile header.
|
|
const mod = result.scopes.find((s) => s.kind === 'Module')!;
|
|
expect(result.moduleScope).toBe(mod.id);
|
|
});
|
|
});
|
|
|
|
describe('selectNodeBearingDef — #1876 one-node-per-binding collapse rule', () => {
|
|
const def = (type: SymbolDefinition['type'], name = 'x'): SymbolDefinition => ({
|
|
nodeId: `def:test.ts#1:0:${type}:${name}`,
|
|
filePath: 'test.ts',
|
|
type,
|
|
qualifiedName: name,
|
|
});
|
|
|
|
it('returns undefined for an empty group', () => {
|
|
expect(selectNodeBearingDef([])).toBeUndefined();
|
|
});
|
|
|
|
it('returns the only def for a single-element group', () => {
|
|
const only = def('Variable');
|
|
expect(selectNodeBearingDef([only])).toBe(only);
|
|
});
|
|
|
|
it('prefers a Function over a co-bound Variable (direct arrow / HOC)', () => {
|
|
const fn = def('Function');
|
|
const variable = def('Variable');
|
|
// Order-independent: function-like wins regardless of position.
|
|
expect(selectNodeBearingDef([variable, fn])).toBe(fn);
|
|
expect(selectNodeBearingDef([fn, variable])).toBe(fn);
|
|
});
|
|
|
|
it('prefers a Method over a co-bound value def', () => {
|
|
const method = def('Method');
|
|
const variable = def('Variable');
|
|
expect(selectNodeBearingDef([variable, method])).toBe(method);
|
|
});
|
|
|
|
it('returns the value def when no function-like def is present (array-method result)', () => {
|
|
const constDef = def('Const');
|
|
expect(selectNodeBearingDef([constDef])).toBe(constDef);
|
|
const variable = def('Variable');
|
|
expect(selectNodeBearingDef([variable])).toBe(variable);
|
|
});
|
|
|
|
it('prefers a value def even when an unranked label appears first', () => {
|
|
const cls = def('Class');
|
|
const variable = def('Variable');
|
|
expect(selectNodeBearingDef([cls, variable])).toBe(variable);
|
|
});
|
|
|
|
it('falls back to the first def for label sets the rule does not rank', () => {
|
|
const cls = def('Class');
|
|
const iface = def('Interface');
|
|
expect(selectNodeBearingDef([cls, iface])).toBe(cls);
|
|
});
|
|
});
|