@@ -523,16 +523,30 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
523
523
while ( code . original [ start - 1 ] !== ')' ) start -= 1 ;
524
524
code . appendLeft ( start , `: ${ returns } ` ) ;
525
525
}
526
+ } else if ( type && ts . isParenthesizedExpression ( node ) ) {
527
+ // convert `/* @type {Foo } */ (foo)` to `foo as Foo`
528
+ // TODO one day we may need to account for operator precedence
529
+ // (i.e. preserve the parens in e.g. `(x as y).z()`)
530
+ let start = node . getStart ( ) ;
531
+ while ( js_code [ start - 1 ] !== '/' ) start -= 1 ;
532
+ code . remove ( start , node . getStart ( ) + 1 ) ;
533
+
534
+ let end = node . getEnd ( ) ;
535
+ code . overwrite ( end - 1 , end , ` as ${ type } ` ) ;
526
536
} else {
527
- throw new Error ( 'Unhandled @type JsDoc->TS conversion: ' + js_code ) ;
537
+ throw new Error (
538
+ 'Unhandled @type JsDoc->TS conversion: ' + js_code . slice ( node . getStart ( ) , node . getEnd ( ) )
539
+ ) ;
528
540
}
529
541
530
542
if ( ! comment ) {
531
543
// remove the whole thing
532
544
let start = jsdoc [ 0 ] . getStart ( ) ;
533
545
let end = jsdoc [ 0 ] . getEnd ( ) ;
534
546
535
- while ( start > 0 && code . original [ start ] !== '\n' ) start -= 1 ;
547
+ while ( start > 0 && code . original [ start - 1 ] === '\t' ) start -= 1 ;
548
+ while ( start > 0 && code . original [ start - 1 ] === '\n' ) start -= 1 ;
549
+
536
550
code . overwrite ( start , end , '' ) ;
537
551
}
538
552
}
@@ -560,7 +574,7 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
560
574
while ( js_code [ i ] !== '\n' ) i += 1 ;
561
575
i += 1 ;
562
576
563
- code . appendLeft ( i , import_statements + '\n' ) ;
577
+ code . appendLeft ( i , '\n' + import_statements + '\n' ) ;
564
578
} else {
565
579
code . prependLeft ( 0 , offset + import_statements + '\n' ) ;
566
580
}
0 commit comments