Skip to content

Commit 975850d

Browse files
committed
iterate only once over lines and exit early if not successful
1 parent a6ed57b commit 975850d

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

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

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

66
const { hardline, join, literalline } = doc.builders;
77

8+
function trimmedIndentableLines(lines: string[]): string[] | undefined {
9+
// If the comment has multiple lines and every line starts with a star
10+
// we can fix the indentation of each line.
11+
if (lines.length > 1) {
12+
const trimmedLines = [];
13+
for (const line of lines) {
14+
const trimmedLine = line.trimStart();
15+
if (!trimmedLine.startsWith('*')) {
16+
return;
17+
}
18+
trimmedLines.push(trimmedLine);
19+
}
20+
return trimmedLines;
21+
}
22+
}
23+
824
function printIndentableBlockComment(lines: string[]): Doc {
925
return join(
1026
hardline,
@@ -15,19 +31,11 @@ function printIndentableBlockComment(lines: string[]): Doc {
1531
export function printBlockComment(comment: BlockComment): Doc {
1632
// We remove the initial `/` to check if every line starts with `*`
1733
const lines = comment.value.slice(1).split('\n');
18-
let trimmedLines;
19-
20-
// Only process lines for possible indentation if the block has multiple
21-
// lines
22-
if (lines.length > 1) {
23-
trimmedLines = lines.map((line) => line.trimStart());
24-
}
34+
const trimmedLines = trimmedIndentableLines(lines);
2535

2636
return [
2737
'/',
28-
// If the comment has multiple lines and every line starts with a star
29-
// we can fix the indentation of each line.
30-
trimmedLines?.every((line) => line.startsWith('*'))
38+
trimmedLines
3139
? printIndentableBlockComment(trimmedLines)
3240
: join(literalline, lines)
3341
];

0 commit comments

Comments
 (0)