Skip to content

Commit

Permalink
fix: prevent completion using empty cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Jan 13, 2024
1 parent 57a548c commit aae88df
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,10 @@ async function createLanguageService(

dirty = false;

if (!oldProgram) {
// host.getCachedExportInfoMap will create the cache if it doesn't exist
// so we need to check the property instead
const cache = project?.exportMapCache;
if (!oldProgram || !cache || cache.isEmpty()) {
changedFilesForExportCache.clear();
return;
}
Expand All @@ -734,10 +737,10 @@ async function createLanguageService(
}

if (oldFile && newFile) {
host.getCachedExportInfoMap?.().onFileChanged?.(oldFile, newFile, false);
cache.onFileChanged?.(oldFile, newFile, false);
} else {
// new file or deleted file
host.getCachedExportInfoMap?.().clear();
cache.clear();
}
}
changedFilesForExportCache.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,55 @@ describe('CompletionProviderImpl', function () {
assert.strictEqual(detail, 'Add import from "./Bar.svelte"\n\nclass Bar');
});

it("doesn't use empty cache", async () => {
const virtualTestDir = getRandomVirtualDirPath(testFilesDir);
const { document, lsAndTsDocResolver, lsConfigManager, docManager } =
setupVirtualEnvironment({
filename: 'index.svelte',
fileContent: '<script>b</script>',
testDir: virtualTestDir
});

const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, lsConfigManager);

await lsAndTsDocResolver.getLSAndTSDoc(document);

docManager.updateDocument(document, [
{
range: Range.create(
Position.create(0, document.content.length),
Position.create(0, document.content.length)
),
text: ' '
}
]);

docManager.openClientDocument({
text: '',
uri: pathToUrl(join(virtualTestDir, 'Bar.svelte'))
});

docManager.updateDocument(document, [
{
range: Range.create(
Position.create(0, document.content.length),
Position.create(0, document.content.length)
),
text: ' '
}
]);

const completions = await completionProvider.getCompletions(document, {
line: 0,
character: 9
});

const item2 = completions?.items.find((item) => item.label === 'Bar');
const { detail } = await completionProvider.resolveCompletion(document, item2!);

assert.strictEqual(detail, 'Add import from "./Bar.svelte"\n\nclass Bar');
});

it('can auto import new export', async () => {
const virtualTestDir = getRandomVirtualDirPath(testFilesDir);
const { document, lsAndTsDocResolver, lsConfigManager, virtualSystem } =
Expand Down

0 comments on commit aae88df

Please sign in to comment.