@@ -549,6 +549,25 @@ const instance_script = {
549549 labeled_statement &&
550550 ( labeled_has_single_assignment || is_expression_assignment )
551551 ) {
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+
552571 // Someone wrote a `$: { ... }` statement which we can turn into a `$derived`
553572 state . str . appendRight (
554573 /** @type {number } */ ( declarator . id . typeAnnotation ?. end ?? declarator . id . end ) ,
@@ -573,6 +592,21 @@ const instance_script = {
573592 ')'
574593 ) ;
575594 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+ }
576610 } else {
577611 state . str . prependLeft (
578612 /** @type {number } */ ( declarator . id . typeAnnotation ?. end ?? declarator . id . end ) ,
@@ -1261,11 +1295,18 @@ function handle_events(element, state) {
12611295 * Returns start and end of the node. If the start is preceeded with white-space-only before a line break,
12621296 * the start will be the start of the line.
12631297 * @param {string } source
1264- * @param {Node } node
1298+ * @param {LabeledStatement } node
12651299 */
12661300function 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 ) ;
12691310
12701311 let idx = start ;
12711312 while ( source [ idx - 1 ] !== '\n' && source [ idx - 1 ] !== '\r' ) {
0 commit comments