@@ -5,31 +5,32 @@ import type { BlockComment } from '../slang-nodes/types.d.ts';
55
66const { hardline, join, literalline } = doc . builders ;
77
8- function isIndentableBlockComment ( lines : string [ ] ) : boolean {
9- // If the comment has multiple lines and every line starts with a star
10- // we can fix the indentation of each line.
11- return lines . length > 1 && lines . every ( ( line ) => line . trimStart ( ) [ 0 ] === '*' ) ;
12- }
13-
148function printIndentableBlockComment ( lines : string [ ] ) : Doc {
159 return join (
1610 hardline ,
1711 lines . map ( ( line , index ) =>
18- index === 0
19- ? line . trimEnd ( )
20- : ` ${ index < lines . length - 1 ? line . trim ( ) : line . trimStart ( ) } `
12+ index === 0 ? line . trimEnd ( ) : ` ${ line . trimEnd ( ) } `
2113 )
2214 ) ;
2315}
2416
2517export function printBlockComment ( comment : BlockComment ) : Doc {
2618 // We remove the initial `/` to check if every line starts with `*`
2719 const lines = comment . value . slice ( 1 ) . split ( '\n' ) ;
20+ let trimmedLines ;
21+
22+ // Only process lines for possible indentation if the block has multiple
23+ // lines
24+ if ( lines . length > 1 ) {
25+ trimmedLines = lines . map ( ( line ) => line . trimStart ( ) ) ;
26+ }
2827
2928 return [
3029 '/' ,
31- isIndentableBlockComment ( lines )
32- ? printIndentableBlockComment ( lines )
30+ // If the comment has multiple lines and every line starts with a star
31+ // we can fix the indentation of each line.
32+ trimmedLines ?. every ( ( line ) => line . startsWith ( '*' ) )
33+ ? printIndentableBlockComment ( trimmedLines )
3334 : join ( literalline , lines )
3435 ] ;
3536}
0 commit comments