11import path from 'path' ;
2- import ts from 'typescript' ;
2+ import type ts from 'typescript' ;
33import { findExports } from './typescript' ;
44
5+ type _ts = typeof ts ;
6+
57export interface AddedCode {
68 generatedPos : number ;
79 originalPos : number ;
@@ -87,29 +89,39 @@ export function isParamsFile(fileName: string, basename: string, paramsPath: str
8789}
8890
8991export function upsertKitFile (
92+ ts : _ts ,
9093 fileName : string ,
9194 kitFilesSettings : KitFilesSettings ,
9295 getSource : ( ) => ts . SourceFile | undefined ,
9396 surround : ( text : string ) => string = ( text ) => text
9497) : { text : string ; addedCode : AddedCode [ ] } {
9598 let basename = path . basename ( fileName ) ;
9699 const result =
97- upserKitRouteFile ( fileName , basename , getSource , surround ) ??
100+ upserKitRouteFile ( ts , fileName , basename , getSource , surround ) ??
98101 upserKitServerHooksFile (
102+ ts ,
99103 fileName ,
100104 basename ,
101105 kitFilesSettings . serverHooksPath ,
102106 getSource ,
103107 surround
104108 ) ??
105109 upserKitClientHooksFile (
110+ ts ,
106111 fileName ,
107112 basename ,
108113 kitFilesSettings . clientHooksPath ,
109114 getSource ,
110115 surround
111116 ) ??
112- upserKitParamsFile ( fileName , basename , kitFilesSettings . paramsPath , getSource , surround ) ;
117+ upserKitParamsFile (
118+ ts ,
119+ fileName ,
120+ basename ,
121+ kitFilesSettings . paramsPath ,
122+ getSource ,
123+ surround
124+ ) ;
113125 if ( ! result ) {
114126 return ;
115127 }
@@ -128,6 +140,7 @@ export function upsertKitFile(
128140}
129141
130142function upserKitRouteFile (
143+ ts : _ts ,
131144 fileName : string ,
132145 basename : string ,
133146 getSource : ( ) => ts . SourceFile | undefined ,
@@ -144,7 +157,7 @@ function upserKitRouteFile(
144157 } ;
145158
146159 const isTsFile = basename . endsWith ( '.ts' ) ;
147- const exports = findExports ( source , isTsFile ) ;
160+ const exports = findExports ( ts , source , isTsFile ) ;
148161
149162 // add type to load function if not explicitly typed
150163 const load = exports . get ( 'load' ) ;
@@ -175,6 +188,7 @@ function upserKitRouteFile(
175188 // add types to GET/PUT/POST/PATCH/DELETE/OPTIONS if not explicitly typed
176189 const insertApiMethod = ( name : string ) => {
177190 addTypeToFunction (
191+ ts ,
178192 exports ,
179193 surround ,
180194 insert ,
@@ -194,6 +208,7 @@ function upserKitRouteFile(
194208}
195209
196210function upserKitParamsFile (
211+ ts : _ts ,
197212 fileName : string ,
198213 basename : string ,
199214 paramsPath : string ,
@@ -213,14 +228,15 @@ function upserKitParamsFile(
213228 } ;
214229
215230 const isTsFile = basename . endsWith ( '.ts' ) ;
216- const exports = findExports ( source , isTsFile ) ;
231+ const exports = findExports ( ts , source , isTsFile ) ;
217232
218- addTypeToFunction ( exports , surround , insert , 'match' , 'string' , 'boolean' ) ;
233+ addTypeToFunction ( ts , exports , surround , insert , 'match' , 'string' , 'boolean' ) ;
219234
220235 return { addedCode, originalText : source . getFullText ( ) } ;
221236}
222237
223238function upserKitClientHooksFile (
239+ ts : _ts ,
224240 fileName : string ,
225241 basename : string ,
226242 clientHooksPath : string ,
@@ -240,9 +256,10 @@ function upserKitClientHooksFile(
240256 } ;
241257
242258 const isTsFile = basename . endsWith ( '.ts' ) ;
243- const exports = findExports ( source , isTsFile ) ;
259+ const exports = findExports ( ts , source , isTsFile ) ;
244260
245261 addTypeToFunction (
262+ ts ,
246263 exports ,
247264 surround ,
248265 insert ,
@@ -254,6 +271,7 @@ function upserKitClientHooksFile(
254271}
255272
256273function upserKitServerHooksFile (
274+ ts : _ts ,
257275 fileName : string ,
258276 basename : string ,
259277 serverHooksPath : string ,
@@ -273,10 +291,10 @@ function upserKitServerHooksFile(
273291 } ;
274292
275293 const isTsFile = basename . endsWith ( '.ts' ) ;
276- const exports = findExports ( source , isTsFile ) ;
294+ const exports = findExports ( ts , source , isTsFile ) ;
277295
278296 const addType = ( name : string , type : string ) => {
279- addTypeToFunction ( exports , surround , insert , name , type ) ;
297+ addTypeToFunction ( ts , exports , surround , insert , name , type ) ;
280298 } ;
281299
282300 addType ( 'handleError' , `import('@sveltejs/kit').HandleServerError` ) ;
@@ -310,6 +328,7 @@ function addTypeToVariable(
310328}
311329
312330function addTypeToFunction (
331+ ts : _ts ,
313332 exports : Map <
314333 string ,
315334 | {
@@ -331,9 +350,11 @@ function addTypeToFunction(
331350 const paramInsertion = surround ( ! returnType ? `: Parameters<${ type } >[0]` : `: ${ type } ` ) ;
332351 insert ( paramPos , paramInsertion ) ;
333352 if ( ! fn . node . type && fn . node . body ) {
334- const returnPos = fn . node . body . getStart ( ) ;
353+ const returnPos = ts . isArrowFunction ( fn . node )
354+ ? fn . node . equalsGreaterThanToken . getStart ( )
355+ : fn . node . body . getStart ( ) ;
335356 const returnInsertion = surround (
336- ! returnType ? `: ReturnType<${ type } >` : `: ${ returnType } `
357+ ! returnType ? `: ReturnType<${ type } > ` : `: ${ returnType } `
337358 ) ;
338359 insert ( returnPos , returnInsertion ) ;
339360 }
0 commit comments