@@ -215,84 +215,76 @@ function analyzeDollarDollarVariables(
215
215
) ;
216
216
}
217
217
218
- for ( const svelte5Global of globalsForSvelte5 ) {
219
- switch ( svelte5Global ) {
220
- case "$state" : {
221
- if (
222
- scopeManager . globalScope ! . through . some (
223
- ( reference ) => reference . identifier . name === svelte5Global ,
224
- )
225
- ) {
226
- appendDeclareVirtualScript (
227
- svelte5Global ,
228
- "<T>(initial: T): T" ,
229
- "function" ,
230
- ) ;
231
- appendDeclareVirtualScript (
232
- svelte5Global ,
233
- "<T>(): T | undefined" ,
234
- "function" ,
235
- ) ;
218
+ addSvelte5Globals ( ) ;
219
+
220
+ function addSvelte5Globals ( ) {
221
+ for ( const svelte5Global of globalsForSvelte5 ) {
222
+ switch ( svelte5Global ) {
223
+ case "$state" : {
224
+ if (
225
+ scopeManager . globalScope ! . through . some (
226
+ ( reference ) => reference . identifier . name === svelte5Global ,
227
+ )
228
+ ) {
229
+ appendDeclareFunctionVirtualScript (
230
+ svelte5Global ,
231
+ "<T>(initial: T): T" ,
232
+ ) ;
233
+ appendDeclareFunctionVirtualScript (
234
+ svelte5Global ,
235
+ "<T>(): T | undefined" ,
236
+ ) ;
237
+ }
238
+ break ;
236
239
}
237
- break ;
238
- }
239
- case "$derived" : {
240
- if (
241
- scopeManager . globalScope ! . through . some (
242
- ( reference ) => reference . identifier . name === svelte5Global ,
243
- )
244
- ) {
245
- appendDeclareVirtualScript (
246
- svelte5Global ,
247
- "<T>(expression: T): T" ,
248
- "function" ,
249
- ) ;
240
+ case "$derived" : {
241
+ if (
242
+ scopeManager . globalScope ! . through . some (
243
+ ( reference ) => reference . identifier . name === svelte5Global ,
244
+ )
245
+ ) {
246
+ appendDeclareFunctionVirtualScript (
247
+ svelte5Global ,
248
+ "<T>(expression: T): T" ,
249
+ ) ;
250
+ }
251
+ break ;
250
252
}
251
- break ;
252
- }
253
- case "$effect" :
254
- case "$effect.pre" : {
255
- if (
256
- scopeManager . globalScope ! . through . some (
257
- ( reference ) => reference . identifier . name === svelte5Global ,
258
- )
259
- ) {
260
- appendDeclareVirtualScript (
261
- svelte5Global ,
262
- "(fn: () => void | (() => void)): void" ,
263
- "function" ,
264
- ) ;
253
+ case "$effect" :
254
+ case "$effect.pre" : {
255
+ if (
256
+ scopeManager . globalScope ! . through . some (
257
+ ( reference ) => reference . identifier . name === svelte5Global ,
258
+ )
259
+ ) {
260
+ appendDeclareFunctionVirtualScript (
261
+ svelte5Global ,
262
+ "(fn: () => void | (() => void)): void" ,
263
+ ) ;
264
+ }
265
+ break ;
265
266
}
266
- break ;
267
- }
268
- case "$props" : {
269
- if (
270
- scopeManager . globalScope ! . through . some (
271
- ( reference ) => reference . identifier . name === svelte5Global ,
272
- )
273
- ) {
274
- appendDeclareVirtualScript ( svelte5Global , "<T>(): T" , "function" ) ;
267
+ case "$props" : {
268
+ if (
269
+ scopeManager . globalScope ! . through . some (
270
+ ( reference ) => reference . identifier . name === svelte5Global ,
271
+ )
272
+ ) {
273
+ appendDeclareFunctionVirtualScript ( svelte5Global , "<T>(): T" ) ;
274
+ }
275
+ break ;
276
+ }
277
+ default : {
278
+ const _ : never = svelte5Global ;
279
+ throw Error ( `Unknown global: ${ _ } ` ) ;
275
280
}
276
- break ;
277
- }
278
- default : {
279
- const _ : never = svelte5Global ;
280
- throw Error ( `Unknown global: ${ _ } ` ) ;
281
281
}
282
282
}
283
283
}
284
284
285
285
/** Append declare virtual script */
286
- function appendDeclareVirtualScript (
287
- name : string ,
288
- type : string ,
289
- letOrFunction : "let" | "function" = "let" ,
290
- ) {
291
- if ( letOrFunction === "let" ) {
292
- ctx . appendVirtualScript ( `declare let ${ name } : ${ type } ;` ) ;
293
- } else {
294
- ctx . appendVirtualScript ( `declare function ${ name } ${ type } ;` ) ;
295
- }
286
+ function appendDeclareVirtualScript ( name : string , type : string ) {
287
+ ctx . appendVirtualScript ( `declare let ${ name } : ${ type } ;` ) ;
296
288
ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
297
289
if (
298
290
node . type !== "VariableDeclaration" ||
@@ -317,6 +309,33 @@ function analyzeDollarDollarVariables(
317
309
return true ;
318
310
} ) ;
319
311
}
312
+
313
+ /** Append declare virtual script */
314
+ function appendDeclareFunctionVirtualScript ( name : string , type : string ) {
315
+ ctx . appendVirtualScript ( `declare function ${ name } ${ type } ;` ) ;
316
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
317
+ if (
318
+ node . type !== "TSDeclareFunction" ||
319
+ ! node . declare ||
320
+ node . id ?. type !== "Identifier" ||
321
+ node . id . name !== name
322
+ ) {
323
+ return false ;
324
+ }
325
+ const program = result . ast ;
326
+ program . body . splice ( program . body . indexOf ( node ) , 1 ) ;
327
+
328
+ const scopeManager = result . scopeManager as ScopeManager ;
329
+
330
+ // Remove `declare` variable
331
+ removeAllScopeAndVariableAndReference ( node , {
332
+ visitorKeys : result . visitorKeys ,
333
+ scopeManager,
334
+ } ) ;
335
+
336
+ return true ;
337
+ } ) ;
338
+ }
320
339
}
321
340
322
341
/**
0 commit comments