forked from jhipster/prettier-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.ts
More file actions
59 lines (56 loc) · 1.68 KB
/
printer.ts
File metadata and controls
59 lines (56 loc) · 1.68 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { AstPath, Printer } from "prettier";
import {
canAttachComment,
handleLineComment,
handleRemainingComment,
isFullyBetweenPrettierIgnore
} from "./comments.js";
import {
isNonTerminal,
isTerminal,
printComment,
type JavaNode,
type JavaTerminal
} from "./printers/helpers.js";
import { printerForNodeType } from "./printers/index.js";
export default {
print(path: DistributedAstPath<JavaNode>, options, print, args) {
return hasTerminal(path)
? path.node.image
: printerForNodeType(path.node.name)(path, print, options, args);
},
hasPrettierIgnore(path) {
const { node } = path;
return (
node.comments?.some(({ image }) =>
/^(\/\/\s*prettier-ignore|\/\*\s*prettier-ignore\s*\*\/)$/.test(image)
) === true ||
(canAttachComment(node) && isFullyBetweenPrettierIgnore(path))
);
},
canAttachComment,
isBlockComment(node) {
return isTerminal(node) && node.tokenType.name === "TraditionalComment";
},
printComment(commentPath) {
const { node } = commentPath;
if (isNonTerminal(node) || node.tokenType.GROUP !== "comments") {
throw new Error(`Not a comment: ${JSON.stringify(node)}`);
}
return printComment(node);
},
getCommentChildNodes(node) {
return isNonTerminal(node)
? Object.values(node.children).flatMap(child => child)
: [];
},
handleComments: {
ownLine: handleLineComment,
endOfLine: handleLineComment,
remaining: handleRemainingComment
}
} satisfies Printer<JavaNode>;
function hasTerminal(path: AstPath<JavaNode>): path is AstPath<JavaTerminal> {
return isTerminal(path.node);
}
type DistributedAstPath<T> = T extends any ? AstPath<T> : never;