mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-10-06 02:49:56 +00:00
* feat(type-env): constructor-call type inference for TypeEnv (Phase 1)
Add extractInitializer as a Tier 1 fallback in buildTypeEnv: when a
declaration node has no explicit type annotation, infer the type from
constructor-call patterns (new X(), X::new(), X::default(), $x = new X()).
Languages covered: TypeScript/JS, Java (var), Rust, PHP, C++ (auto).
Python/Kotlin/Swift deferred — need symbol-table access to distinguish
class constructors from function calls.
Adds 20 new unit tests covering constructor inference, annotation
precedence, and known limitations across all supported languages.
* fix(type-env): class-aware constructor resolution, multi-declarator fix
- Add collectClassNames pre-scan: walks AST to build Set<string> of
class/struct names defined in the file
- C++ extractInitializer uses classNames.has() to verify identifier is
a known class before inferring (auto x = User() resolves, auto x =
getUser() does not — no false positives)
- Add InitializerExtractor type that receives classNames parameter
- Fix env.size gating: always call extractInitializer when available,
so mixed declarators like const a: A = x, b = new B() resolve both
- Add env.has() guard in Java extractInitializer to skip already-bound vars
- Document Rust new/default whitelist rationale
- Pin all test assertions, add mixed multi-declarator test case
* fix(type-env): resolve Self/self/static/parent to actual type names
- Rust: Self::new()/Self::default() resolves to enclosing impl type
- PHP: new self()/static() resolves to enclosing class, parent() to superclass
- Rust: Tier 0 annotation guard prevents overwrite by constructor inference
- Rust: mut_pattern handling in extractVarName for let mut bindings
- TS: fix misleading comment in extractInitializer
- 58 tests passing (3 new Self/self resolution tests)
* perf(type-env): single-pass AST walk with closure-scoped state
Refactors buildTypeEnv to use closures instead of passing mutable state
as parameters. classNames, env, and config are captured by the inner
walk and extractTypeBinding functions — no parameter mutation.
- Eliminates separate collectClassNames pre-scan (O(2n) → O(n))
- config looked up once per file instead of per-node
- 29 fewer lines
* feat(type-env): constructor-inferred type resolution for all languages
Add cross-file constructor type inference to the ingestion pipeline,
enabling receiver-type disambiguation for member calls like
`user.save()` when the variable is assigned from a constructor without
explicit type annotations.
Pipeline changes:
- Add extractInitializer to Python and Swift type extractors
- Add CONSTRUCTOR_BINDING_SCANNERS for Python, Swift, C/C++ in type-env
- Wire constructorBindings through parse-worker → parsing-processor →
pipeline → processCallsFromExtracted
- Rewrite resolveCallTarget receiver-type filtering (step D) to use
tiered import resolution (same-file → import-scoped → global) before
falling back to fuzzy ownerId matching
- Use collectTieredCandidates for constructor binding verification
instead of raw lookupFuzzy
Bug fixes:
- Fix C++ inline method query: @definition.method was captured on
field_declaration_list instead of function_definition, causing wrong
parameterCount for all inline class methods
- Fix parse-worker accumulated/flush results missing constructorBindings
CI changes:
- Add swift.test.ts to ci-integration pipeline group and coverage job
- Update ci-report to fetch base branch (main) coverage for delta
reporting instead of showing config thresholds
- Add per-suite timing breakdown table (unit/integration/total)
- Add expandable skipped test details section
Tests: 288 passed, 4 skipped (swift — macOS only) across 10 languages
- 36 new constructor-inferred integration tests (4 per language)
- 10 fixture directories with cross-file constructor patterns
- TypeScript, JavaScript, Java, Kotlin, Python, PHP, Rust, Go, C++, Swift
* fix(type-extractors): add type assertion for LanguageTypeConfig
* feat(ruby): constructor-inferred type resolution and self-receiver mapping
Add Ruby User.new constructor binding scanner to type-env, enabling
receiver-type disambiguation for member calls like user.save vs repo.save.
Add self/this → enclosing class resolution in lookupTypeEnv so self.method()
calls resolve to the correct class even when the method name is ambiguous.
* docs: update README with constructor inference and self/this resolution details
* refactor(ingestion): unified ResolutionContext replaces fragmented map passing
Introduce createResolutionContext() as the single resolution API for all
processors. Eliminates duplicated tier-selection logic, fixes heritage
namedImportMap bug, and adds per-file resolution caching.
- NEW resolution-context.ts: closure-factory with resolve(), per-file cache,
TIER_CONFIDENCE constant, and shared ResolutionTier type
- DELETE symbol-resolver.ts: zero production importers, logic now in
resolution-context.ts
- call-processor: all functions take ctx instead of 6 separate maps,
collectTieredCandidates removed (ctx.resolve replaces it),
D4 redundant re-resolve eliminated
- heritage-processor: takes ctx, resolveHeritageId helper extracts
repeated 14-line fallback pattern, namedImportMap now included
- import-processor: takes ctx, dead createImportMap/createPackageMap/
createNamedImportMap factories removed
- pipeline: creates single ctx, wires onProgress to all processors,
logs cache hit rate in dev mode
- Tier renamed: unique-global → global (honest about returning all candidates)
- Tests migrated: 1178 unit + 84 integration passing
* feat(type-env): self/this/super resolution, TypeEnvironment API, and review fixes
Add cross-language receiver keyword resolution:
- self/this/$this → enclosing class name via AST walk
- super/base/parent → parent class name via heritage AST extraction
(8 grammar variants: TS/JS, Java, Python, Ruby, C#, PHP, Kotlin, C++, Swift)
- D-phase widening in resolveCallTarget for super→parent method dispatch
Introduce TypeEnvironment API replacing loose TypeEnvResult + lookupTypeEnv:
- buildTypeEnv() returns TypeEnvironment with .lookup() method
- Single-pass AST walk merges constructor binding scan (was separate traversal)
- ClassNameLookup type replaces over-broad ReadonlySet<string> facade
- Memoized class name lookups to avoid redundant SymbolTable scans
Code review fixes (6 agents, 11 findings):
- Replace ctx.resolve(name, '') hack with direct symbols.lookupFuzzy()
- Extract scope key helpers (extractFuncNameFromScope, receiverKey)
- Simplify D-phase from 5 steps to 4 with deduped typeNodeIds
- Remove C from CONSTRUCTOR_BINDING_SCANNERS (YAGNI — C has no constructors)
- Cache Map reuse in ResolutionContext to reduce GC pressure
- Remove unused TieredCandidates import
Integration tests for self/this, parent, and super resolution across all
12 supported languages with per-language fixture directories.
* fix(type-env): generic parent resolution, TS cast inference, C++ brace-init
Fix generic parent class breaking super resolution:
- extractParentClassFromNode now uses extractSimpleTypeName to strip
generic params (Base<T> → Base) and qualified names (models.Model → Model)
- Affects TS, Java, Python, C# heritage extraction
Fix TypeScript new X() as T / new X()! missed inference:
- Unwrap as_expression and non_null_expression before checking for
new_expression in extractInitializer
Fix C++ brace-init User{} missed inference:
- Handle compound_literal_expression with type_identifier child
in extractInitializer
Clean up deprecated lookupTypeEnv:
- Remove standalone lookupTypeEnv export, migrate all callers to
TypeEnvironment.lookup() method
- Update all 80+ test assertions to use the new API
Integration test fixtures added:
- typescript-cast-constructor-inference (new X() as T, new X()!)
- typescript/java/csharp/kotlin-generic-parent-resolution
- cpp-brace-init-inference (auto x = User{})
* fix(type-extractors): Go &User{}, TS double-cast, Swift .init inference
Fix Go pointer-to-struct literal not inferred:
- Unwrap unary_expression (address-of &) before composite_literal check
- user := &User{} now correctly infers type User
Fix TypeScript double-cast only unwrapping one level:
- Change if to while loop for nested as_expression/non_null_expression
- new User() as unknown as Admin now correctly infers type User
Fix Swift User.init(name:) explicit init call missed:
- Handle navigation_expression callee with .init suffix in extractInitializer
Integration test fixtures:
- go-pointer-constructor-inference (&User{}, &Repo{})
- typescript-double-cast-inference (as unknown as T)
* feat: Rust struct literal, Python qualified ctor, Go new(), Swift .init scanner
- Rust: handle struct_expression in extractInitializer (User { name: "alice" })
- Python: support attribute nodes in extractInitializer (models.User("alice"))
and the cross-file scanner — extractSimpleTypeName handles qualified names
- Go: handle new(User) built-in in extractGoShortVarDeclaration
- Swift: extend CONSTRUCTOR_BINDING_SCANNERS to handle navigation_expression
callee for User.init(name:) cross-file resolution
Unit tests: 87 → 96 (Rust struct literal, Go new(), Python qualified ctor,
Python scanner qualified, plus edge cases)
Integration tests: 4 new describe blocks with fixtures
* fix: Rust Self{} resolution, C++ scoped brace-init, PHP promotion params, Ruby constants
- Rust: resolve Self {} struct literal to enclosing impl type (was stored as "Self")
- C++: replace type_identifier guard with extractSimpleTypeName for compound_literal_expression,
enabling ns::User{} scoped brace-init (closes previously deferred gap)
- PHP: add property_promotion_parameter to TYPED_PARAMETER_TYPES for PHP 8.0+
constructor property promotion (__construct(private Foo $x))
- Ruby: extend extractRubyConstructorBinding to accept constant left-hand side
(REPO = Repo.new)
Unit tests: 96 → 101 (+5: Rust Self{} ×2, C++ ns::User{} ×1, PHP promotion ×1,
Ruby constant ×1)
Integration tests: 4 new describe blocks with fixtures
* feat: Phase 1 type resolution gaps — walrus, PHP properties, nullable, Go make/assert
Phase 1 quick wins from the type resolution gap analysis:
1. Python walrus operator := (named_expression) — extractInitializer + scanner
2. PHP 7.4+ typed class properties — property_declaration in extractDeclaration
3. Nullable union unwrapping — User | null → User in extractSimpleTypeName
4. Go make() builtin — slice/map element type extraction
5. Go type assertions — iface.(User) type extraction
Also: PHP primitive_type handling in extractSimpleTypeName (string, int, etc.)
Unit tests: 101 → 114 (+13)
Integration tests: 8 new describe blocks with fixtures
* feat: Phase 2 type resolution gaps — C++ range-for, Rust if-let, C# pattern matching, Python class annotations
Phase 2 medium-effort improvements:
1. C++ range-for with explicit type — for (User& u : vec) binds u: User
2. Rust if-let/while-let captured_pattern — user @ User { .. } binds user: User
3. C# is-pattern matching — if (obj is User user) binds user: User
4. Python class-level annotations — confirmed already working, added tests
Unit tests: 114 → 127 (+13)
Integration tests: 11 new test cases with fixtures
496 lines
20 KiB
TypeScript
496 lines
20 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { createResolutionContext, type ResolutionContext } from '../../src/core/ingestion/resolution-context.js';
|
|
import { createSymbolTable } from '../../src/core/ingestion/symbol-table.js';
|
|
import { isFileInPackageDir } from '../../src/core/ingestion/import-processor.js';
|
|
|
|
/** Helper: resolve to single best definition (refuses ambiguous global) */
|
|
const resolveOne = (ctx: ResolutionContext, name: string, fromFile: string) => {
|
|
const tiered = ctx.resolve(name, fromFile);
|
|
if (!tiered) return null;
|
|
if (tiered.tier === 'global' && tiered.candidates.length !== 1) return null;
|
|
return tiered.candidates[0];
|
|
};
|
|
|
|
/** Helper: resolve with tier metadata (refuses ambiguous global) */
|
|
const resolveInternal = (ctx: ResolutionContext, name: string, fromFile: string) => {
|
|
const tiered = ctx.resolve(name, fromFile);
|
|
if (!tiered) return null;
|
|
if (tiered.tier === 'global' && tiered.candidates.length !== 1) return null;
|
|
return { definition: tiered.candidates[0], tier: tiered.tier, candidateCount: tiered.candidates.length };
|
|
};
|
|
|
|
describe('ResolutionContext.resolve — resolveSymbol compatibility', () => {
|
|
let ctx: ResolutionContext;
|
|
|
|
beforeEach(() => {
|
|
ctx = createResolutionContext();
|
|
});
|
|
|
|
describe('Tier 1: Same-file resolution', () => {
|
|
it('resolves symbol defined in the same file', () => {
|
|
ctx.symbols.add('src/models/user.ts', 'User', 'Class:src/models/user.ts:User', 'Class');
|
|
|
|
const result = resolveOne(ctx, 'User', 'src/models/user.ts');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.nodeId).toBe('Class:src/models/user.ts:User');
|
|
expect(result!.filePath).toBe('src/models/user.ts');
|
|
expect(result!.type).toBe('Class');
|
|
});
|
|
|
|
it('prefers same-file over imported definition', () => {
|
|
ctx.symbols.add('src/local.ts', 'Config', 'Class:src/local.ts:Config', 'Class');
|
|
ctx.symbols.add('src/shared.ts', 'Config', 'Class:src/shared.ts:Config', 'Class');
|
|
ctx.importMap.set('src/local.ts', new Set(['src/shared.ts']));
|
|
|
|
const result = resolveOne(ctx, 'Config', 'src/local.ts');
|
|
|
|
expect(result!.nodeId).toBe('Class:src/local.ts:Config');
|
|
expect(result!.filePath).toBe('src/local.ts');
|
|
});
|
|
});
|
|
|
|
describe('Tier 2: Import-scoped resolution', () => {
|
|
it('resolves symbol from an imported file', () => {
|
|
ctx.symbols.add('src/services/auth.ts', 'AuthService', 'Class:src/services/auth.ts:AuthService', 'Class');
|
|
ctx.importMap.set('src/controllers/login.ts', new Set(['src/services/auth.ts']));
|
|
|
|
const result = resolveOne(ctx, 'AuthService', 'src/controllers/login.ts');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.nodeId).toBe('Class:src/services/auth.ts:AuthService');
|
|
expect(result!.filePath).toBe('src/services/auth.ts');
|
|
});
|
|
|
|
it('prefers imported definition over non-imported with same name', () => {
|
|
ctx.symbols.add('src/services/logger.ts', 'Logger', 'Class:src/services/logger.ts:Logger', 'Class');
|
|
ctx.symbols.add('src/testing/mock-logger.ts', 'Logger', 'Class:src/testing/mock-logger.ts:Logger', 'Class');
|
|
ctx.importMap.set('src/app.ts', new Set(['src/services/logger.ts']));
|
|
|
|
const result = resolveOne(ctx, 'Logger', 'src/app.ts');
|
|
|
|
expect(result!.nodeId).toBe('Class:src/services/logger.ts:Logger');
|
|
expect(result!.filePath).toBe('src/services/logger.ts');
|
|
});
|
|
|
|
it('handles file with no imports — unique global falls through', () => {
|
|
ctx.symbols.add('src/utils.ts', 'Helper', 'Class:src/utils.ts:Helper', 'Class');
|
|
|
|
const result = resolveOne(ctx, 'Helper', 'src/app.ts');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.nodeId).toBe('Class:src/utils.ts:Helper');
|
|
});
|
|
});
|
|
|
|
describe('Tier 3: Global resolution', () => {
|
|
it('resolves unique global when not in imports', () => {
|
|
ctx.symbols.add('src/external/base.ts', 'BaseModel', 'Class:src/external/base.ts:BaseModel', 'Class');
|
|
ctx.importMap.set('src/app.ts', new Set(['src/other.ts']));
|
|
|
|
const result = resolveOne(ctx, 'BaseModel', 'src/app.ts');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.nodeId).toBe('Class:src/external/base.ts:BaseModel');
|
|
});
|
|
|
|
it('refuses ambiguous global — returns null when multiple candidates exist', () => {
|
|
ctx.symbols.add('src/a.ts', 'Config', 'Class:src/a.ts:Config', 'Class');
|
|
ctx.symbols.add('src/b.ts', 'Config', 'Class:src/b.ts:Config', 'Class');
|
|
|
|
const result = resolveOne(ctx, 'Config', 'src/other.ts');
|
|
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('ctx.resolve returns all candidates at global tier (consumers decide)', () => {
|
|
ctx.symbols.add('src/a.ts', 'Config', 'Class:src/a.ts:Config', 'Class');
|
|
ctx.symbols.add('src/b.ts', 'Config', 'Class:src/b.ts:Config', 'Class');
|
|
|
|
const tiered = ctx.resolve('Config', 'src/other.ts');
|
|
|
|
expect(tiered).not.toBeNull();
|
|
expect(tiered!.tier).toBe('global');
|
|
expect(tiered!.candidates.length).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('null cases', () => {
|
|
it('returns null for unknown symbol', () => {
|
|
const result = resolveOne(ctx, 'NonExistent', 'src/app.ts');
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('returns null when symbol table is empty', () => {
|
|
const result = resolveOne(ctx, 'Anything', 'src/app.ts');
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('type preservation', () => {
|
|
it('preserves Interface type for heritage resolution', () => {
|
|
ctx.symbols.add('src/interfaces.ts', 'ILogger', 'Interface:src/interfaces.ts:ILogger', 'Interface');
|
|
ctx.importMap.set('src/app.ts', new Set(['src/interfaces.ts']));
|
|
|
|
const result = resolveOne(ctx, 'ILogger', 'src/app.ts');
|
|
|
|
expect(result!.type).toBe('Interface');
|
|
});
|
|
|
|
it('preserves Class type for heritage resolution', () => {
|
|
ctx.symbols.add('src/base.ts', 'BaseService', 'Class:src/base.ts:BaseService', 'Class');
|
|
ctx.importMap.set('src/app.ts', new Set(['src/base.ts']));
|
|
|
|
const result = resolveOne(ctx, 'BaseService', 'src/app.ts');
|
|
|
|
expect(result!.type).toBe('Class');
|
|
});
|
|
});
|
|
|
|
describe('heritage-specific scenarios', () => {
|
|
it('resolves C# interface vs class ambiguity via imports', () => {
|
|
ctx.symbols.add('src/logging/ilogger.cs', 'ILogger', 'Interface:src/logging/ilogger.cs:ILogger', 'Interface');
|
|
ctx.symbols.add('src/testing/ilogger.cs', 'ILogger', 'Class:src/testing/ilogger.cs:ILogger', 'Class');
|
|
ctx.importMap.set('src/services/auth.cs', new Set(['src/logging/ilogger.cs']));
|
|
|
|
const result = resolveOne(ctx, 'ILogger', 'src/services/auth.cs');
|
|
|
|
expect(result!.type).toBe('Interface');
|
|
expect(result!.filePath).toBe('src/logging/ilogger.cs');
|
|
});
|
|
|
|
it('resolves parent class from imported file for extends', () => {
|
|
ctx.symbols.add('src/api/controller.ts', 'UserController', 'Class:src/api/controller.ts:UserController', 'Class');
|
|
ctx.symbols.add('src/base/controller.ts', 'BaseController', 'Class:src/base/controller.ts:BaseController', 'Class');
|
|
ctx.importMap.set('src/api/controller.ts', new Set(['src/base/controller.ts']));
|
|
|
|
const result = resolveOne(ctx, 'BaseController', 'src/api/controller.ts');
|
|
|
|
expect(result!.nodeId).toBe('Class:src/base/controller.ts:BaseController');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('ResolutionContext.resolve — tier metadata', () => {
|
|
let ctx: ResolutionContext;
|
|
|
|
beforeEach(() => {
|
|
ctx = createResolutionContext();
|
|
});
|
|
|
|
it('returns same-file tier for Tier 1 match', () => {
|
|
ctx.symbols.add('src/a.ts', 'Foo', 'Class:src/a.ts:Foo', 'Class');
|
|
|
|
const result = resolveInternal(ctx, 'Foo', 'src/a.ts');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.tier).toBe('same-file');
|
|
expect(result!.candidateCount).toBe(1);
|
|
expect(result!.definition.nodeId).toBe('Class:src/a.ts:Foo');
|
|
});
|
|
|
|
it('returns import-scoped tier for Tier 2 match', () => {
|
|
ctx.symbols.add('src/logger.ts', 'Logger', 'Class:src/logger.ts:Logger', 'Class');
|
|
ctx.symbols.add('src/mock.ts', 'Logger', 'Class:src/mock.ts:Logger', 'Class');
|
|
ctx.importMap.set('src/app.ts', new Set(['src/logger.ts']));
|
|
|
|
const result = resolveInternal(ctx, 'Logger', 'src/app.ts');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.tier).toBe('import-scoped');
|
|
});
|
|
|
|
it('returns global tier for Tier 3 match', () => {
|
|
ctx.symbols.add('src/only.ts', 'Singleton', 'Class:src/only.ts:Singleton', 'Class');
|
|
|
|
const result = resolveInternal(ctx, 'Singleton', 'src/other.ts');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.tier).toBe('global');
|
|
expect(result!.candidateCount).toBe(1);
|
|
});
|
|
|
|
it('returns null for ambiguous global — refuses to guess', () => {
|
|
ctx.symbols.add('src/a.ts', 'Config', 'Class:src/a.ts:Config', 'Class');
|
|
ctx.symbols.add('src/b.ts', 'Config', 'Class:src/b.ts:Config', 'Class');
|
|
|
|
const result = resolveInternal(ctx, 'Config', 'src/other.ts');
|
|
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('returns null for unknown symbol', () => {
|
|
const result = resolveInternal(ctx, 'Ghost', 'src/any.ts');
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('Tier 1 wins over Tier 2 — same-file takes priority', () => {
|
|
ctx.symbols.add('src/app.ts', 'Util', 'Function:src/app.ts:Util', 'Function');
|
|
ctx.symbols.add('src/lib.ts', 'Util', 'Function:src/lib.ts:Util', 'Function');
|
|
ctx.importMap.set('src/app.ts', new Set(['src/lib.ts']));
|
|
|
|
const result = resolveInternal(ctx, 'Util', 'src/app.ts');
|
|
|
|
expect(result!.tier).toBe('same-file');
|
|
expect(result!.definition.filePath).toBe('src/app.ts');
|
|
});
|
|
});
|
|
|
|
describe('negative tests — ambiguous refusal per language family', () => {
|
|
let ctx: ResolutionContext;
|
|
|
|
beforeEach(() => {
|
|
ctx = createResolutionContext();
|
|
});
|
|
|
|
it('TS/JS: two Logger definitions with no import → returns null', () => {
|
|
ctx.symbols.add('src/services/logger.ts', 'Logger', 'Class:src/services/logger.ts:Logger', 'Class');
|
|
ctx.symbols.add('src/testing/logger.ts', 'Logger', 'Class:src/testing/logger.ts:Logger', 'Class');
|
|
|
|
const result = resolveOne(ctx, 'Logger', 'src/app.ts');
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('Java: same-named class in different packages, no import → returns null', () => {
|
|
ctx.symbols.add('com/example/models/User.java', 'User', 'Class:com/example/models/User.java:User', 'Class');
|
|
ctx.symbols.add('com/example/dto/User.java', 'User', 'Class:com/example/dto/User.java:User', 'Class');
|
|
|
|
const result = resolveOne(ctx, 'User', 'com/example/services/UserService.java');
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('C/C++: type defined in transitively-included header → returns null (not reachable via direct import)', () => {
|
|
ctx.symbols.add('src/c.h', 'Widget', 'Struct:src/c.h:Widget', 'Struct');
|
|
ctx.symbols.add('src/d.h', 'Widget', 'Struct:src/d.h:Widget', 'Struct');
|
|
ctx.importMap.set('src/a.c', new Set(['src/b.h']));
|
|
|
|
const result = resolveOne(ctx, 'Widget', 'src/a.c');
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('C#: two IService interfaces in different namespaces, no import → returns null', () => {
|
|
ctx.symbols.add('src/Services/IService.cs', 'IService', 'Interface:src/Services/IService.cs:IService', 'Interface');
|
|
ctx.symbols.add('src/Testing/IService.cs', 'IService', 'Interface:src/Testing/IService.cs:IService', 'Interface');
|
|
|
|
const result = resolveOne(ctx, 'IService', 'src/App.cs');
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('heritage false-positive guard', () => {
|
|
let ctx: ResolutionContext;
|
|
|
|
beforeEach(() => {
|
|
ctx = createResolutionContext();
|
|
});
|
|
|
|
it('null from resolve prevents false edge — generateId fallback produces synthetic ID, not wrong match', () => {
|
|
ctx.symbols.add('src/api/base.ts', 'BaseController', 'Class:src/api/base.ts:BaseController', 'Class');
|
|
ctx.symbols.add('src/testing/base.ts', 'BaseController', 'Class:src/testing/base.ts:BaseController', 'Class');
|
|
|
|
const result = resolveOne(ctx, 'BaseController', 'src/routes/admin.ts');
|
|
expect(result).toBeNull();
|
|
|
|
ctx.importMap.set('src/routes/admin.ts', new Set(['src/api/base.ts']));
|
|
const resolved = resolveOne(ctx, 'BaseController', 'src/routes/admin.ts');
|
|
expect(resolved).not.toBeNull();
|
|
expect(resolved!.filePath).toBe('src/api/base.ts');
|
|
});
|
|
});
|
|
|
|
describe('lookupExactFull', () => {
|
|
it('returns full SymbolDefinition for same-file lookup via O(1) direct storage', () => {
|
|
const symbolTable = createSymbolTable();
|
|
symbolTable.add('src/models/user.ts', 'User', 'Class:src/models/user.ts:User', 'Class');
|
|
|
|
const result = symbolTable.lookupExactFull('src/models/user.ts', 'User');
|
|
|
|
expect(result).not.toBeUndefined();
|
|
expect(result!.nodeId).toBe('Class:src/models/user.ts:User');
|
|
expect(result!.filePath).toBe('src/models/user.ts');
|
|
expect(result!.type).toBe('Class');
|
|
});
|
|
|
|
it('returns undefined for non-existent symbol', () => {
|
|
const symbolTable = createSymbolTable();
|
|
const result = symbolTable.lookupExactFull('src/app.ts', 'NonExistent');
|
|
expect(result).toBeUndefined();
|
|
});
|
|
|
|
it('returns undefined for wrong file', () => {
|
|
const symbolTable = createSymbolTable();
|
|
symbolTable.add('src/a.ts', 'Foo', 'Class:src/a.ts:Foo', 'Class');
|
|
|
|
const result = symbolTable.lookupExactFull('src/b.ts', 'Foo');
|
|
expect(result).toBeUndefined();
|
|
});
|
|
|
|
it('shares same object reference between fileIndex and globalIndex', () => {
|
|
const symbolTable = createSymbolTable();
|
|
symbolTable.add('src/x.ts', 'Bar', 'Class:src/x.ts:Bar', 'Class');
|
|
|
|
const fromExact = symbolTable.lookupExactFull('src/x.ts', 'Bar');
|
|
const fromFuzzy = symbolTable.lookupFuzzy('Bar')[0];
|
|
|
|
expect(fromExact).toBe(fromFuzzy);
|
|
});
|
|
|
|
it('preserves optional callable metadata on stored definitions', () => {
|
|
const symbolTable = createSymbolTable();
|
|
symbolTable.add('src/math.ts', 'sum', 'Function:src/math.ts:sum', 'Function', { parameterCount: 2 });
|
|
|
|
const fromExact = symbolTable.lookupExactFull('src/math.ts', 'sum');
|
|
const fromFuzzy = symbolTable.lookupFuzzy('sum')[0];
|
|
|
|
expect(fromExact?.parameterCount).toBe(2);
|
|
expect(fromFuzzy.parameterCount).toBe(2);
|
|
expect(fromExact).toBe(fromFuzzy);
|
|
});
|
|
});
|
|
|
|
describe('isFileInPackageDir', () => {
|
|
it('matches file directly in the package directory', () => {
|
|
expect(isFileInPackageDir('internal/auth/handler.go', '/internal/auth/')).toBe(true);
|
|
});
|
|
|
|
it('matches with leading path segments', () => {
|
|
expect(isFileInPackageDir('myrepo/internal/auth/handler.go', '/internal/auth/')).toBe(true);
|
|
expect(isFileInPackageDir('src/github.com/user/repo/internal/auth/handler.go', '/internal/auth/')).toBe(true);
|
|
});
|
|
|
|
it('rejects files in subdirectories', () => {
|
|
expect(isFileInPackageDir('internal/auth/middleware/jwt.go', '/internal/auth/')).toBe(false);
|
|
});
|
|
|
|
it('matches any file extension in the directory', () => {
|
|
expect(isFileInPackageDir('internal/auth/README.md', '/internal/auth/')).toBe(true);
|
|
expect(isFileInPackageDir('Models/User.cs', '/Models/')).toBe(true);
|
|
expect(isFileInPackageDir('internal/auth/handler_test.go', '/internal/auth/')).toBe(true);
|
|
});
|
|
|
|
it('rejects files not in the package', () => {
|
|
expect(isFileInPackageDir('internal/db/connection.go', '/internal/auth/')).toBe(false);
|
|
});
|
|
|
|
it('handles backslash paths (Windows)', () => {
|
|
expect(isFileInPackageDir('internal\\auth\\handler.go', '/internal/auth/')).toBe(true);
|
|
});
|
|
|
|
it('matches C# namespace directories', () => {
|
|
expect(isFileInPackageDir('MyProject/Models/User.cs', '/MyProject/Models/')).toBe(true);
|
|
expect(isFileInPackageDir('MyProject/Models/Order.cs', '/MyProject/Models/')).toBe(true);
|
|
expect(isFileInPackageDir('MyProject/Models/Sub/Nested.cs', '/MyProject/Models/')).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('Tier 2b: PackageMap resolution (Go)', () => {
|
|
let ctx: ResolutionContext;
|
|
|
|
beforeEach(() => {
|
|
ctx = createResolutionContext();
|
|
});
|
|
|
|
it('resolves symbol via PackageMap when not in ImportMap', () => {
|
|
ctx.symbols.add('internal/auth/handler.go', 'HandleLogin', 'Function:internal/auth/handler.go:HandleLogin', 'Function');
|
|
ctx.packageMap.set('cmd/server/main.go', new Set(['/internal/auth/']));
|
|
|
|
const result = ctx.resolve('HandleLogin', 'cmd/server/main.go');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.tier).toBe('import-scoped');
|
|
expect(result!.candidates[0].filePath).toBe('internal/auth/handler.go');
|
|
});
|
|
|
|
it('does not resolve symbol from wrong package', () => {
|
|
ctx.symbols.add('internal/db/connection.go', 'Connect', 'Function:internal/db/connection.go:Connect', 'Function');
|
|
ctx.packageMap.set('cmd/server/main.go', new Set(['/internal/auth/']));
|
|
|
|
const result = ctx.resolve('Connect', 'cmd/server/main.go');
|
|
|
|
// Not in imported package, single global def → global tier
|
|
expect(result).not.toBeNull();
|
|
expect(result!.tier).toBe('global');
|
|
});
|
|
|
|
it('Tier 2a (ImportMap) takes precedence over Tier 2b (PackageMap)', () => {
|
|
ctx.symbols.add('internal/auth/handler.go', 'Validate', 'Function:internal/auth/handler.go:Validate', 'Function');
|
|
ctx.symbols.add('internal/db/validator.go', 'Validate', 'Function:internal/db/validator.go:Validate', 'Function');
|
|
|
|
ctx.importMap.set('cmd/server/main.go', new Set(['internal/db/validator.go']));
|
|
ctx.packageMap.set('cmd/server/main.go', new Set(['/internal/auth/']));
|
|
|
|
const result = ctx.resolve('Validate', 'cmd/server/main.go');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.tier).toBe('import-scoped');
|
|
expect(result!.candidates[0].filePath).toBe('internal/db/validator.go');
|
|
});
|
|
|
|
it('resolves both symbols in same imported package', () => {
|
|
ctx.symbols.add('internal/auth/handler.go', 'Run', 'Function:internal/auth/handler.go:Run', 'Function');
|
|
ctx.symbols.add('internal/auth/worker.go', 'Run', 'Function:internal/auth/worker.go:Run', 'Function');
|
|
ctx.packageMap.set('cmd/main.go', new Set(['/internal/auth/']));
|
|
|
|
const result = ctx.resolve('Run', 'cmd/main.go');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.tier).toBe('import-scoped');
|
|
expect(result!.candidates.length).toBe(2);
|
|
});
|
|
|
|
it('returns global without packageMap when ambiguous', () => {
|
|
ctx.symbols.add('internal/auth/handler.go', 'X', 'Function:internal/auth/handler.go:X', 'Function');
|
|
ctx.symbols.add('internal/db/handler.go', 'X', 'Function:internal/db/handler.go:X', 'Function');
|
|
|
|
const result = resolveInternal(ctx, 'X', 'cmd/main.go');
|
|
|
|
// No import or package match, 2 candidates → ambiguous → null
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('per-file cache', () => {
|
|
let ctx: ResolutionContext;
|
|
|
|
beforeEach(() => {
|
|
ctx = createResolutionContext();
|
|
});
|
|
|
|
it('caches results per file', () => {
|
|
ctx.symbols.add('src/a.ts', 'Foo', 'Class:src/a.ts:Foo', 'Class');
|
|
|
|
ctx.enableCache('src/a.ts');
|
|
const r1 = ctx.resolve('Foo', 'src/a.ts');
|
|
const r2 = ctx.resolve('Foo', 'src/a.ts');
|
|
ctx.clearCache();
|
|
|
|
// Same object reference from cache
|
|
expect(r1).toBe(r2);
|
|
expect(ctx.getStats().cacheHits).toBe(1);
|
|
expect(ctx.getStats().cacheMisses).toBe(1);
|
|
});
|
|
|
|
it('resolve works without cache enabled', () => {
|
|
ctx.symbols.add('src/a.ts', 'Foo', 'Class:src/a.ts:Foo', 'Class');
|
|
|
|
const result = ctx.resolve('Foo', 'src/a.ts');
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result!.candidates[0].nodeId).toBe('Class:src/a.ts:Foo');
|
|
expect(ctx.getStats().cacheHits).toBe(0);
|
|
});
|
|
|
|
it('cache does not leak across files', () => {
|
|
ctx.symbols.add('src/a.ts', 'Foo', 'Class:src/a.ts:Foo', 'Class');
|
|
|
|
ctx.enableCache('src/a.ts');
|
|
ctx.resolve('Foo', 'src/a.ts'); // cached for a.ts
|
|
|
|
// Resolve from different file — should NOT use cache
|
|
const r = ctx.resolve('Foo', 'src/b.ts');
|
|
ctx.clearCache();
|
|
|
|
// Foo is not in src/b.ts, so same-file fails. Falls to global with 1 candidate.
|
|
expect(r!.tier).toBe('global');
|
|
});
|
|
});
|