@@ -114,7 +114,10 @@ export function migrate(source, { filename } = {}) {
114
114
script_insertions : new Set ( ) ,
115
115
derived_components : new Map ( ) ,
116
116
derived_labeled_statements : new Set ( ) ,
117
- has_svelte_self : false
117
+ has_svelte_self : false ,
118
+ uses_ts : ! ! parsed . instance ?. attributes . some (
119
+ ( attr ) => attr . name === 'lang' && /** @type {any } */ ( attr ) . value [ 0 ] . data === 'ts'
120
+ )
118
121
} ;
119
122
120
123
if ( parsed . module ) {
@@ -207,15 +210,12 @@ export function migrate(source, { filename } = {}) {
207
210
// some render tags or forwarded event attributes to add
208
211
str . appendRight ( insertion_point , ` ${ props } ,` ) ;
209
212
} else {
210
- const uses_ts = parsed . instance ?. attributes . some (
211
- ( attr ) => attr . name === 'lang' && /** @type {any } */ ( attr ) . value [ 0 ] . data === 'ts'
212
- ) ;
213
213
const type_name = state . scope . root . unique ( 'Props' ) . name ;
214
214
let type = '' ;
215
215
216
216
// Try to infer when we don't want to add types (e.g. user doesn't use types, or this is a zero-types +page.svelte)
217
217
if ( state . has_type_or_fallback || state . props . every ( ( prop ) => prop . slot_name ) ) {
218
- if ( uses_ts ) {
218
+ if ( state . uses_ts ) {
219
219
type = `interface ${ type_name } {${ newline_separator } ${ state . props
220
220
. map ( ( prop ) => {
221
221
const comment = prop . comment ? `${ prop . comment } ${ newline_separator } ` : '' ;
@@ -236,7 +236,7 @@ export function migrate(source, { filename } = {}) {
236
236
}
237
237
238
238
let props_declaration = `let {${ props_separator } ${ props } ${ has_many_props ? `\n${ indent } ` : ' ' } }` ;
239
- if ( uses_ts ) {
239
+ if ( state . uses_ts ) {
240
240
if ( type ) {
241
241
props_declaration = `${ type } \n\n${ indent } ${ props_declaration } ` ;
242
242
}
@@ -355,6 +355,7 @@ export function migrate(source, { filename } = {}) {
355
355
* derived_components: Map<string, string>;
356
356
* derived_labeled_statements: Set<LabeledStatement>;
357
357
* has_svelte_self: boolean;
358
+ * uses_ts: boolean;
358
359
* }} State
359
360
*/
360
361
@@ -1319,7 +1320,7 @@ function extract_type_and_comment(declarator, state, path) {
1319
1320
return { type : str . original . substring ( start , declarator . id . typeAnnotation . end ) , comment } ;
1320
1321
}
1321
1322
1322
- let cleaned_comment = comment
1323
+ let cleaned_comment_arr = comment
1323
1324
?. split ( '\n' )
1324
1325
. map ( ( line ) =>
1325
1326
line
@@ -1334,17 +1335,17 @@ function extract_type_and_comment(declarator, state, path) {
1334
1335
. replace ( / ^ \* \s * / g, '' )
1335
1336
)
1336
1337
. filter ( Boolean ) ;
1337
- const first_at_comment = cleaned_comment ?. findIndex ( ( line ) => line . startsWith ( '@' ) ) ;
1338
- comment = cleaned_comment
1339
- ?. slice ( 0 , first_at_comment !== - 1 ? first_at_comment : cleaned_comment . length )
1338
+ const first_at_comment = cleaned_comment_arr ?. findIndex ( ( line ) => line . startsWith ( '@' ) ) ;
1339
+ let cleaned_comment = cleaned_comment_arr
1340
+ ?. slice ( 0 , first_at_comment !== - 1 ? first_at_comment : cleaned_comment_arr . length )
1340
1341
. join ( '\n' ) ;
1341
1342
1342
1343
// try to find a comment with a type annotation, hinting at jsdoc
1343
1344
if ( parent ?. type === 'ExportNamedDeclaration' && comment_node ) {
1344
1345
state . has_type_or_fallback = true ;
1345
1346
const match = / @ t y p e { ( .+ ) } / . exec ( comment_node . value ) ;
1346
1347
if ( match ) {
1347
- return { type : match [ 1 ] , comment } ;
1348
+ return { type : match [ 1 ] , comment : cleaned_comment } ;
1348
1349
}
1349
1350
}
1350
1351
@@ -1353,11 +1354,11 @@ function extract_type_and_comment(declarator, state, path) {
1353
1354
state . has_type_or_fallback = true ; // only assume type if it's trivial to infer - else someone would've added a type annotation
1354
1355
const type = typeof declarator . init . value ;
1355
1356
if ( type === 'string' || type === 'number' || type === 'boolean' ) {
1356
- return { type, comment } ;
1357
+ return { type, comment : state . uses_ts ? comment : cleaned_comment } ;
1357
1358
}
1358
1359
}
1359
1360
1360
- return { type : 'any' , comment } ;
1361
+ return { type : 'any' , comment : state . uses_ts ? comment : cleaned_comment } ;
1361
1362
}
1362
1363
1363
1364
// Ensure modifiers are applied in the same order as Svelte 4
0 commit comments