1
1
/** @import { VariableDeclarator, Node, Identifier, AssignmentExpression, LabeledStatement, ExpressionStatement } from 'estree' */
2
- /** @import { Visitors } from 'zimmerframe' */
2
+ /** @import { Visitors, Context } from 'zimmerframe' */
3
3
/** @import { ComponentAnalysis } from '../phases/types.js' */
4
4
/** @import { Scope, ScopeRoot } from '../phases/scope.js' */
5
5
/** @import { AST, Binding, SvelteNode, ValidatedCompileOptions } from '#compiler' */
@@ -398,6 +398,8 @@ export function migrate(source, { filename, use_ts } = {}) {
398
398
}
399
399
}
400
400
401
+ /** @typedef {SvelteNode | { type: "TSTypeReference", typeName: Identifier, start: number, end: number } } ASTNode */
402
+
401
403
/**
402
404
* @typedef {{
403
405
* scope: Scope;
@@ -416,11 +418,12 @@ export function migrate(source, { filename, use_ts } = {}) {
416
418
* derived_components: Map<string, string>;
417
419
* derived_labeled_statements: Set<LabeledStatement>;
418
420
* has_svelte_self: boolean;
421
+ * migrate_prop_component_type?: boolean;
419
422
* uses_ts: boolean;
420
423
* }} State
421
424
*/
422
425
423
- /** @type {Visitors<SvelteNode , State> } */
426
+ /** @type {Visitors<ASTNode , State> } */
424
427
const instance_script = {
425
428
_ ( node , { state, next } ) {
426
429
// @ts -expect-error
@@ -437,8 +440,27 @@ const instance_script = {
437
440
}
438
441
next ( ) ;
439
442
} ,
440
- Identifier ( node , { state, path } ) {
443
+ TSTypeReference ( node , { state, path } ) {
444
+ if ( state . analysis . runes ) return ;
445
+ if ( node . typeName . type === 'Identifier' ) {
446
+ const binding = state . scope . get ( node . typeName . name ) ;
447
+ if (
448
+ binding &&
449
+ binding . declaration_kind === 'import' &&
450
+ binding . initial ?. type === 'ImportDeclaration' &&
451
+ binding . initial . source . value ?. toString ( ) . endsWith ( '.svelte' )
452
+ ) {
453
+ state . str . overwrite (
454
+ node . start ,
455
+ node . end ,
456
+ `import('svelte').ComponentExports<typeof ${ state . str . original . substring ( node . start , node . end ) } >`
457
+ ) ;
458
+ }
459
+ }
460
+ } ,
461
+ Identifier ( node , { state, path, next } ) {
441
462
handle_identifier ( node , state , path ) ;
463
+ next ( ) ;
442
464
} ,
443
465
ImportDeclaration ( node , { state } ) {
444
466
state . props_insertion_point = node . end ?? state . props_insertion_point ;
@@ -503,6 +525,8 @@ const instance_script = {
503
525
return ;
504
526
}
505
527
528
+ next ( ) ;
529
+
506
530
let nr_of_props = 0 ;
507
531
508
532
for ( const declarator of node . declarations ) {
@@ -1409,7 +1433,7 @@ function migrate_slot_usage(node, path, state) {
1409
1433
/**
1410
1434
* @param {VariableDeclarator } declarator
1411
1435
* @param {State } state
1412
- * @param {SvelteNode [] } path
1436
+ * @param {ASTNode [] } path
1413
1437
*/
1414
1438
function extract_type_and_comment ( declarator , state , path ) {
1415
1439
const str = state . str ;
@@ -1432,7 +1456,10 @@ function extract_type_and_comment(declarator, state, path) {
1432
1456
while ( str . original [ start ] === ' ' ) {
1433
1457
start ++ ;
1434
1458
}
1435
- return { type : str . original . substring ( start , declarator . id . typeAnnotation . end ) , comment } ;
1459
+ return {
1460
+ type : str . snip ( start , declarator . id . typeAnnotation . end ) . toString ( ) ,
1461
+ comment
1462
+ } ;
1436
1463
}
1437
1464
1438
1465
let cleaned_comment_arr = comment
0 commit comments