@@ -313,27 +313,30 @@ function analyzeRuneVariables(
313313 }
314314 switch ( globalName ) {
315315 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+ ] ) ;
318320 break ;
319321 }
320322 case "$derived" : {
321- appendDeclareFunctionVirtualScript ( globalName , "<T>(expression: T): T" ) ;
323+ appendDeclareFunctionVirtualScripts ( globalName , [
324+ "<T>(expression: T): T" ,
325+ ] ) ;
322326 break ;
323327 }
324328 case "$effect" : {
325- appendDeclareFunctionVirtualScript (
326- globalName ,
329+ appendDeclareFunctionVirtualScripts ( globalName , [
327330 "(fn: () => void | (() => void)): void" ,
328- ) ;
329- appendDeclareNamespaceVirtualScript (
330- globalName ,
331+ ] ) ;
332+ appendDeclareNamespaceVirtualScripts ( globalName , [
331333 "export function pre(fn: () => void | (() => void)): void;" ,
332- ) ;
334+ "export function active(): boolean;" ,
335+ ] ) ;
333336 break ;
334337 }
335338 case "$props" : {
336- appendDeclareFunctionVirtualScript ( globalName , "<T>(): T" ) ;
339+ appendDeclareFunctionVirtualScripts ( globalName , [ "<T>(): T" ] ) ;
337340 break ;
338341 }
339342 default : {
@@ -344,56 +347,63 @@ function analyzeRuneVariables(
344347 }
345348
346349 /** 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 ) ;
360364
361- const scopeManager = result . scopeManager as ScopeManager ;
365+ const scopeManager = result . scopeManager as ScopeManager ;
362366
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+ } ) ;
368372
369- return true ;
370- } ) ;
373+ return true ;
374+ } ) ;
375+ }
371376 }
372377
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 ) ;
386395
387- const scopeManager = result . scopeManager as ScopeManager ;
396+ const scopeManager = result . scopeManager as ScopeManager ;
388397
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+ } ) ;
394403
395- return true ;
396- } ) ;
404+ return true ;
405+ } ) ;
406+ }
397407 }
398408}
399409
0 commit comments