@@ -313,27 +313,30 @@ function analyzeRuneVariables(
313
313
}
314
314
switch ( globalName ) {
315
315
case "$state" : {
316
- appendDeclareFunctionVirtualScript ( globalName , "<T>(initial: T): T" ) ;
317
- appendDeclareFunctionVirtualScript ( globalName , "<T>(): T | undefined" ) ;
316
+ appendDeclareFunctionVirtualScripts ( globalName , [
317
+ "<T>(initial: T): T" ,
318
+ "<T>(): T | undefined" ,
319
+ ] ) ;
318
320
break ;
319
321
}
320
322
case "$derived" : {
321
- appendDeclareFunctionVirtualScript ( globalName , "<T>(expression: T): T" ) ;
323
+ appendDeclareFunctionVirtualScripts ( globalName , [
324
+ "<T>(expression: T): T" ,
325
+ ] ) ;
322
326
break ;
323
327
}
324
328
case "$effect" : {
325
- appendDeclareFunctionVirtualScript (
326
- globalName ,
329
+ appendDeclareFunctionVirtualScripts ( globalName , [
327
330
"(fn: () => void | (() => void)): void" ,
328
- ) ;
329
- appendDeclareNamespaceVirtualScript (
330
- globalName ,
331
+ ] ) ;
332
+ appendDeclareNamespaceVirtualScripts ( globalName , [
331
333
"export function pre(fn: () => void | (() => void)): void;" ,
332
- ) ;
334
+ "export function active(): boolean;" ,
335
+ ] ) ;
333
336
break ;
334
337
}
335
338
case "$props" : {
336
- appendDeclareFunctionVirtualScript ( globalName , "<T>(): T" ) ;
339
+ appendDeclareFunctionVirtualScripts ( globalName , [ "<T>(): T" ] ) ;
337
340
break ;
338
341
}
339
342
default : {
@@ -344,56 +347,63 @@ function analyzeRuneVariables(
344
347
}
345
348
346
349
/** Append declare virtual script */
347
- function appendDeclareFunctionVirtualScript ( name : string , type : string ) {
348
- ctx . appendVirtualScript ( `declare function ${ name } ${ type } ;` ) ;
349
- ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
350
- if (
351
- node . type !== "TSDeclareFunction" ||
352
- ! node . declare ||
353
- node . id ?. type !== "Identifier" ||
354
- node . id . name !== name
355
- ) {
356
- return false ;
357
- }
358
- const program = result . ast ;
359
- program . body . splice ( program . body . indexOf ( node ) , 1 ) ;
350
+ function appendDeclareFunctionVirtualScripts ( name : string , types : string [ ] ) {
351
+ for ( const type of types ) {
352
+ ctx . appendVirtualScript ( `declare function ${ name } ${ type } ;` ) ;
353
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
354
+ if (
355
+ node . type !== "TSDeclareFunction" ||
356
+ ! node . declare ||
357
+ node . id ?. type !== "Identifier" ||
358
+ node . id . name !== name
359
+ ) {
360
+ return false ;
361
+ }
362
+ const program = result . ast ;
363
+ program . body . splice ( program . body . indexOf ( node ) , 1 ) ;
360
364
361
- const scopeManager = result . scopeManager as ScopeManager ;
365
+ const scopeManager = result . scopeManager as ScopeManager ;
362
366
363
- // Remove `declare` variable
364
- removeAllScopeAndVariableAndReference ( node , {
365
- visitorKeys : result . visitorKeys ,
366
- scopeManager,
367
- } ) ;
367
+ // Remove `declare` variable
368
+ removeAllScopeAndVariableAndReference ( node , {
369
+ visitorKeys : result . visitorKeys ,
370
+ scopeManager,
371
+ } ) ;
368
372
369
- return true ;
370
- } ) ;
373
+ return true ;
374
+ } ) ;
375
+ }
371
376
}
372
377
373
- function appendDeclareNamespaceVirtualScript ( name : string , script : string ) {
374
- ctx . appendVirtualScript ( `declare namespace $effect { ${ script } }` ) ;
375
- ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
376
- if (
377
- node . type !== "TSModuleDeclaration" ||
378
- ! node . declare ||
379
- node . id ?. type !== "Identifier" ||
380
- node . id . name !== name
381
- ) {
382
- return false ;
383
- }
384
- const program = result . ast ;
385
- program . body . splice ( program . body . indexOf ( node ) , 1 ) ;
378
+ function appendDeclareNamespaceVirtualScripts (
379
+ name : string ,
380
+ scripts : string [ ] ,
381
+ ) {
382
+ for ( const script of scripts ) {
383
+ ctx . appendVirtualScript ( `declare namespace ${ name } { ${ script } }` ) ;
384
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
385
+ if (
386
+ node . type !== "TSModuleDeclaration" ||
387
+ ! node . declare ||
388
+ node . id ?. type !== "Identifier" ||
389
+ node . id . name !== name
390
+ ) {
391
+ return false ;
392
+ }
393
+ const program = result . ast ;
394
+ program . body . splice ( program . body . indexOf ( node ) , 1 ) ;
386
395
387
- const scopeManager = result . scopeManager as ScopeManager ;
396
+ const scopeManager = result . scopeManager as ScopeManager ;
388
397
389
- // Remove `declare` variable
390
- removeAllScopeAndVariableAndReference ( node , {
391
- visitorKeys : result . visitorKeys ,
392
- scopeManager,
393
- } ) ;
398
+ // Remove `declare` variable
399
+ removeAllScopeAndVariableAndReference ( node , {
400
+ visitorKeys : result . visitorKeys ,
401
+ scopeManager,
402
+ } ) ;
394
403
395
- return true ;
396
- } ) ;
404
+ return true ;
405
+ } ) ;
406
+ }
397
407
}
398
408
}
399
409
0 commit comments