@@ -549,6 +549,25 @@ const instance_script = {
549
549
labeled_statement &&
550
550
( labeled_has_single_assignment || is_expression_assignment )
551
551
) {
552
+ const indent = state . str . original . substring (
553
+ state . str . original . lastIndexOf ( '\n' , /** @type {number } */ ( node . start ) ) + 1 ,
554
+ /** @type {number } */ ( node . start )
555
+ ) ;
556
+ // transfer all the leading comments
557
+ if (
558
+ labeled_statement . body . type === 'BlockStatement' &&
559
+ labeled_statement . body . body [ 0 ] . leadingComments
560
+ ) {
561
+ for ( let comment of labeled_statement . body . body [ 0 ] . leadingComments ) {
562
+ state . str . prependLeft (
563
+ /** @type {number } */ ( node . start ) ,
564
+ comment . type === 'Block'
565
+ ? `/*${ comment . value } */\n${ indent } `
566
+ : `// ${ comment . value } \n${ indent } `
567
+ ) ;
568
+ }
569
+ }
570
+
552
571
// Someone wrote a `$: { ... }` statement which we can turn into a `$derived`
553
572
state . str . appendRight (
554
573
/** @type {number } */ ( declarator . id . typeAnnotation ?. end ?? declarator . id . end ) ,
@@ -573,6 +592,21 @@ const instance_script = {
573
592
')'
574
593
) ;
575
594
state . derived_labeled_statements . add ( labeled_statement ) ;
595
+
596
+ // transfer all the trailing comments
597
+ if (
598
+ labeled_statement . body . type === 'BlockStatement' &&
599
+ labeled_statement . body . body [ 0 ] . trailingComments
600
+ ) {
601
+ for ( let comment of labeled_statement . body . body [ 0 ] . trailingComments ) {
602
+ state . str . appendRight (
603
+ /** @type {number } */ ( declarator . id . typeAnnotation ?. end ?? declarator . id . end ) ,
604
+ comment . type === 'Block'
605
+ ? `\n${ indent } /*${ comment . value } */`
606
+ : `\n${ indent } // ${ comment . value } `
607
+ ) ;
608
+ }
609
+ }
576
610
} else {
577
611
state . str . prependLeft (
578
612
/** @type {number } */ ( declarator . id . typeAnnotation ?. end ?? declarator . id . end ) ,
@@ -1261,11 +1295,18 @@ function handle_events(element, state) {
1261
1295
* Returns start and end of the node. If the start is preceeded with white-space-only before a line break,
1262
1296
* the start will be the start of the line.
1263
1297
* @param {string } source
1264
- * @param {Node } node
1298
+ * @param {LabeledStatement } node
1265
1299
*/
1266
1300
function get_node_range ( source , node ) {
1267
- let start = /** @type {number } */ ( node . start ) ;
1268
- let end = /** @type {number } */ ( node . end ) ;
1301
+ const first_leading_comment = node . leadingComments ?. [ 0 ] ;
1302
+ const last_trailing_comment = node . trailingComments ?. [ node . trailingComments . length - 1 ] ;
1303
+
1304
+ // @ts -expect-error the type of `Comment` seems to be wrong...the node actually contains
1305
+ // start and end but the type seems to only contain a `range` (which doesn't actually exists)
1306
+ let start = /** @type {number } */ ( first_leading_comment ?. start ?? node . start ) ;
1307
+ // @ts -expect-error the type of `Comment` seems to be wrong...the node actually contains
1308
+ // start and end but the type seems to only contain a `range` (which doesn't actually exists)
1309
+ let end = /** @type {number } */ ( last_trailing_comment ?. end ?? node . end ) ;
1269
1310
1270
1311
let idx = start ;
1271
1312
while ( source [ idx - 1 ] !== '\n' && source [ idx - 1 ] !== '\r' ) {
0 commit comments