From 616da9235f9eebd9a8b7108aacf8ac4a4018c2be Mon Sep 17 00:00:00 2001 From: "Lyu, Wei Da" Date: Sun, 17 Sep 2023 13:48:33 +0800 Subject: [PATCH 1/2] fix: ensure SvelteKit file is patched when first open --- .../src/language-service/sveltekit.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/typescript-plugin/src/language-service/sveltekit.ts b/packages/typescript-plugin/src/language-service/sveltekit.ts index 8740cf63e..3fce696cd 100644 --- a/packages/typescript-plugin/src/language-service/sveltekit.ts +++ b/packages/typescript-plugin/src/language-service/sveltekit.ts @@ -481,6 +481,8 @@ export const kitExports: Record< } }; +const FORCE_UPDATE_VERSION = 'FORCE_UPDATE_VERSION'; + export function isKitRouteExportAllowedIn( basename: string, kitExport: (typeof kitExports)[keyof typeof kitExports] @@ -607,10 +609,11 @@ function getProxiedLanguageService(info: ts.server.PluginCreateInfo, ts: _ts, lo } getKitScriptSnapshotIfUpToDate(fileName: string) { + const scriptVersion = this.getScriptVersion(fileName); if ( !this.files[fileName] || - this.getScriptVersion(fileName) !== - originalLanguageServiceHost.getScriptVersion(fileName) + (scriptVersion !== originalLanguageServiceHost.getScriptVersion(fileName) && + scriptVersion !== FORCE_UPDATE_VERSION) ) { return undefined; } @@ -635,8 +638,16 @@ function getProxiedLanguageService(info: ts.server.PluginCreateInfo, ts: _ts, lo const { text, addedCode } = result; const snap = ts.ScriptSnapshot.fromString(text); snap.getChangeRange = (_) => undefined; + + // If this is a new file, typescript might have cached the unpatched version + // It won't update even if we return the patched version in getScriptSnapshot, so we force an update + // This should only happen to files that are opened by the client after the first compilation of the proxy language service + // and won't happen if there are any updates to the file afterwards this.files[fileName] = { - version: originalLanguageServiceHost.getScriptVersion(fileName), + version: + this.files[fileName] === undefined + ? FORCE_UPDATE_VERSION + : originalLanguageServiceHost.getScriptVersion(fileName), file: snap, addedCode }; From ee83ff2ce3ef3b22acdeda7c083a650f1399cd85 Mon Sep 17 00:00:00 2001 From: "Lyu, Wei Da" Date: Sun, 17 Sep 2023 13:56:36 +0800 Subject: [PATCH 2/2] format --- packages/typescript-plugin/src/language-service/sveltekit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescript-plugin/src/language-service/sveltekit.ts b/packages/typescript-plugin/src/language-service/sveltekit.ts index 3fce696cd..bfde70bf9 100644 --- a/packages/typescript-plugin/src/language-service/sveltekit.ts +++ b/packages/typescript-plugin/src/language-service/sveltekit.ts @@ -639,7 +639,7 @@ function getProxiedLanguageService(info: ts.server.PluginCreateInfo, ts: _ts, lo const snap = ts.ScriptSnapshot.fromString(text); snap.getChangeRange = (_) => undefined; - // If this is a new file, typescript might have cached the unpatched version + // If this is a new file, typescript might have cached the unpatched version // It won't update even if we return the patched version in getScriptSnapshot, so we force an update // This should only happen to files that are opened by the client after the first compilation of the proxy language service // and won't happen if there are any updates to the file afterwards