Skip to content

Commit 827a741

Browse files
committed
avoid trimStart twice
1 parent 29a513d commit 827a741

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

src/slang-printers/print-block-comment.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ import type { BlockComment } from '../slang-nodes/types.d.ts';
55

66
const { 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-
148
function 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

2517
export 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

Comments
 (0)