GitNexus/gitnexus/test/unit/scope-resolution/java/java-interpret.test.ts
Abhinav Pandey 281ce2600c
fix(java): close parsing-layer coverage gaps F35/F38/F41 (#1928) (#2045)
* fix(java): close parsing-layer coverage gaps F35/F38/F41 (#1928)

Registry-primary scope-resolution path (the live one post-#942/#943):

- F35 [HIGH]: qualified / qualified-generic constructor calls. `new pkg.Foo()`
  parses as a `scoped_type_identifier` that the query bound only as
  `@reference.call.constructor.qualified` with no `@reference.name`, so the
  scope extractor fell back to the whole-expression anchor and the reference
  name became the raw `new pkg.Foo()` text (never resolved). Bind the simple
  -name tail (end-anchored last child) and add an arm for the previously
  uncaptured `new pkg.Box<String>()` (qualified + generic) shape.

- F38 [MEDIUM]: `super(...)` / `this(...)` explicit constructor invocations,
  modeled as `explicit_constructor_invocation` and never matched by the scope
  query, dropped the chained-constructor CALLS edges. Synthesize them with the
  target resolved structurally (this -> enclosing type name; super -> superclass
  tail via the shared javaBaseLookupNameNode, skipping implicit Object) plus
  arity for overload disambiguation.

- F41 [LOW]: interpretJavaTypeBinding stripped the qualifier before generics, so
  a qualified generic type arg (`Map<String, com.example.User>`) was cut inside
  the generic into `User>`. Strip generics first, then the qualifier; make the
  erasure fallback qualifier-tolerant.

F36/F37 already landed upstream (#1940/#1956); F39/F40 are legacy-bank remnants
that are no longer consumed (legacy @import skipped in parse-worker; legacy
@call never read in parse-impl) so they are intentionally left untouched.

Tests: low-level capture unit tests (constructor shapes incl. double-match
guard; super/this/enum/implicit-Object), interpretJavaTypeBinding unit tests
(qualified generic args + the corruption case), and end-to-end resolver tests
with new fixtures asserting the CALLS edges resolve to the correct constructors.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(scope-resolution): register Constructor overload keys so this()/super() chains don't self-loop (#1928 F38 review)

Review of #2045 caught two gaps; both confirmed by reproduction.

P2 — F38 this() emitted a self-loop. On the java-explicit-constructor fixture,
Child(int){ this(); } produced CALLS Child()#0 -> Child()#0 instead of
Child(int)#1 -> Child()#0. Root cause is the language-agnostic graph-bridge: the
parse phase mints distinct Constructor nodes (Child#0, Child#1) carrying
parameterTypes, but node-lookup.ts registered the parameter-types / shape
overload keys only for Function/Method, never Constructor, so both ctors
collapsed onto the first-wins qualified/simple key and the caller Child(int)
resolved to Child#0 (the this() target). Extend the overload keys to Constructor
in both node-lookup.ts (registration) and ids.ts (lookup) via a shared
isOverloadableCallable predicate. Verified the edge now connects distinct nodes
(Child#1 -> Child#0); super(1)->Base#1 still correct. No cross-language
regressions (the 9 worker-path failures reproduce identically on clean HEAD).

Also harden the integration test: it matched the this() edge on name only, which
a self-loop satisfies; now assert the endpoints are DISTINCT constructors.

P3 — F41 order-regression guard was inert (List<Map<String,User>> normalizes to
List under both strip orders). Add List<com.x.Foo<String>> -> List, which is
corrupted to Foo<String>> under the old order and only correct generics-first.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(java): update fingerprint and add notes for constructor query captures in baselines.json

Updated the fingerprint for the Java section and added detailed notes regarding the enhancements in constructor query captures, including qualified and qualified-generic constructor queries. This change reflects ongoing improvements in the parsing layer coverage and fixture updates.

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
2026-06-05 06:39:12 +01:00

86 lines
3.5 KiB
TypeScript

/**
* Coverage for `interpretJavaTypeBinding` type-name normalization, focused on
* F41 (#1928): generics must be stripped BEFORE the qualifier so a qualified
* generic *type argument* (`Map<String, com.example.User>`) is not corrupted
* into `User>` by an early `lastIndexOf('.')` cut.
*/
import { describe, it, expect } from 'vitest';
import type { Capture, CaptureMatch } from 'gitnexus-shared';
import { interpretJavaTypeBinding } from '../../../../src/core/ingestion/languages/java/interpret.js';
const ZERO_RANGE = { startLine: 0, startCol: 0, endLine: 0, endCol: 0 } as const;
const cap = (name: string, text: string): Capture => ({ name, text, range: ZERO_RANGE });
/** Build an annotation-source type binding (`Type name;`). */
function binding(typeText: string, sourceTag = '@type-binding.annotation'): CaptureMatch {
return {
'@type-binding.name': cap('@type-binding.name', 'x'),
'@type-binding.type': cap('@type-binding.type', typeText),
[sourceTag]: cap(sourceTag, typeText),
};
}
/** The normalized `rawTypeName` for a given raw type string. */
function raw(typeText: string): string | undefined {
return interpretJavaTypeBinding(binding(typeText))?.rawTypeName;
}
describe('interpretJavaTypeBinding — type normalization (F41 #1928)', () => {
it('strips a qualifier from a plain qualified type', () => {
expect(raw('com.example.User')).toBe('User');
});
it('strips generics from an unqualified generic base', () => {
expect(raw('BaseModel<T>')).toBe('BaseModel');
});
it('strips generics AND qualifier from a qualified generic base', () => {
expect(raw('com.example.BaseModel<T>')).toBe('BaseModel');
});
it('does not corrupt a qualified generic TYPE ARGUMENT (the F41 bug)', () => {
// Before the fix: stripQualifier ran first → `User>` (trailing bracket).
expect(raw('Map<String, com.example.User>')).toBe('User');
});
it('extracts the element type from a single-arg container with qualified arg', () => {
expect(raw('List<com.example.User>')).toBe('User');
});
it('extracts the element type from a qualified container', () => {
expect(raw('java.util.List<User>')).toBe('User');
});
it('extracts the element type from a simple container', () => {
expect(raw('List<User>')).toBe('User');
expect(raw('Optional<User>')).toBe('User');
});
it('extracts the value type from a two-arg map', () => {
expect(raw('Map<String, User>')).toBe('User');
});
it('passes through a plain simple type', () => {
expect(raw('User')).toBe('User');
});
it('falls back to the raw class name for an unrecognized nested generic', () => {
// Nested generic args defeat the single/two-arg element extraction; the
// erasure fallback keeps the outer class name (pre-existing behavior).
expect(raw('List<Map<String, User>>')).toBe('List');
});
it('keeps the outer class for a QUALIFIED nested generic element (guards the strip order)', () => {
// Unlike `List<Map<String, User>>` (no dot inside the args, so it yields
// `List` under both strip orders), this input has a qualified nested
// element: the OLD order (stripQualifier first) cut inside the generic and
// produced a corrupted `Foo<String>>`; only generics-first yields `List`.
// This is the case that actually fails if the F41 reorder regresses.
expect(raw('List<com.x.Foo<String>>')).toBe('List');
});
it('returns null for a bare `var` with no concrete type', () => {
expect(interpretJavaTypeBinding(binding('var'))).toBeNull();
});
});