From 75f360ac323b53d9254ca07cd3fd2011017f114f Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sat, 20 Jul 2024 01:39:31 +0800 Subject: [PATCH] fix(typescript): synchronize script snapshots in a side-effect-free manner --- .../createAsyncLanguageServicePlugin.ts | 17 ++++------------- .../quickstart/createLanguageServicePlugin.ts | 11 +---------- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/packages/typescript/lib/quickstart/createAsyncLanguageServicePlugin.ts b/packages/typescript/lib/quickstart/createAsyncLanguageServicePlugin.ts index 5583f86c..aed90e0d 100644 --- a/packages/typescript/lib/quickstart/createAsyncLanguageServicePlugin.ts +++ b/packages/typescript/lib/quickstart/createAsyncLanguageServicePlugin.ts @@ -75,7 +75,6 @@ export function createAsyncLanguageServicePlugin( info.languageService = proxy; create(ts, info).then(({ languagePlugins, setup }) => { - const syncedScriptVersions = new FileMap(ts.sys.useCaseSensitiveFileNames); const language = createLanguage( [ ...languagePlugins, @@ -83,19 +82,11 @@ export function createAsyncLanguageServicePlugin( ], new FileMap(ts.sys.useCaseSensitiveFileNames), fileName => { - const version = getScriptVersion(fileName); - if (syncedScriptVersions.get(fileName) === version) { - return; - } - syncedScriptVersions.set(fileName, version); - - const snapshot = getScriptSnapshot(fileName); + const snapshot = info.project.getScriptInfo(fileName)?.getSnapshot(); if (snapshot) { - language.scripts.set( - fileName, - snapshot - ); - } else { + language.scripts.set(fileName, snapshot); + } + else { language.scripts.delete(fileName); } } diff --git a/packages/typescript/lib/quickstart/createLanguageServicePlugin.ts b/packages/typescript/lib/quickstart/createLanguageServicePlugin.ts index a12a5ca8..c13f95c9 100644 --- a/packages/typescript/lib/quickstart/createLanguageServicePlugin.ts +++ b/packages/typescript/lib/quickstart/createLanguageServicePlugin.ts @@ -34,9 +34,6 @@ export function createLanguageServicePlugin( .map(plugin => plugin.typescript?.extraFileExtensions.map(ext => '.' + ext.extension) ?? []) .flat(); projectExternalFileExtensions.set(info.project, extensions); - const getScriptSnapshot = info.languageServiceHost.getScriptSnapshot.bind(info.languageServiceHost); - const getScriptVersion = info.languageServiceHost.getScriptVersion.bind(info.languageServiceHost); - const syncedScriptVersions = new FileMap(ts.sys.useCaseSensitiveFileNames); const language = createLanguage( [ ...languagePlugins, @@ -44,13 +41,7 @@ export function createLanguageServicePlugin( ], new FileMap(ts.sys.useCaseSensitiveFileNames), fileName => { - const version = getScriptVersion(fileName); - if (syncedScriptVersions.get(fileName) === version) { - return; - } - syncedScriptVersions.set(fileName, version); - - const snapshot = getScriptSnapshot(fileName); + const snapshot = info.project.getScriptInfo(fileName)?.getSnapshot(); if (snapshot) { language.scripts.set(fileName, snapshot); }