@@ -335,27 +335,42 @@ const instance_script = {
335
335
// }
336
336
}
337
337
338
- const binding = /** @type {Binding } */ ( state . scope . get ( declarator . id . name ) ) ;
338
+ const name = declarator . id . name ;
339
+ const binding = /** @type {Binding } */ ( state . scope . get ( name ) ) ;
339
340
340
341
if ( state . analysis . uses_props && ( declarator . init || binding . updated ) ) {
341
342
throw new Error (
342
343
'$$props is used together with named props in a way that cannot be automatically migrated.'
343
344
) ;
344
345
}
345
346
346
- state . props . push ( {
347
- local : declarator . id . name ,
348
- exported : binding . prop_alias ? binding . prop_alias : declarator . id . name ,
349
- init : declarator . init
347
+ const prop = state . props . find ( ( prop ) => prop . exported === ( binding . prop_alias || name ) ) ;
348
+ if ( prop ) {
349
+ // $$Props type was used
350
+ prop . init = declarator . init
350
351
? state . str . original . substring (
351
352
/** @type {number } */ ( declarator . init . start ) ,
352
353
/** @type {number } */ ( declarator . init . end )
353
354
)
354
- : '' ,
355
- optional : ! ! declarator . init ,
356
- bindable : binding . updated ,
357
- ...extract_type_and_comment ( declarator , state . str , path )
358
- } ) ;
355
+ : '' ;
356
+ prop . bindable = binding . updated ;
357
+ prop . exported = binding . prop_alias || name ;
358
+ } else {
359
+ state . props . push ( {
360
+ local : name ,
361
+ exported : binding . prop_alias ? binding . prop_alias : name ,
362
+ init : declarator . init
363
+ ? state . str . original . substring (
364
+ /** @type {number } */ ( declarator . init . start ) ,
365
+ /** @type {number } */ ( declarator . init . end )
366
+ )
367
+ : '' ,
368
+ optional : ! ! declarator . init ,
369
+ bindable : binding . updated ,
370
+ ...extract_type_and_comment ( declarator , state . str , path )
371
+ } ) ;
372
+ }
373
+
359
374
state . props_insertion_point = /** @type {number } */ ( declarator . end ) ;
360
375
state . str . update (
361
376
/** @type {number } */ ( declarator . start ) ,
@@ -944,6 +959,48 @@ function handle_identifier(node, state, path) {
944
959
}
945
960
}
946
961
// else passed as identifier, we don't know what to do here, so let it error
962
+ } else if (
963
+ parent ?. type === 'TSInterfaceDeclaration' ||
964
+ parent ?. type === 'TSTypeAliasDeclaration'
965
+ ) {
966
+ const members =
967
+ parent . type === 'TSInterfaceDeclaration' ? parent . body . body : parent . typeAnnotation ?. members ;
968
+ if ( Array . isArray ( members ) ) {
969
+ if ( node . name === '$$Props' ) {
970
+ for ( const member of members ) {
971
+ const prop = state . props . find ( ( prop ) => prop . exported === member . key . name ) ;
972
+
973
+ const type = state . str . original . substring (
974
+ member . typeAnnotation . typeAnnotation . start ,
975
+ member . typeAnnotation . typeAnnotation . end
976
+ ) ;
977
+
978
+ let comment ;
979
+ const comment_node = member . leadingComments ?. at ( - 1 ) ;
980
+ if ( comment_node ?. type === 'Block' ) {
981
+ comment = state . str . original . substring ( comment_node . start , comment_node . end ) ;
982
+ }
983
+
984
+ if ( prop ) {
985
+ prop . type = type ;
986
+ prop . optional = member . optional ;
987
+ prop . comment = comment ?? prop . comment ;
988
+ } else {
989
+ state . props . push ( {
990
+ local : member . key . name ,
991
+ exported : member . key . name ,
992
+ init : '' ,
993
+ bindable : false ,
994
+ optional : member . optional ,
995
+ type,
996
+ comment
997
+ } ) ;
998
+ }
999
+ }
1000
+
1001
+ state . str . remove ( parent . start , parent . end ) ;
1002
+ }
1003
+ }
947
1004
}
948
1005
}
949
1006
0 commit comments