-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathprint-comments.ts
More file actions
40 lines (36 loc) · 1.09 KB
/
print-comments.ts
File metadata and controls
40 lines (36 loc) · 1.09 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
import { doc, util } from 'prettier';
import { printComment } from '../slang-comments/printer.js';
import { locEnd } from '../slang-utils/loc.js';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { Comment, PrintableNode } from '../slang-nodes/types.d.ts';
const { hardline } = doc.builders;
function isPrintable(comment: Comment): boolean {
return !comment.trailing && !comment.leading && !comment.printed;
}
export function printComments(
node: PrintableNode,
path: AstPath<PrintableNode>,
options: ParserOptions<PrintableNode>
): Doc[] {
const lastPrintableIndex = (node.comments ?? []).findLastIndex(isPrintable);
if (lastPrintableIndex === -1) {
return [];
}
return path.map(({ node: comment }, index) => {
if (!isPrintable(comment)) {
return '';
}
comment.printed = true;
return [
printComment(path),
index !== lastPrintableIndex
? [
hardline,
util.isNextLineEmpty(options.originalText, locEnd(comment))
? hardline
: ''
]
: ''
];
}, 'comments');
}