-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathprint-comments.ts
More file actions
44 lines (40 loc) · 1.15 KB
/
print-comments.ts
File metadata and controls
44 lines (40 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { doc, util } from 'prettier';
import { printComment } from '../slang-comments/printer.js';
import { joinExisting } from '../slang-utils/join-existing.js';
import { locEnd } from '../slang-utils/loc.js';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type {
AstNode,
Comment,
StrictAstNode
} from '../slang-nodes/types.d.ts';
const { hardline, line } = doc.builders;
function isPrintable(comment: Comment): boolean {
return !comment.trailing && !comment.leading && !comment.printed;
}
export function printComments(
node: StrictAstNode,
path: AstPath<StrictAstNode>,
options: ParserOptions<AstNode>
): Doc[] {
const lastPrintableIndex = (node.comments ?? []).findLastIndex(isPrintable);
if (lastPrintableIndex === -1) {
return [];
}
return joinExisting(
line,
path.map(({ node: comment }, index) => {
if (!isPrintable(comment)) {
return '';
}
comment.printed = true;
return [
printComment(path),
index !== lastPrintableIndex &&
util.isNextLineEmpty(options.originalText, locEnd(comment))
? hardline
: ''
];
}, 'comments')
);
}