prettier-plugin-java 2.6.8 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/comments.d.ts +17 -0
  2. package/dist/comments.js +199 -0
  3. package/dist/index.d.ts +543 -0
  4. package/dist/index.js +26 -63
  5. package/dist/options.d.ts +23 -0
  6. package/dist/options.js +247 -239
  7. package/dist/parser.d.ts +9 -0
  8. package/dist/parser.js +24 -4
  9. package/dist/printer.d.ts +18 -0
  10. package/dist/printer.js +39 -5
  11. package/dist/printers/arrays.d.ts +9 -0
  12. package/dist/printers/arrays.js +8 -24
  13. package/dist/printers/blocks-and-statements.d.ts +117 -0
  14. package/dist/printers/blocks-and-statements.js +316 -412
  15. package/dist/printers/classes.d.ts +157 -0
  16. package/dist/printers/classes.js +422 -685
  17. package/dist/printers/expressions.d.ts +134 -0
  18. package/dist/printers/expressions.js +549 -555
  19. package/dist/printers/helpers.d.ts +71 -0
  20. package/dist/printers/helpers.js +233 -0
  21. package/dist/printers/index.d.ts +2 -0
  22. package/dist/printers/index.js +13 -0
  23. package/dist/printers/interfaces.d.ts +62 -0
  24. package/dist/printers/interfaces.js +146 -211
  25. package/dist/printers/lexical-structure.d.ts +14 -0
  26. package/dist/printers/lexical-structure.js +26 -28
  27. package/dist/printers/names.d.ts +12 -0
  28. package/dist/printers/names.js +11 -29
  29. package/dist/printers/packages-and-modules.d.ts +46 -0
  30. package/dist/printers/packages-and-modules.js +157 -159
  31. package/dist/printers/types-values-and-variables.d.ts +46 -0
  32. package/dist/printers/types-values-and-variables.js +86 -149
  33. package/package.json +5 -8
  34. package/dist/base-cst-printer.js +0 -55
  35. package/dist/cst-printer.js +0 -29
  36. package/dist/printers/comments/comments-utils.js +0 -21
  37. package/dist/printers/comments/format-comments.js +0 -171
  38. package/dist/printers/comments/handle-comments.js +0 -102
  39. package/dist/printers/prettier-builder.js +0 -45
  40. package/dist/printers/printer-utils.js +0 -598
  41. package/dist/types/utils.js +0 -20
  42. package/dist/utils/index.js +0 -2
  43. package/dist/utils/isEmptyDoc.js +0 -4
  44. package/dist/utils/printArgumentListWithBraces.js +0 -37
  45. package/index.d.ts +0 -4
@@ -0,0 +1,17 @@
1
+ import type { IToken } from "java-parser";
2
+ import { type AstPath } from "prettier";
3
+ import { type JavaNode, type JavaNonTerminal, type JavaParserOptions } from "./printers/helpers.js";
4
+ export declare function determineFormatterOffOnRanges(cst: JavaNonTerminal): void;
5
+ export declare function isFullyBetweenFormatterOffOn(path: AstPath<JavaNode>): boolean;
6
+ export declare function canAttachComment(node: JavaNode): boolean;
7
+ export declare function handleLineComment(commentNode: JavaComment, _: string, options: JavaParserOptions): boolean;
8
+ export declare function handleRemainingComment(commentNode: JavaComment): boolean;
9
+ export type JavaComment = IToken & {
10
+ value: string;
11
+ leading: boolean;
12
+ trailing: boolean;
13
+ printed: boolean;
14
+ enclosingNode?: JavaNonTerminal;
15
+ precedingNode?: JavaNonTerminal;
16
+ followingNode?: JavaNonTerminal;
17
+ };
@@ -0,0 +1,199 @@
1
+ import { util } from "prettier";
2
+ import parser from "./parser.js";
3
+ import { isEmptyStatement, isNonTerminal, isTerminal } from "./printers/helpers.js";
4
+ const formatterOffOnRangesByCst = new WeakMap();
5
+ export function determineFormatterOffOnRanges(cst) {
6
+ const { comments } = cst;
7
+ if (!comments) {
8
+ return;
9
+ }
10
+ const ranges = comments
11
+ .filter(({ image }) => /^(\/\/\s*@formatter:(off|on)\s*|\/\*\s*@formatter:(off|on)\s*\*\/)$/.test(image))
12
+ .reduce((ranges, { image, startOffset }) => {
13
+ const previous = ranges.at(-1);
14
+ if (image.endsWith("off")) {
15
+ if ((previous === null || previous === void 0 ? void 0 : previous.on) !== Infinity) {
16
+ ranges.push({ off: startOffset, on: Infinity });
17
+ }
18
+ }
19
+ else if ((previous === null || previous === void 0 ? void 0 : previous.on) === Infinity) {
20
+ previous.on = startOffset;
21
+ }
22
+ return ranges;
23
+ }, new Array());
24
+ formatterOffOnRangesByCst.set(cst, ranges);
25
+ }
26
+ export function isFullyBetweenFormatterOffOn(path) {
27
+ var _a;
28
+ const { node, root } = path;
29
+ const start = parser.locStart(node);
30
+ const end = parser.locEnd(node);
31
+ return (((_a = formatterOffOnRangesByCst
32
+ .get(root)) === null || _a === void 0 ? void 0 : _a.some(range => range.off < start && end < range.on)) === true);
33
+ }
34
+ export function canAttachComment(node) {
35
+ var _a, _b, _c;
36
+ if (isTerminal(node)) {
37
+ const { name, CATEGORIES } = node.tokenType;
38
+ return (name === "Identifier" ||
39
+ (CATEGORIES === null || CATEGORIES === void 0 ? void 0 : CATEGORIES.find(({ name }) => name === "BinaryOperator")) !== undefined);
40
+ }
41
+ const { children, name } = node;
42
+ switch (name) {
43
+ case "argumentList":
44
+ case "blockStatements":
45
+ case "emptyStatement":
46
+ case "enumBodyDeclarations":
47
+ return false;
48
+ case "annotationInterfaceMemberDeclaration":
49
+ case "classMemberDeclaration":
50
+ case "interfaceMemberDeclaration":
51
+ case "methodBody":
52
+ return !children.Semicolon;
53
+ case "blockStatement":
54
+ return !children.statement || !isEmptyStatement(children.statement[0]);
55
+ case "classBodyDeclaration":
56
+ return !((_a = children.classMemberDeclaration) === null || _a === void 0 ? void 0 : _a[0].children.Semicolon);
57
+ case "recordBodyDeclaration":
58
+ return !((_c = (_b = children.classBodyDeclaration) === null || _b === void 0 ? void 0 : _b[0].children.classMemberDeclaration) === null || _c === void 0 ? void 0 : _c[0].children.Semicolon);
59
+ case "statement":
60
+ return !isEmptyStatement(node);
61
+ case "statementWithoutTrailingSubstatement":
62
+ return !children.emptyStatement;
63
+ default:
64
+ return true;
65
+ }
66
+ }
67
+ export function handleLineComment(commentNode, _, options) {
68
+ return [
69
+ handleBinaryExpressionComments,
70
+ handleFqnOrRefTypeComments,
71
+ handleIfStatementComments,
72
+ handleJumpStatementComments,
73
+ handleLabeledStatementComments,
74
+ handleNameComments
75
+ ].some(fn => fn(commentNode, options));
76
+ }
77
+ export function handleRemainingComment(commentNode) {
78
+ return [
79
+ handleFqnOrRefTypeComments,
80
+ handleMethodDeclaratorComments,
81
+ handleNameComments,
82
+ handleJumpStatementComments
83
+ ].some(fn => fn(commentNode));
84
+ }
85
+ function handleBinaryExpressionComments(commentNode, options) {
86
+ const { enclosingNode, precedingNode, followingNode } = commentNode;
87
+ if (enclosingNode &&
88
+ isNonTerminal(enclosingNode) &&
89
+ enclosingNode.name === "binaryExpression") {
90
+ if (isBinaryOperator(followingNode)) {
91
+ if (options.experimentalOperatorPosition === "start") {
92
+ util.addLeadingComment(followingNode, commentNode);
93
+ }
94
+ else {
95
+ util.addTrailingComment(followingNode, commentNode);
96
+ }
97
+ return true;
98
+ }
99
+ else if (options.experimentalOperatorPosition === "start" &&
100
+ isBinaryOperator(precedingNode)) {
101
+ util.addLeadingComment(precedingNode, commentNode);
102
+ return true;
103
+ }
104
+ }
105
+ return false;
106
+ }
107
+ function handleFqnOrRefTypeComments(commentNode) {
108
+ const { enclosingNode, followingNode } = commentNode;
109
+ if (enclosingNode &&
110
+ isNonTerminal(enclosingNode) &&
111
+ enclosingNode.name === "fqnOrRefType" &&
112
+ followingNode) {
113
+ util.addLeadingComment(followingNode, commentNode);
114
+ return true;
115
+ }
116
+ return false;
117
+ }
118
+ function handleIfStatementComments(commentNode) {
119
+ const { enclosingNode, precedingNode } = commentNode;
120
+ if (enclosingNode &&
121
+ isNonTerminal(enclosingNode) &&
122
+ enclosingNode.name === "ifStatement" &&
123
+ precedingNode &&
124
+ isNonTerminal(precedingNode) &&
125
+ precedingNode.name === "statement") {
126
+ util.addDanglingComment(enclosingNode, commentNode, undefined);
127
+ return true;
128
+ }
129
+ return false;
130
+ }
131
+ function handleJumpStatementComments(commentNode) {
132
+ const { enclosingNode, precedingNode, followingNode } = commentNode;
133
+ if (enclosingNode &&
134
+ !precedingNode &&
135
+ !followingNode &&
136
+ isNonTerminal(enclosingNode) &&
137
+ ["breakStatement", "continueStatement", "returnStatement"].includes(enclosingNode.name)) {
138
+ util.addTrailingComment(enclosingNode, commentNode);
139
+ return true;
140
+ }
141
+ return false;
142
+ }
143
+ function handleLabeledStatementComments(commentNode) {
144
+ const { enclosingNode, precedingNode } = commentNode;
145
+ if (enclosingNode &&
146
+ precedingNode &&
147
+ isNonTerminal(enclosingNode) &&
148
+ enclosingNode.name === "labeledStatement" &&
149
+ isTerminal(precedingNode) &&
150
+ precedingNode.tokenType.name === "Identifier") {
151
+ util.addLeadingComment(precedingNode, commentNode);
152
+ return true;
153
+ }
154
+ return false;
155
+ }
156
+ function handleMethodDeclaratorComments(commentNode) {
157
+ const { enclosingNode } = commentNode;
158
+ if (enclosingNode &&
159
+ isNonTerminal(enclosingNode) &&
160
+ enclosingNode.name === "methodDeclarator" &&
161
+ !enclosingNode.children.receiverParameter &&
162
+ !enclosingNode.children.formalParameterList &&
163
+ enclosingNode.children.LBrace[0].startOffset < commentNode.startOffset &&
164
+ commentNode.startOffset < enclosingNode.children.RBrace[0].startOffset) {
165
+ util.addDanglingComment(enclosingNode, commentNode, undefined);
166
+ return true;
167
+ }
168
+ return false;
169
+ }
170
+ function handleNameComments(commentNode) {
171
+ const { enclosingNode, precedingNode } = commentNode;
172
+ if (enclosingNode &&
173
+ precedingNode &&
174
+ isNonTerminal(enclosingNode) &&
175
+ isTerminal(precedingNode) &&
176
+ precedingNode.tokenType.name === "Identifier" &&
177
+ [
178
+ "ambiguousName",
179
+ "classOrInterfaceTypeToInstantiate",
180
+ "expressionName",
181
+ "moduleDeclaration",
182
+ "moduleName",
183
+ "packageDeclaration",
184
+ "packageName",
185
+ "packageOrTypeName",
186
+ "typeName"
187
+ ].includes(enclosingNode.name)) {
188
+ util.addTrailingComment(precedingNode, commentNode);
189
+ return true;
190
+ }
191
+ return false;
192
+ }
193
+ function isBinaryOperator(node) {
194
+ var _a;
195
+ return (node !== undefined &&
196
+ (isNonTerminal(node)
197
+ ? node.name === "shiftOperator"
198
+ : (_a = node.tokenType.CATEGORIES) === null || _a === void 0 ? void 0 : _a.some(({ name }) => name === "BinaryOperator")));
199
+ }