@@ -417,7 +417,7 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
417
417
const code = new MagicString ( js_code ) ;
418
418
const imports = new Map ( ) ;
419
419
420
- async function walk ( node : ts . Node ) {
420
+ async function walk ( node : ts . Node , prev : ts . Node | null ) {
421
421
const jsdoc = get_jsdoc ( node ) ;
422
422
423
423
if ( jsdoc ) {
@@ -563,19 +563,30 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
563
563
while ( start > 0 && code . original [ start - 1 ] === '\t' ) start -= 1 ;
564
564
while ( start > 0 && code . original [ start - 1 ] === '\n' ) start -= 1 ;
565
565
566
- const slice = code . original . slice ( node . getStart ( ) , node . getEnd ( ) ) ;
567
- const is_multiline = slice . includes ( '\n' ) ;
566
+ let is_multiline = false ;
567
+
568
+ if ( prev ) {
569
+ is_multiline =
570
+ code . original . slice ( prev . getStart ( ) , prev . getEnd ( ) ) . includes ( '\n' ) ||
571
+ code . original . slice ( node . getStart ( ) , node . getEnd ( ) ) . includes ( '\n' ) ;
572
+ }
568
573
569
574
code . overwrite ( start , end , is_multiline ? '\n' : '' ) ;
570
575
}
571
576
}
572
577
578
+ // the TypeScript API is such a hot mess, AFAICT there is no non-stupid way
579
+ // to get the previous sibling within the visitor, so since we need it we
580
+ // have to pass it in from the parent visitor
581
+ let child_prev : ts . Node | null = null ;
582
+
573
583
for ( const child_node of node . getChildren ( ) ) {
574
- await walk ( child_node ) ;
584
+ await walk ( child_node , child_prev ) ;
585
+ child_prev = child_node ;
575
586
}
576
587
}
577
588
578
- await walk ( ast ) ;
589
+ await walk ( ast , null ) ;
579
590
580
591
if ( imports . size ) {
581
592
const import_statements = Array . from ( imports . entries ( ) )
0 commit comments