Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure SvelteKit file is patched when first open #2160

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions packages/typescript-plugin/src/language-service/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
};
Expand Down