A function nested inside another function is only callable from inside its
container (#1230). matchByExactName already declined such candidates; the
fuzzy fallback did not, so a builtin method call (`res.text()`) whose only
same-named project symbol was some file's closure resolved onto that
closure at 0.5 (#1708).
and on vitejs/vite@8492422 that traded 12 correct removals for 59 wrong
additions: the repo has a dozen `resolve` definitions, most nested, so the
filter left exactly one reachable `resolve` method and the strategy
committed every `import { resolve } from 'node:path'` call in the
playground configs to it. Filtering a crowd down to one survivor is not
evidence the survivor was ever the target.
So the check sits on the ONE candidate matchFuzzy would commit to: a
unique candidate the call cannot reach is declined; a crowd stays a crowd.
Same tree, measured against this branch's own base b9ca4b7: 12 edges lost
(all fuzzy, all onto nested functions — the same 12 #1709 removes), 0
gained, fuzzy 13 -> 1, every other resolvedBy row at zero.
The two-file fixture is #1709's, credited in the previous commit; four
direct tests pin the shape: a lone unreachable closure declines, the same
closure resolves from inside its container, closure + method is ambiguous
and declines (the candidate-set filter fails exactly this one), a lone
reachable method resolves as before.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
145 lines
5.9 KiB
TypeScript
145 lines
5.9 KiB
TypeScript
/**
|
|
* A function nested inside another function is only callable from inside its
|
|
* container. matchByExactName already filters candidates that way; matchFuzzy
|
|
* must too, or a call to a builtin method (`res.text()`) whose only same-named
|
|
* project symbol is some file's closure resolves onto that closure.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as os from 'os';
|
|
import { CodeGraph } from '../src';
|
|
import { matchFuzzy } from '../src/resolution/name-matcher';
|
|
import type { Node } from '../src/types';
|
|
import type { ResolutionContext, UnresolvedRef } from '../src/resolution/types';
|
|
|
|
describe('fuzzy matching respects lexical reachability of nested functions', () => {
|
|
let tempDir: string;
|
|
let cg: CodeGraph | null = null;
|
|
|
|
beforeEach(() => {
|
|
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-fuzzy-reach-'));
|
|
});
|
|
|
|
afterEach(() => {
|
|
cg?.destroy();
|
|
cg = null;
|
|
try {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
} catch {
|
|
// Windows can still hold the SQLite handle for a moment; the OS temp dir is swept anyway.
|
|
}
|
|
});
|
|
|
|
it('does not resolve a builtin method call onto another file\'s closure of the same name', async () => {
|
|
fs.writeFileSync(
|
|
path.join(tempDir, 'seed.ts'),
|
|
[
|
|
'export function readSeedState(raw: string): string {',
|
|
' function text(): string {',
|
|
' return raw.trim();',
|
|
' }',
|
|
' return text();',
|
|
'}',
|
|
'',
|
|
].join('\n')
|
|
);
|
|
fs.writeFileSync(
|
|
path.join(tempDir, 'fetch.ts'),
|
|
[
|
|
'export async function readOkText(settled: { value: Response }): Promise<string> {',
|
|
' // A chained receiver reaches the resolver as the bare method name.',
|
|
' return settled.value.text();',
|
|
'}',
|
|
'',
|
|
].join('\n')
|
|
);
|
|
cg = await CodeGraph.init(tempDir, { index: true });
|
|
cg.resolveReferences();
|
|
|
|
const closure = cg
|
|
.getNodesByKind('function')
|
|
.find((n) => n.name === 'text' && n.filePath === 'seed.ts');
|
|
const caller = cg.getNodesByKind('function').find((n) => n.name === 'readOkText');
|
|
expect(closure).toBeDefined();
|
|
expect(caller).toBeDefined();
|
|
|
|
const fromCaller = cg.getOutgoingEdges(caller!.id).filter((e) => e.kind === 'calls');
|
|
expect(fromCaller.map((e) => e.target)).not.toContain(closure!.id);
|
|
|
|
// The in-container call still resolves.
|
|
const container = cg.getNodesByKind('function').find((n) => n.name === 'readSeedState');
|
|
const inside = cg.getOutgoingEdges(container!.id).filter((e) => e.kind === 'calls');
|
|
expect(inside.map((e) => e.target)).toContain(closure!.id);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* The reachability check must sit on the one candidate matchFuzzy would
|
|
* commit to, never on the candidate set. Filtering a crowd of same-named
|
|
* definitions down to the reachable ones leaves a single survivor, and the
|
|
* strategy then hands it every call of that name: vite has a dozen `resolve`
|
|
* definitions, most nested, and one reachable `resolve` method inherited 59
|
|
* `import { resolve } from 'node:path'` calls that way (#1709). Driven
|
|
* directly, so the shape is pinned regardless of what the earlier strategies
|
|
* make of a given fixture.
|
|
*/
|
|
describe('fuzzy reachability rejects a unique guess but never manufactures one', () => {
|
|
const node = (partial: Partial<Node> & Pick<Node, 'id' | 'kind' | 'name' | 'filePath'>): Node => ({
|
|
qualifiedName: partial.name,
|
|
language: 'typescript',
|
|
startLine: 1,
|
|
endLine: 1,
|
|
startColumn: 0,
|
|
endColumn: 0,
|
|
updatedAt: 0,
|
|
...partial,
|
|
});
|
|
// build.ts: function build() { const resolve = …; function resolve() {} }
|
|
const container = node({ id: 'f:build', kind: 'function', name: 'build', filePath: 'build.ts', startLine: 1, endLine: 40 });
|
|
const closure = node({ id: 'f:build.resolve', kind: 'function', name: 'resolve', qualifiedName: 'build::resolve', filePath: 'build.ts', startLine: 10, endLine: 12 });
|
|
// pluginContainer.ts: class PluginContainer { resolve() {} }
|
|
const method = node({ id: 'm:resolve', kind: 'method', name: 'resolve', qualifiedName: 'PluginContainer::resolve', filePath: 'pluginContainer.ts', startLine: 5, endLine: 9 });
|
|
const contextWith = (nodes: Node[]): ResolutionContext =>
|
|
({
|
|
getNodesInFile: () => [],
|
|
getNodesByName: (name: string) => nodes.filter((n) => n.name === name),
|
|
getNodesByLowerName: (name: string) => nodes.filter((n) => n.name.toLowerCase() === name),
|
|
getNodesByQualifiedName: (qn: string) => [container].filter((n) => n.qualifiedName === qn),
|
|
getNodesByKind: () => [],
|
|
fileExists: () => false,
|
|
readFile: () => null,
|
|
getFileLines: () => [],
|
|
getProjectRoot: () => '',
|
|
getAllFiles: () => [],
|
|
getImportMappings: () => [],
|
|
}) as unknown as ResolutionContext;
|
|
const callFrom = (filePath: string, line: number): UnresolvedRef => ({
|
|
fromNodeId: 'f:caller',
|
|
referenceName: 'resolve',
|
|
referenceKind: 'calls',
|
|
line,
|
|
column: 2,
|
|
filePath,
|
|
language: 'typescript',
|
|
});
|
|
|
|
it('declines the sole candidate when it is a closure the call cannot reach', () => {
|
|
expect(matchFuzzy(callFrom('vite.config.js', 3), contextWith([closure]))).toBeNull();
|
|
});
|
|
|
|
it('still resolves the sole candidate from inside its container', () => {
|
|
expect(matchFuzzy(callFrom('build.ts', 20), contextWith([closure]))?.targetNodeId).toBe('f:build.resolve');
|
|
});
|
|
|
|
it('does not let the unreachable closure drop out and leave the method as a "unique" match', () => {
|
|
// Two same-named callables: ambiguous, exactly as before the check existed.
|
|
expect(matchFuzzy(callFrom('vite.config.js', 3), contextWith([closure, method]))).toBeNull();
|
|
});
|
|
|
|
it('resolves a lone reachable method as before', () => {
|
|
expect(matchFuzzy(callFrom('vite.config.js', 3), contextWith([method]))?.targetNodeId).toBe('m:resolve');
|
|
});
|
|
});
|