GitNexus/gitnexus/test/integration/resolvers/null-ternary.test.ts
shashank-100 858ae768b7
fix(typescript): bind the value arm of a ternary whose other arm is null (#4266)
* fix(typescript): bind the value arm of a ternary whose other arm is null or undefined

* fix(typescript): skip an undefined arm that reads a local binding, and add nullish ternary tests

* fix(typescript): detect undefined shadowing with boundNames from the arm outward, and share the nullish ternary patterns

* fix(typescript): limit ternary bindings to a literal null arm and drop the undefined shadow check

* fix(typescript): keep the selected overload for a null ternary call arm, and loosen negative tests

* fix(typescript): bind a null ternary call arm only in a const, so a mutable local never falls back to the first overload

* Skip inferred call aliases on explicitly annotated TypeScript locals

* Pass the JavaScript grammar to the null-ternary query

* Use TypeScript grammar constraints for ternary call aliases

* Cover annotated call aliases on both sides of a ternary

---------

Co-authored-by: Abhigyan Patwari <126312502+abhigyanpatwari@users.noreply.github.com>
2026-11-30 18:24:13 +00:00

162 lines
6.3 KiB
TypeScript

/**
* Null ternary initializers: `const s = c ? new Svc() : null`.
*
* A `null` arm adds no receiver type, so the variable is bound to the value
* arm, in either position and for constructor, call, member-call and
* identifier arms.
*
* A call arm keeps the overload the call selects, as `const r = parse(1)` does.
* A `let` or `var` call arm is not bound: only a constant keeps the selected
* overload, so a binding there would fall back to the first overload.
*
* `undefined` is not treated as nullish: it is an ordinary name, and a
* parameter or local named `undefined` can hold another receiver. Those arms,
* and two value arms, must never link a single guessed receiver; the
* assertions allow a later fix to link them correctly.
*
* Every receiver class has a same-named `ping`, so a call that resolves by
* name alone, or to the wrong arm, shows up as a wrong or extra target.
*/
import { describe, it, expect } from 'vitest';
import { getRelationships, useFixture } from './helpers.js';
const TS_CLASSES = `export class Svc {
ping(): void {}
next(): Svc { return new Svc(); }
}
export class Other {
ping(): void {}
}
`;
const TS_OVERLOADS = `export class A { m(): void {} }
export class B { m(): void {} }
export function parse(s: string): A;
export function parse(n: number): B;
export function parse(x: string | number): A | B {
return typeof x === 'string' ? new A() : new B();
}
`;
const TS_USE = `import { Svc, Other } from './svc';
import { parse } from './overloads';
export function makeSvc(): Svc { return new Svc(); }
export function ctorOrNull(c: boolean) { const s = c ? new Svc() : null; s?.ping(); }
export function nullOrCtor(c: boolean) { const s = c ? null : new Svc(); s?.ping(); }
export function callOrNull(c: boolean) { const s = c ? makeSvc() : null; s?.ping(); }
export function nullOrMemberCall(c: boolean, x: Svc) { const s = c ? null : x.next(); s?.ping(); }
export function identOrNull(c: boolean, x: Svc) { var s = c ? x : null; s?.ping(); }
export function numberOverloadOrNull(c: boolean) { const r = c ? parse(1) : null; r?.m(); }
export function nullOrStringOverload(c: boolean) { const r = c ? null : parse('x'); r?.m(); }
export function mutableNumberOverloadOrNull(c: boolean) { let r = c ? parse(1) : null; r?.m(); }
export function annotatedNumberOrNull(c: boolean) { const r: any = c ? parse(1) : null; r?.m(); }
export function nullOrAnnotatedNumber(c: boolean) { const r: any = c ? null : parse(1); r?.m(); }
export function ctorOrUndefined(c: boolean) { const s = c ? new Svc() : undefined; s?.ping(); }
export function shadowedUndefined(c: boolean, undefined: Other) {
const s = c ? new Svc() : undefined;
s.ping();
}
export function twoValueArms(c: boolean) { const s = c ? new Svc() : new Other(); s.ping(); }
`;
const JS_CLASSES = `export class Svc {
ping() {}
}
export class Other {
ping() {}
}
`;
const JS_USE = `import { Svc, Other } from './svc.js';
function makeSvc() { return new Svc(); }
export function ctorOrNull(c) { const s = c ? new Svc() : null; s.ping(); }
export function nullOrCtor(c) { const s = c ? null : new Svc(); s.ping(); }
export function identOrNull(c) { const x = new Svc(); let s = c ? x : null; s.ping(); }
export function callOrNull(c) { const s = c ? makeSvc() : null; s.ping(); }
export function nullOrMemberCall(c, x) { const s = c ? null : x.next(); s.ping(); }
export function ctorOrUndefined(c) { const s = c ? new Svc() : undefined; s.ping(); }
export function shadowedUndefined(c, undefined) {
const s = c ? new Svc() : undefined;
s.ping();
}
export function twoValueArms(c) { const s = c ? new Svc() : new Other(); s.ping(); }
`;
describe.each([
{
lang: 'TypeScript',
files: { 'svc.ts': TS_CLASSES, 'overloads.ts': TS_OVERLOADS, 'use.ts': TS_USE },
resolved: ['ctorOrNull', 'nullOrCtor', 'callOrNull', 'nullOrMemberCall', 'identOrNull'],
},
{
lang: 'JavaScript',
files: { 'svc.js': JS_CLASSES, 'use.js': JS_USE },
resolved: ['ctorOrNull', 'nullOrCtor', 'identOrNull'],
},
])('$lang null ternary initializers', ({ lang, files, resolved }) => {
const result = useFixture(`null-ternary-${lang.toLowerCase()}`, files);
const pingOwners = (caller: string) =>
getRelationships(result(), 'CALLS')
.filter((r) => r.source === caller && r.target === 'ping')
.map((r) => r.rel.targetId)
.sort();
it.each(resolved)('%s binds the value arm and calls Svc.ping only', (caller) => {
const owners = pingOwners(caller);
expect(owners).toHaveLength(1);
expect(owners[0]).toContain('Svc.ping');
});
// JavaScript infers no return type for `makeSvc()` / `x.next()`, so its call
// arms have no binding to follow; they must still never link the decoy.
it.each([
'ctorOrUndefined',
...(lang === 'JavaScript' ? ['callOrNull', 'nullOrMemberCall'] : []),
])('%s never links the other receiver', (caller) => {
expect(pingOwners(caller).some((id) => id.includes('Other.ping'))).toBe(false);
});
// Either arm may be the receiver: linking one of them alone is a guess.
it.each(['shadowedUndefined', 'twoValueArms'])('%s does not link a single receiver', (caller) => {
expect(pingOwners(caller)).not.toHaveLength(1);
});
});
describe('TypeScript null ternary call arms keep the selected overload', () => {
const result = useFixture('null-ternary-overloads', {
'svc.ts': TS_CLASSES,
'overloads.ts': TS_OVERLOADS,
'use.ts': TS_USE,
});
const mOwners = (caller: string) =>
getRelationships(result(), 'CALLS')
.filter((r) => r.source === caller && r.target === 'm')
.map((r) => r.rel.targetId);
it('parse(1) beside null selects parse(n: number): B', () => {
expect(mOwners('numberOverloadOrNull')).toEqual([expect.stringContaining('B.m')]);
});
it("parse('x') beside null selects parse(s: string): A", () => {
expect(mOwners('nullOrStringOverload')).toEqual([expect.stringContaining('A.m')]);
});
it('a let parse(1) beside null never links the first overload A.m', () => {
expect(mOwners('mutableNumberOverloadOrNull').some((id) => id.includes('A.m'))).toBe(false);
});
it.each(['annotatedNumberOrNull', 'nullOrAnnotatedNumber'])(
'%s with an explicit type never binds to the first overload A.m',
(caller) => {
expect(mOwners(caller)).not.toContainEqual(expect.stringContaining('A.m'));
},
);
});