Skip to content

Commit 0f813a3

Browse files
authored
(fix) Kit fixes (#1931)
- hopefully fix issue with linux (#1930) - check return type of api methods
1 parent 0e97944 commit 0f813a3

File tree

2 files changed

+155
-171
lines changed

2 files changed

+155
-171
lines changed

packages/svelte2tsx/src/helpers/sveltekit.ts

Lines changed: 79 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -167,55 +167,21 @@ function upserKitRouteFile(
167167
insert(pos, inserted);
168168
}
169169

170-
// add type to prerender variable if not explicitly typed
171-
const prerender = exports.get('prerender');
172-
if (prerender?.type === 'var' && !prerender.hasTypeDefinition && prerender.node.initializer) {
173-
const pos = prerender.node.name.getEnd();
174-
const inserted = surround(` : boolean | 'auto'`);
175-
insert(pos, inserted);
176-
}
177-
178-
// add type to trailingSlash variable if not explicitly typed
179-
const trailingSlash = exports.get('trailingSlash');
180-
if (
181-
trailingSlash?.type === 'var' &&
182-
!trailingSlash.hasTypeDefinition &&
183-
trailingSlash.node.initializer
184-
) {
185-
const pos = trailingSlash.node.name.getEnd();
186-
const inserted = surround(` : 'never' | 'always' | 'ignore'`);
187-
insert(pos, inserted);
188-
}
189-
190-
// add type to ssr variable if not explicitly typed
191-
const ssr = exports.get('ssr');
192-
if (ssr?.type === 'var' && !ssr.hasTypeDefinition && ssr.node.initializer) {
193-
const pos = ssr.node.name.getEnd();
194-
const inserted = surround(` : boolean`);
195-
insert(pos, inserted);
196-
}
197-
198-
// add type to csr variable if not explicitly typed
199-
const csr = exports.get('csr');
200-
if (csr?.type === 'var' && !csr.hasTypeDefinition && csr.node.initializer) {
201-
const pos = csr.node.name.getEnd();
202-
const inserted = surround(` : boolean`);
203-
insert(pos, inserted);
204-
}
170+
addTypeToVariable(exports, surround, insert, 'prerender', `boolean | 'auto'`);
171+
addTypeToVariable(exports, surround, insert, 'trailingSlash', `'never' | 'always' | 'ignore'`);
172+
addTypeToVariable(exports, surround, insert, 'ssr', `boolean`);
173+
addTypeToVariable(exports, surround, insert, 'csr', `boolean`);
205174

206175
// add types to GET/PUT/POST/PATCH/DELETE/OPTIONS if not explicitly typed
207176
const insertApiMethod = (name: string) => {
208-
const api = exports.get(name);
209-
if (
210-
api?.type === 'function' &&
211-
api.node.parameters.length === 1 &&
212-
!api.hasTypeDefinition
213-
) {
214-
const pos = api.node.parameters[0].getEnd();
215-
const inserted = surround(`: import('./$types').RequestEvent`);
216-
217-
insert(pos, inserted);
218-
}
177+
addTypeToFunction(
178+
exports,
179+
surround,
180+
insert,
181+
name,
182+
`import('./$types').RequestEvent`,
183+
`Response | Promise<Response>`
184+
);
219185
};
220186
insertApiMethod('GET');
221187
insertApiMethod('PUT');
@@ -249,22 +215,7 @@ function upserKitParamsFile(
249215
const isTsFile = basename.endsWith('.ts');
250216
const exports = findExports(source, isTsFile);
251217

252-
// add type to match function if not explicitly typed
253-
const match = exports.get('match');
254-
if (
255-
match?.type === 'function' &&
256-
match.node.parameters.length === 1 &&
257-
!match.hasTypeDefinition
258-
) {
259-
const pos = match.node.parameters[0].getEnd();
260-
const inserted = surround(`: string`);
261-
insert(pos, inserted);
262-
if (!match.node.type && match.node.body) {
263-
const returnPos = match.node.body.getStart();
264-
const returnInsertion = surround(`: boolean`);
265-
insert(returnPos, returnInsertion);
266-
}
267-
}
218+
addTypeToFunction(exports, surround, insert, 'match', 'string', 'boolean');
268219

269220
return { addedCode, originalText: source.getFullText() };
270221
}
@@ -291,26 +242,13 @@ function upserKitClientHooksFile(
291242
const isTsFile = basename.endsWith('.ts');
292243
const exports = findExports(source, isTsFile);
293244

294-
// add type to handleError function if not explicitly typed
295-
const handleError = exports.get('handleError');
296-
if (
297-
handleError?.type === 'function' &&
298-
handleError.node.parameters.length === 1 &&
299-
!handleError.hasTypeDefinition
300-
) {
301-
const paramPos = handleError.node.parameters[0].getEnd();
302-
const paramInsertion = surround(
303-
`: Parameters<import('@sveltejs/kit').HandleClientError>[0]`
304-
);
305-
insert(paramPos, paramInsertion);
306-
if (!handleError.node.type && handleError.node.body) {
307-
const returnPos = handleError.node.body.getStart();
308-
const returnInsertion = surround(
309-
`: ReturnType<import('@sveltejs/kit').HandleClientError>`
310-
);
311-
insert(returnPos, returnInsertion);
312-
}
313-
}
245+
addTypeToFunction(
246+
exports,
247+
surround,
248+
insert,
249+
'handleError',
250+
`import('@sveltejs/kit').HandleClientError`
251+
);
314252

315253
return { addedCode, originalText: source.getFullText() };
316254
}
@@ -337,27 +275,71 @@ function upserKitServerHooksFile(
337275
const isTsFile = basename.endsWith('.ts');
338276
const exports = findExports(source, isTsFile);
339277

340-
const addTypeToFunction = (name: string, type: string) => {
341-
const fn = exports.get(name);
342-
if (fn?.type === 'function' && fn.node.parameters.length === 1 && !fn.hasTypeDefinition) {
343-
const paramPos = fn.node.parameters[0].getEnd();
344-
const paramInsertion = surround(`: Parameters<${type}>[0]`);
345-
insert(paramPos, paramInsertion);
346-
if (!fn.node.type && fn.node.body) {
347-
const returnPos = fn.node.body.getStart();
348-
const returnInsertion = surround(`: ReturnType<${type}>`);
349-
insert(returnPos, returnInsertion);
350-
}
351-
}
278+
const addType = (name: string, type: string) => {
279+
addTypeToFunction(exports, surround, insert, name, type);
352280
};
353281

354-
addTypeToFunction('handleError', `import('@sveltejs/kit').HandleServerError`);
355-
addTypeToFunction('handle', `import('@sveltejs/kit').Handle`);
356-
addTypeToFunction('handleFetch', `import('@sveltejs/kit').HandleFetch`);
282+
addType('handleError', `import('@sveltejs/kit').HandleServerError`);
283+
addType('handle', `import('@sveltejs/kit').Handle`);
284+
addType('handleFetch', `import('@sveltejs/kit').HandleFetch`);
357285

358286
return { addedCode, originalText: source.getFullText() };
359287
}
360288

289+
function addTypeToVariable(
290+
exports: Map<
291+
string,
292+
| {
293+
type: 'function';
294+
node: ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression;
295+
hasTypeDefinition: boolean;
296+
}
297+
| { type: 'var'; node: ts.VariableDeclaration; hasTypeDefinition: boolean }
298+
>,
299+
surround: (text: string) => string,
300+
insert: (pos: number, inserted: string) => void,
301+
name: string,
302+
type: string
303+
) {
304+
const variable = exports.get(name);
305+
if (variable?.type === 'var' && !variable.hasTypeDefinition && variable.node.initializer) {
306+
const pos = variable.node.name.getEnd();
307+
const inserted = surround(` : ${type}`);
308+
insert(pos, inserted);
309+
}
310+
}
311+
312+
function addTypeToFunction(
313+
exports: Map<
314+
string,
315+
| {
316+
type: 'function';
317+
node: ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression;
318+
hasTypeDefinition: boolean;
319+
}
320+
| { type: 'var'; node: ts.VariableDeclaration; hasTypeDefinition: boolean }
321+
>,
322+
surround: (text: string) => string,
323+
insert: (pos: number, inserted: string) => void,
324+
name: string,
325+
type: string,
326+
returnType?: string
327+
) {
328+
const fn = exports.get(name);
329+
if (fn?.type === 'function' && fn.node.parameters.length === 1 && !fn.hasTypeDefinition) {
330+
const paramPos = fn.node.parameters[0].getEnd();
331+
const paramInsertion = surround(!returnType ? `: Parameters<${type}>[0]` : `: ${type}`);
332+
insert(paramPos, paramInsertion);
333+
if (!fn.node.type && fn.node.body) {
334+
const returnPos = fn.node.body.getStart();
335+
const returnInsertion = surround(
336+
!returnType ? `: ReturnType<${type}>` : `: ${returnType}`
337+
);
338+
insert(returnPos, returnInsertion);
339+
}
340+
}
341+
}
342+
361343
function insertCode(addedCode: AddedCode[], pos: number, inserted: string) {
362344
const insertionIdx = addedCode.findIndex((c) => c.originalPos > pos);
363345
if (insertionIdx >= 0) {

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

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -650,80 +650,82 @@ function createProxyRegistry(
650650
) {
651651
// Don't destructure options param, as the value may be mutated through a svelte.config.js later
652652
const registry = ts.createDocumentRegistry();
653-
const originalRegistry = (originalLanguageServiceHost as any).documentRegistry;
654-
const proxyRegistry: ts.DocumentRegistry = {
655-
...originalRegistry,
656-
acquireDocumentWithKey(
657-
fileName,
658-
tsPath,
659-
compilationSettingsOrHost,
660-
key,
661-
scriptSnapshot,
662-
version,
663-
scriptKind,
664-
sourceFileOptions
665-
) {
666-
if (internalHelpers.isKitFile(fileName, options)) {
667-
return registry.acquireDocumentWithKey(
668-
fileName,
669-
tsPath,
670-
compilationSettingsOrHost,
671-
key,
672-
scriptSnapshot,
673-
version,
674-
scriptKind,
675-
sourceFileOptions
676-
);
677-
}
678-
679-
return originalRegistry.acquireDocumentWithKey(
680-
fileName,
681-
tsPath,
682-
compilationSettingsOrHost,
683-
key,
684-
scriptSnapshot,
685-
version,
686-
scriptKind,
687-
sourceFileOptions
688-
);
689-
},
690-
updateDocumentWithKey(
691-
fileName,
692-
tsPath,
693-
compilationSettingsOrHost,
694-
key,
695-
scriptSnapshot,
696-
version,
697-
scriptKind,
698-
sourceFileOptions
699-
) {
700-
if (internalHelpers.isKitFile(fileName, options)) {
701-
return registry.updateDocumentWithKey(
702-
fileName,
703-
tsPath,
704-
compilationSettingsOrHost,
705-
key,
706-
scriptSnapshot,
707-
version,
708-
scriptKind,
709-
sourceFileOptions
710-
);
711-
}
712-
713-
return originalRegistry.updateDocumentWithKey(
714-
fileName,
715-
tsPath,
716-
compilationSettingsOrHost,
717-
key,
718-
scriptSnapshot,
719-
version,
720-
scriptKind,
721-
sourceFileOptions
722-
);
723-
}
724-
};
725-
726-
return proxyRegistry;
653+
return registry;
654+
// TODO check why this fails on linux and reenable later
655+
// const originalRegistry = (originalLanguageServiceHost as any).documentRegistry;
656+
// const proxyRegistry: ts.DocumentRegistry = {
657+
// ...originalRegistry,
658+
// acquireDocumentWithKey(
659+
// fileName,
660+
// tsPath,
661+
// compilationSettingsOrHost,
662+
// key,
663+
// scriptSnapshot,
664+
// version,
665+
// scriptKind,
666+
// sourceFileOptions
667+
// ) {
668+
// if (internalHelpers.isKitFile(fileName, options)) {
669+
// return registry.acquireDocumentWithKey(
670+
// fileName,
671+
// tsPath,
672+
// compilationSettingsOrHost,
673+
// key,
674+
// scriptSnapshot,
675+
// version,
676+
// scriptKind,
677+
// sourceFileOptions
678+
// );
679+
// }
680+
681+
// return originalRegistry.acquireDocumentWithKey(
682+
// fileName,
683+
// tsPath,
684+
// compilationSettingsOrHost,
685+
// key,
686+
// scriptSnapshot,
687+
// version,
688+
// scriptKind,
689+
// sourceFileOptions
690+
// );
691+
// },
692+
// updateDocumentWithKey(
693+
// fileName,
694+
// tsPath,
695+
// compilationSettingsOrHost,
696+
// key,
697+
// scriptSnapshot,
698+
// version,
699+
// scriptKind,
700+
// sourceFileOptions
701+
// ) {
702+
// if (internalHelpers.isKitFile(fileName, options)) {
703+
// return registry.updateDocumentWithKey(
704+
// fileName,
705+
// tsPath,
706+
// compilationSettingsOrHost,
707+
// key,
708+
// scriptSnapshot,
709+
// version,
710+
// scriptKind,
711+
// sourceFileOptions
712+
// );
713+
// }
714+
715+
// return originalRegistry.updateDocumentWithKey(
716+
// fileName,
717+
// tsPath,
718+
// compilationSettingsOrHost,
719+
// key,
720+
// scriptSnapshot,
721+
// version,
722+
// scriptKind,
723+
// sourceFileOptions
724+
// );
725+
// }
726+
// };
727+
728+
// return proxyRegistry;
727729
}
728730

729731
export function getVirtualLS(

0 commit comments

Comments
 (0)