GitNexus/gitnexus/test/unit/scope-resolution/vue/template-component-imports.test.ts
Ritesh Das 01c90f073f
perf(vue): index template imports once per resolver pass (#4268)
Co-authored-by: Abhigyan Patwari <126312502+abhigyanpatwari@users.noreply.github.com>
2026-11-29 18:16:13 +00:00

80 lines
2.9 KiB
TypeScript

import { assert, expect, it } from 'vitest';
import { createEagerImportVisibility, type ImportEdge } from 'gitnexus-shared';
import { finalizeScopeModel } from '../../../../src/core/ingestion/finalize-orchestrator.js';
import { vueScopeResolver } from '../../../../src/core/ingestion/languages/vue/scope-resolver.js';
import { extractParsedFile } from '../../../../src/core/ingestion/scope-extractor-bridge.js';
import { buildTestGraph } from '../../../helpers/test-graph.js';
function buttonImport(targetFile: string | null): ImportEdge {
return {
kind: targetFile === null ? 'dynamic-unresolved' : 'alias',
localName: 'Button',
targetExportedName: 'default',
targetFile,
};
}
it.each([2, 8])(
'resolves template components in %i views with one pass over import scopes',
(count) => {
const views = Array.from({ length: count }, (_, i) => ({
view: `View${i}.vue`,
button: `Button${i}.vue`,
}));
const paths = ['Fallback.vue', ...views.flatMap(({ view, button }) => [view, button])];
const graph = buildTestGraph(
paths.map((filePath) => ({
id: `File:${filePath}`,
label: 'File',
name: filePath,
filePath,
})),
);
const source = '<script setup>function setupView() {}</script><template><Button /></template>';
const fileContents = new Map(views.map(({ view }) => [view, source]));
const importsByScope = new Map<string, ImportEdge[]>();
const files = views.map(({ view, button }) => {
const parsed = extractParsedFile(vueScopeResolver.languageProvider, source, view);
assert(parsed);
const inner = parsed.scopes.find((scope) => scope.kind === 'Function');
assert(inner);
// The last resolved binding for Button wins across scopes in this file.
importsByScope.set(parsed.moduleScope, [buttonImport('Fallback.vue')]);
importsByScope.set(inner.id, [
buttonImport('Fallback.vue'),
buttonImport(button),
buttonImport(null),
]);
return parsed;
});
const imports = createEagerImportVisibility(importsByScope);
let scopeVisits = 0;
const indexes = {
...finalizeScopeModel(files),
imports: {
...imports,
*scopes() {
for (const scope of imports.scopes()) {
scopeVisits++;
yield scope;
}
},
},
};
assert(vueScopeResolver.emitPostResolutionEdges);
vueScopeResolver.emitPostResolutionEdges(graph, files, new Map(), indexes, { fileContents });
expect([...graph.iterRelationships()]).toEqual(
views.map(({ view, button }) => ({
id: `CALLS:File:${view}:Button->File:${button}`,
sourceId: `File:${view}`,
targetId: `File:${button}`,
type: 'CALLS',
reason: 'vue-template-component',
confidence: 0.9,
})),
);
expect(scopeVisits).toBeLessThanOrEqual(importsByScope.size);
},
);