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
60 lines (58 loc) · 1.6 KB
/
Copy pathprinter.ts
File metadata and controls
60 lines (58 loc) · 1.6 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
60
import type { AstPath, Printer } from "prettier";
import {
canAttachComment,
handleLineComment,
handleRemainingComment,
isFullyBetweenPrettierIgnore,
isPrettierIgnore,
willPrintOwnComments
} from "./comments.ts";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.ts";
import {
embedTextBlock,
hasType,
printComment,
printValue,
type NamedNodePath
} from "./printers/helpers.ts";
import { printerForNodeType } from "./printers/index.ts";
export default {
print(path, options, print, args) {
return hasJavaNode(path)
? printerForNodeType(path.node.type)(path, print, options, args)
: printValue(path);
},
embed(path) {
return hasType(path, SyntaxType.StringLiteral)
? embedTextBlock(path)
: null;
},
hasPrettierIgnore(path) {
return (
path.node.comments?.some(isPrettierIgnore) === true ||
(canAttachComment(path.node) && isFullyBetweenPrettierIgnore(path))
);
},
canAttachComment,
isBlockComment(node) {
return (node as unknown as CommentNode).type === SyntaxType.BlockComment;
},
willPrintOwnComments,
printComment(commentPath) {
return printComment(commentPath.node as unknown as CommentNode);
},
getCommentChildNodes(node) {
return node.isNamed ? node.children : [];
},
handleComments: {
ownLine: handleLineComment,
endOfLine: handleLineComment,
remaining: handleRemainingComment
},
getVisitorKeys() {
return ["namedChildren"];
}
} satisfies Printer<SyntaxNode>;
function hasJavaNode(path: AstPath<SyntaxNode>): path is NamedNodePath {
return path.node.isNamed;
}