Skip to content

Commit 5c08ff6

Browse files
authored
feat: zero effort typings for reroute (#2252)
Related to sveltejs/kit#11537 Also turns the error on unknown exports into a warning - people could be stuck on old versions of language tools while new features are added, and SvelteKit will throw a dev time error anyway. Also commented out that never worked.
1 parent f5ec504 commit 5c08ff6

File tree

9 files changed

+126
-87
lines changed

9 files changed

+126
-87
lines changed

packages/language-server/src/plugins/typescript/DocumentSnapshot.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ export class JSOrTSDocumentSnapshot extends IdentityMapper implements DocumentSn
443443
private paramsPath = 'src/params';
444444
private serverHooksPath = 'src/hooks.server';
445445
private clientHooksPath = 'src/hooks.client';
446+
private universalHooksPath = 'src/hooks';
446447

447448
private openedByClient = false;
448449

@@ -578,7 +579,8 @@ export class JSOrTSDocumentSnapshot extends IdentityMapper implements DocumentSn
578579
{
579580
clientHooksPath: this.clientHooksPath,
580581
paramsPath: this.paramsPath,
581-
serverHooksPath: this.serverHooksPath
582+
serverHooksPath: this.serverHooksPath,
583+
universalHooksPath: this.universalHooksPath
582584
},
583585
() => this.createSource(),
584586
surroundWithIgnoreComments
@@ -596,6 +598,7 @@ export class JSOrTSDocumentSnapshot extends IdentityMapper implements DocumentSn
596598
this.paramsPath ||= files.params;
597599
this.serverHooksPath ||= files.hooks?.server;
598600
this.clientHooksPath ||= files.hooks?.client;
601+
this.universalHooksPath ||= files.hooks?.universal;
599602
}
600603
}
601604

packages/svelte2tsx/index.d.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,11 @@ export const internalHelpers: {
135135
options: InternalHelpers.KitFilesSettings
136136
) => boolean;
137137
isKitRouteFile: (basename: string) => boolean,
138-
isClientHooksFile: (
138+
isHooksFile: (
139139
fileName: string,
140140
basename: string,
141-
clientHooksPath: string
142-
) =>boolean,
143-
isServerHooksFile: (
144-
fileName: string,
145-
basename: string,
146-
serverHooksPath: string
147-
)=> boolean,
141+
hooksPath: string
142+
) => boolean,
148143
isParamsFile: (fileName: string, basename: string, paramsPath: string) =>boolean,
149144
upsertKitFile: (
150145
_ts: typeof ts,
@@ -185,6 +180,7 @@ export namespace InternalHelpers {
185180
export interface KitFilesSettings {
186181
serverHooksPath: string;
187182
clientHooksPath: string;
183+
universalHooksPath: string;
188184
paramsPath: string;
189185
}
190186
}

packages/svelte2tsx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte2tsx",
3-
"version": "0.6.8",
3+
"version": "0.7.0",
44
"description": "Convert Svelte components to TSX for type checking",
55
"author": "David Pershouse",
66
"license": "MIT",

packages/svelte2tsx/src/helpers/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {
2-
isClientHooksFile,
2+
isHooksFile,
33
isKitFile,
44
isKitRouteFile,
55
isParamsFile,
6-
isServerHooksFile,
76
toOriginalPos,
87
toVirtualPos,
98
upsertKitFile
@@ -19,8 +18,7 @@ import { findExports } from './typescript';
1918
export const internalHelpers = {
2019
isKitFile,
2120
isKitRouteFile,
22-
isClientHooksFile,
23-
isServerHooksFile,
21+
isHooksFile,
2422
isParamsFile,
2523
upsertKitFile,
2624
toVirtualPos,

packages/svelte2tsx/src/helpers/sveltekit.ts

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface AddedCode {
1515
export interface KitFilesSettings {
1616
serverHooksPath: string;
1717
clientHooksPath: string;
18+
universalHooksPath: string;
1819
paramsPath: string;
1920
}
2021

@@ -27,8 +28,9 @@ export function isKitFile(fileName: string, options: KitFilesSettings): boolean
2728
const basename = path.basename(fileName);
2829
return (
2930
isKitRouteFile(basename) ||
30-
isServerHooksFile(fileName, basename, options.serverHooksPath) ||
31-
isClientHooksFile(fileName, basename, options.clientHooksPath) ||
31+
isHooksFile(fileName, basename, options.serverHooksPath) ||
32+
isHooksFile(fileName, basename, options.clientHooksPath) ||
33+
isHooksFile(fileName, basename, options.universalHooksPath) ||
3234
isParamsFile(fileName, basename, options.paramsPath)
3335
);
3436
}
@@ -50,30 +52,11 @@ export function isKitRouteFile(basename: string): boolean {
5052
/**
5153
* Determines whether or not a given file is a SvelteKit-specific hooks file
5254
*/
53-
export function isServerHooksFile(
54-
fileName: string,
55-
basename: string,
56-
serverHooksPath: string
57-
): boolean {
55+
export function isHooksFile(fileName: string, basename: string, hooksPath: string): boolean {
5856
return (
5957
((basename === 'index.ts' || basename === 'index.js') &&
60-
fileName.slice(0, -basename.length - 1).endsWith(serverHooksPath)) ||
61-
fileName.slice(0, -path.extname(basename).length).endsWith(serverHooksPath)
62-
);
63-
}
64-
65-
/**
66-
* Determines whether or not a given file is a SvelteKit-specific hooks file
67-
*/
68-
export function isClientHooksFile(
69-
fileName: string,
70-
basename: string,
71-
clientHooksPath: string
72-
): boolean {
73-
return (
74-
((basename === 'index.ts' || basename === 'index.js') &&
75-
fileName.slice(0, -basename.length - 1).endsWith(clientHooksPath)) ||
76-
fileName.slice(0, -path.extname(basename).length).endsWith(clientHooksPath)
58+
fileName.slice(0, -basename.length - 1).endsWith(hooksPath)) ||
59+
fileName.slice(0, -path.extname(basename).length).endsWith(hooksPath)
7760
);
7861
}
7962

@@ -97,24 +80,32 @@ export function upsertKitFile(
9780
): { text: string; addedCode: AddedCode[] } {
9881
let basename = path.basename(fileName);
9982
const result =
100-
upserKitRouteFile(ts, basename, getSource, surround) ??
101-
upserKitServerHooksFile(
83+
upsertKitRouteFile(ts, basename, getSource, surround) ??
84+
upsertKitServerHooksFile(
10285
ts,
10386
fileName,
10487
basename,
10588
kitFilesSettings.serverHooksPath,
10689
getSource,
10790
surround
10891
) ??
109-
upserKitClientHooksFile(
92+
upsertKitClientHooksFile(
11093
ts,
11194
fileName,
11295
basename,
11396
kitFilesSettings.clientHooksPath,
11497
getSource,
11598
surround
11699
) ??
117-
upserKitParamsFile(
100+
upsertKitUniversalHooksFile(
101+
ts,
102+
fileName,
103+
basename,
104+
kitFilesSettings.universalHooksPath,
105+
getSource,
106+
surround
107+
) ??
108+
upsertKitParamsFile(
118109
ts,
119110
fileName,
120111
basename,
@@ -139,7 +130,7 @@ export function upsertKitFile(
139130
return { text, addedCode };
140131
}
141132

142-
function upserKitRouteFile(
133+
function upsertKitRouteFile(
143134
ts: _ts,
144135
basename: string,
145136
getSource: () => ts.SourceFile | undefined,
@@ -225,7 +216,7 @@ function upserKitRouteFile(
225216
return { addedCode, originalText: source.getFullText() };
226217
}
227218

228-
function upserKitParamsFile(
219+
function upsertKitParamsFile(
229220
ts: _ts,
230221
fileName: string,
231222
basename: string,
@@ -253,15 +244,15 @@ function upserKitParamsFile(
253244
return { addedCode, originalText: source.getFullText() };
254245
}
255246

256-
function upserKitClientHooksFile(
247+
function upsertKitClientHooksFile(
257248
ts: _ts,
258249
fileName: string,
259250
basename: string,
260251
clientHooksPath: string,
261252
getSource: () => ts.SourceFile | undefined,
262253
surround: (text: string) => string
263254
) {
264-
if (!isClientHooksFile(fileName, basename, clientHooksPath)) {
255+
if (!isHooksFile(fileName, basename, clientHooksPath)) {
265256
return;
266257
}
267258

@@ -288,15 +279,15 @@ function upserKitClientHooksFile(
288279
return { addedCode, originalText: source.getFullText() };
289280
}
290281

291-
function upserKitServerHooksFile(
282+
function upsertKitServerHooksFile(
292283
ts: _ts,
293284
fileName: string,
294285
basename: string,
295286
serverHooksPath: string,
296287
getSource: () => ts.SourceFile | undefined,
297288
surround: (text: string) => string
298289
) {
299-
if (!isServerHooksFile(fileName, basename, serverHooksPath)) {
290+
if (!isHooksFile(fileName, basename, serverHooksPath)) {
300291
return;
301292
}
302293

@@ -322,6 +313,34 @@ function upserKitServerHooksFile(
322313
return { addedCode, originalText: source.getFullText() };
323314
}
324315

316+
function upsertKitUniversalHooksFile(
317+
ts: _ts,
318+
fileName: string,
319+
basename: string,
320+
universalHooksPath: string,
321+
getSource: () => ts.SourceFile | undefined,
322+
surround: (text: string) => string
323+
) {
324+
if (!isHooksFile(fileName, basename, universalHooksPath)) {
325+
return;
326+
}
327+
328+
const source = getSource();
329+
if (!source) return;
330+
331+
const addedCode: AddedCode[] = [];
332+
const insert = (pos: number, inserted: string) => {
333+
insertCode(addedCode, pos, inserted);
334+
};
335+
336+
const isTsFile = basename.endsWith('.ts');
337+
const exports = findExports(ts, source, isTsFile);
338+
339+
addTypeToFunction(ts, exports, surround, insert, 'reroute', `import('@sveltejs/kit').Reroute`);
340+
341+
return { addedCode, originalText: source.getFullText() };
342+
}
343+
325344
function addTypeToVariable(
326345
exports: Map<
327346
string,

packages/svelte2tsx/test/helpers/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('Internal Helpers - upsertKitFile', () => {
1111
{
1212
clientHooksPath: 'hooks.client',
1313
paramsPath: 'params',
14-
serverHooksPath: 'hooks.server'
14+
serverHooksPath: 'hooks.server',
15+
universalHooksPath: 'hooks'
1516
},
1617
() => sourceFile
1718
);

packages/typescript-plugin/src/language-service/diagnostics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ function getKitDiagnostics<
183183
messageText: `Invalid export '${exportName}' (valid exports are ${validExports.join(
184184
', '
185185
)}, or anything with a '_' prefix)`,
186-
category: ts.DiagnosticCategory.Error,
186+
// make it a warning in case people are stuck on old versions and new exports are added to SvelteKit
187+
category: ts.DiagnosticCategory.Warning,
187188
code: 71001 // arbitrary
188189
});
189190
}

packages/typescript-plugin/src/language-service/hover.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ export function decorateHover(
2626
const node = source && findNodeAtPosition(source, virtualPos);
2727
if (node && isTopLevelExport(ts, node, source) && ts.isIdentifier(node)) {
2828
const name = node.text;
29-
if (name in kitExports) {
30-
quickInfo.documentation = !quickInfo.documentation?.length
31-
? kitExports[name].documentation
32-
: quickInfo.documentation;
29+
if (name in kitExports && !quickInfo.documentation?.length) {
30+
quickInfo.documentation = kitExports[name].documentation;
3331
}
3432
}
3533

0 commit comments

Comments
 (0)