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,71 @@
1
+ import type { AnnotationCstNode, ClassPermitsCstNode, ClassTypeCtx, CstElement, CstNode, ExpressionCstNode, InterfacePermitsCstNode, IToken, StatementCstNode } from "java-parser";
2
+ import type { AstPath, Doc, ParserOptions } from "prettier";
3
+ import { builders } from "prettier/doc";
4
+ import type { JavaComment } from "../comments.js";
5
+ export declare function onlyDefinedKey<T extends Record<string, any>, K extends Key<T> & string>(obj: T, options?: K[]): K;
6
+ export declare function definedKeys<T extends Record<string, any>, K extends Key<T> & string>(obj: T, options?: K[]): K[];
7
+ export declare function printWithModifiers<T extends CstNode, P extends IterProperties<T["children"]>>(path: AstPath<T>, print: JavaPrintFn, modifierChild: P, contents: Doc, noTypeAnnotations?: boolean): builders.Doc[];
8
+ export declare function hasDeclarationAnnotations(modifiers: ModifierNode[]): boolean;
9
+ export declare function call<T extends CstNode, U, P extends IterProperties<T["children"]>>(path: AstPath<T>, callback: MapCallback<IndexValue<IndexValue<T, "children">, P>, U>, child: P, index?: number): U;
10
+ export declare function each<T extends CstNode, P extends IterProperties<T["children"]>>(path: AstPath<T>, callback: MapCallback<IndexValue<IndexValue<T, "children">, P>, void>, child: P): void;
11
+ export declare function map<T extends CstNode, U, P extends IterProperties<T["children"]>>(path: AstPath<T>, callback: MapCallback<IndexValue<IndexValue<T, "children">, P>, U>, child: P): U[];
12
+ export declare function flatMap<T extends CstNode, U, P extends IterProperties<T["children"]>>(path: AstPath<T>, callback: MapCallback<IndexValue<IndexValue<T, "children">, P>, U>, children: P[]): U[];
13
+ export declare function printSingle(path: AstPath<JavaNonTerminal>, print: JavaPrintFn, _?: JavaParserOptions, args?: unknown): builders.Doc;
14
+ export declare function lineStartWithComments(node: JavaNonTerminal): number;
15
+ export declare function lineEndWithComments(node: JavaNonTerminal): number;
16
+ export declare function printDanglingComments(path: AstPath<JavaNonTerminal>): builders.Doc[];
17
+ export declare function printComment(node: JavaTerminal): string | builders.Doc[];
18
+ export declare function hasLeadingComments(node: JavaNode): boolean | undefined;
19
+ export declare function indentInParentheses(contents: Doc, opts?: {
20
+ shouldBreak?: boolean;
21
+ }): builders.Group | "()";
22
+ export declare function printArrayInitializer<T extends JavaNonTerminal, P extends IterProperties<T["children"]>>(path: AstPath<T>, print: JavaPrintFn, options: JavaParserOptions, child: P): builders.Group | "{}";
23
+ export declare function printBlock(path: AstPath<JavaNonTerminal>, contents: Doc[]): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
24
+ export declare function printName(path: AstPath<JavaNonTerminal & {
25
+ children: {
26
+ Identifier: IToken[];
27
+ };
28
+ }>, print: JavaPrintFn): builders.Doc[];
29
+ export declare function printList<T extends JavaNonTerminal, P extends IterProperties<T["children"]>>(path: AstPath<T>, print: JavaPrintFn, child: P): builders.Doc[];
30
+ export declare function printClassPermits(path: AstPath<ClassPermitsCstNode | InterfacePermitsCstNode>, print: JavaPrintFn): builders.Group;
31
+ export declare function printClassType(path: AstPath<JavaNonTerminal & {
32
+ children: ClassTypeCtx;
33
+ }>, print: JavaPrintFn): builders.Doc[][];
34
+ export declare function isBinaryExpression(expression: ExpressionCstNode): boolean;
35
+ export declare function findBaseIndent(lines: string[]): number;
36
+ export declare function isEmptyStatement(statement: StatementCstNode): boolean;
37
+ export declare function isNonTerminal(node: CstElement): node is JavaNonTerminal;
38
+ export declare function isTerminal(node: CstElement): node is IToken;
39
+ export type JavaNode = CstElement & {
40
+ comments?: JavaComment[];
41
+ };
42
+ export type JavaNonTerminal = Exclude<JavaNode, IToken>;
43
+ export type JavaTerminal = Exclude<JavaNode, CstNode>;
44
+ export type JavaNodePrinters = {
45
+ [T in JavaNonTerminal["name"]]: JavaNodePrinter<T>;
46
+ };
47
+ export type JavaNodePrinter<T> = (path: AstPath<Extract<JavaNonTerminal, {
48
+ name: T;
49
+ }>>, print: JavaPrintFn, options: JavaParserOptions, args?: unknown) => Doc;
50
+ export type JavaPrintFn = (path: AstPath<JavaNode>, args?: unknown) => Doc;
51
+ export type JavaParserOptions = ParserOptions<JavaNode> & {
52
+ entrypoint?: string;
53
+ };
54
+ export type IterProperties<T> = T extends any[] ? IndexProperties<T> : ArrayProperties<T>;
55
+ type Key<T> = T extends T ? keyof T : never;
56
+ type ModifierNode = JavaNonTerminal & {
57
+ children: {
58
+ annotation?: AnnotationCstNode[];
59
+ };
60
+ };
61
+ type IsTuple<T> = T extends [] ? true : T extends [infer First, ...infer Remain] ? IsTuple<Remain> : false;
62
+ type IndexProperties<T extends {
63
+ length: number;
64
+ }> = IsTuple<T> extends true ? Exclude<Partial<T>["length"], T["length"]> : number;
65
+ type ArrayProperties<T> = {
66
+ [K in keyof T]: NonNullable<T[K]> extends readonly any[] ? K : never;
67
+ }[keyof T];
68
+ type ArrayElement<T> = T extends Array<infer E> ? E : never;
69
+ type MapCallback<T, U> = (path: AstPath<ArrayElement<T>>, index: number, value: any) => U;
70
+ type IndexValue<T, P> = T extends any[] ? P extends number ? T[P] : never : P extends keyof T ? T[P] : never;
71
+ export {};
@@ -0,0 +1,233 @@
1
+ import { builders } from "prettier/doc";
2
+ import parser from "../parser.js";
3
+ const { group, hardline, ifBreak, indent, join, line, softline } = builders;
4
+ export function onlyDefinedKey(obj, options) {
5
+ const keys = definedKeys(obj, options);
6
+ if (keys.length === 1) {
7
+ return keys[0];
8
+ }
9
+ throw new Error(keys.length > 1
10
+ ? `More than one defined key found: ${keys}`
11
+ : "No defined keys found");
12
+ }
13
+ export function definedKeys(obj, options) {
14
+ return (options !== null && options !== void 0 ? options : Object.keys(obj)).filter(key => obj[key] !== undefined);
15
+ }
16
+ const indexByModifier = [
17
+ "public",
18
+ "protected",
19
+ "private",
20
+ "abstract",
21
+ "default",
22
+ "static",
23
+ "final",
24
+ "transient",
25
+ "volatile",
26
+ "synchronized",
27
+ "native",
28
+ "sealed",
29
+ "non-sealed",
30
+ "strictfp"
31
+ ].reduce((map, name, index) => map.set(name, index), new Map());
32
+ export function printWithModifiers(path, print, modifierChild, contents, noTypeAnnotations = false) {
33
+ const declarationAnnotations = [];
34
+ const otherModifiers = [];
35
+ const typeAnnotations = [];
36
+ each(path, modifierPath => {
37
+ const { children } = modifierPath.node;
38
+ const modifier = print(modifierPath);
39
+ if (children.annotation) {
40
+ (otherModifiers.length ? typeAnnotations : declarationAnnotations).push(modifier);
41
+ }
42
+ else {
43
+ otherModifiers.push(modifier);
44
+ declarationAnnotations.push(...typeAnnotations);
45
+ typeAnnotations.length = 0;
46
+ }
47
+ }, modifierChild);
48
+ if (noTypeAnnotations) {
49
+ declarationAnnotations.push(...typeAnnotations);
50
+ typeAnnotations.length = 0;
51
+ }
52
+ otherModifiers.sort((a, b) => indexByModifier.get(a) - indexByModifier.get(b));
53
+ return join(hardline, [
54
+ ...declarationAnnotations,
55
+ join(" ", [...otherModifiers, ...typeAnnotations, contents])
56
+ ]);
57
+ }
58
+ export function hasDeclarationAnnotations(modifiers) {
59
+ let hasAnnotation = false;
60
+ let hasNonAnnotation = false;
61
+ for (const modifier of modifiers) {
62
+ if (modifier.children.annotation) {
63
+ hasAnnotation = true;
64
+ }
65
+ else if (hasAnnotation) {
66
+ return true;
67
+ }
68
+ else {
69
+ hasNonAnnotation = true;
70
+ }
71
+ }
72
+ return hasAnnotation && !hasNonAnnotation;
73
+ }
74
+ export function call(path, callback, child, index = 0) {
75
+ return path.map(callback, "children", child)[index];
76
+ }
77
+ export function each(path, callback, child) {
78
+ if (path.node.children[child]) {
79
+ path.each(callback, "children", child);
80
+ }
81
+ }
82
+ export function map(path, callback, child) {
83
+ return path.node.children[child] ? path.map(callback, "children", child) : [];
84
+ }
85
+ export function flatMap(path, callback, children) {
86
+ return children
87
+ .flatMap(child => map(path, callback, child).map((doc, index) => {
88
+ const node = path.node.children[child][index];
89
+ return {
90
+ doc,
91
+ startOffset: parser.locStart(node)
92
+ };
93
+ }))
94
+ .sort((a, b) => a.startOffset - b.startOffset)
95
+ .map(({ doc }) => doc);
96
+ }
97
+ export function printSingle(path, print, _, args) {
98
+ return call(path, childPath => print(childPath, args), onlyDefinedKey(path.node.children));
99
+ }
100
+ export function lineStartWithComments(node) {
101
+ const { comments, location } = node;
102
+ return comments
103
+ ? Math.min(location.startLine, comments[0].startLine)
104
+ : location.startLine;
105
+ }
106
+ export function lineEndWithComments(node) {
107
+ const { comments, location } = node;
108
+ return comments
109
+ ? Math.max(location.endLine, comments.at(-1).endLine)
110
+ : location.endLine;
111
+ }
112
+ export function printDanglingComments(path) {
113
+ if (!path.node.comments) {
114
+ return [];
115
+ }
116
+ const comments = [];
117
+ path.each(commentPath => {
118
+ const comment = commentPath.node;
119
+ if (comment.leading || comment.trailing) {
120
+ return;
121
+ }
122
+ comment.printed = true;
123
+ comments.push(printComment(comment));
124
+ }, "comments");
125
+ return join(hardline, comments);
126
+ }
127
+ export function printComment(node) {
128
+ const { image } = node;
129
+ const lines = image.split("\n").map(line => line.trim());
130
+ return lines.length > 1 &&
131
+ lines[0].startsWith("/*") &&
132
+ lines.slice(1).every(line => line.startsWith("*")) &&
133
+ lines.at(-1).endsWith("*/")
134
+ ? join(hardline, lines.map((line, index) => (index === 0 ? line : ` ${line}`)))
135
+ : image;
136
+ }
137
+ export function hasLeadingComments(node) {
138
+ var _a;
139
+ return (_a = node.comments) === null || _a === void 0 ? void 0 : _a.some(({ leading }) => leading);
140
+ }
141
+ export function indentInParentheses(contents, opts) {
142
+ return !Array.isArray(contents) || contents.length
143
+ ? group(["(", indent([softline, contents]), softline, ")"], opts)
144
+ : "()";
145
+ }
146
+ export function printArrayInitializer(path, print, options, child) {
147
+ const list = [];
148
+ if (child && child in path.node.children) {
149
+ list.push(call(path, print, child));
150
+ if (options.trailingComma !== "none") {
151
+ list.push(ifBreak(","));
152
+ }
153
+ }
154
+ list.push(...printDanglingComments(path));
155
+ return list.length ? group(["{", indent([line, ...list]), line, "}"]) : "{}";
156
+ }
157
+ export function printBlock(path, contents) {
158
+ if (!contents.length) {
159
+ const danglingComments = printDanglingComments(path);
160
+ return danglingComments.length
161
+ ? ["{", indent([hardline, ...danglingComments]), hardline, "}"]
162
+ : "{}";
163
+ }
164
+ return group([
165
+ "{",
166
+ indent([hardline, ...join(hardline, contents)]),
167
+ hardline,
168
+ "}"
169
+ ]);
170
+ }
171
+ export function printName(path, print) {
172
+ return join(".", map(path, print, "Identifier"));
173
+ }
174
+ export function printList(path, print, child) {
175
+ return join([",", line], map(path, print, child));
176
+ }
177
+ export function printClassPermits(path, print) {
178
+ return group([
179
+ "permits",
180
+ indent([line, group(printList(path, print, "typeName"))])
181
+ ]);
182
+ }
183
+ export function printClassType(path, print) {
184
+ return flatMap(path, childPath => {
185
+ const { node, isLast } = childPath;
186
+ const child = [print(childPath)];
187
+ if (isTerminal(node)) {
188
+ if (!isLast) {
189
+ child.push(".");
190
+ }
191
+ }
192
+ else if (node.name === "annotation") {
193
+ child.push(" ");
194
+ }
195
+ return child;
196
+ }, definedKeys(path.node.children, [
197
+ "annotation",
198
+ "Identifier",
199
+ "typeArguments"
200
+ ]));
201
+ }
202
+ export function isBinaryExpression(expression) {
203
+ var _a;
204
+ const conditionalExpression = (_a = expression.children.conditionalExpression) === null || _a === void 0 ? void 0 : _a[0].children;
205
+ if (!conditionalExpression) {
206
+ return false;
207
+ }
208
+ const isTernary = conditionalExpression.QuestionMark !== undefined;
209
+ if (isTernary) {
210
+ return false;
211
+ }
212
+ const hasNonAssignmentOperators = Object.values(conditionalExpression.binaryExpression[0].children).some(child => {
213
+ var _a;
214
+ return isTerminal(child[0]) &&
215
+ !((_a = child[0].tokenType.CATEGORIES) === null || _a === void 0 ? void 0 : _a.some(category => category.name === "AssignmentOperator"));
216
+ });
217
+ return hasNonAssignmentOperators;
218
+ }
219
+ export function findBaseIndent(lines) {
220
+ return lines.length
221
+ ? Math.min(...lines.map(line => line.search(/\S/)).filter(indent => indent >= 0))
222
+ : 0;
223
+ }
224
+ export function isEmptyStatement(statement) {
225
+ var _a;
226
+ return (((_a = statement.children.statementWithoutTrailingSubstatement) === null || _a === void 0 ? void 0 : _a[0].children.emptyStatement) !== undefined);
227
+ }
228
+ export function isNonTerminal(node) {
229
+ return !isTerminal(node);
230
+ }
231
+ export function isTerminal(node) {
232
+ return "tokenType" in node;
233
+ }
@@ -0,0 +1,2 @@
1
+ import type { JavaNodePrinter, JavaNodePrinters } from "./helpers.js";
2
+ export declare function printerForNodeType<T extends keyof JavaNodePrinters>(type: T): JavaNodePrinter<T>;
@@ -0,0 +1,13 @@
1
+ import arrays from "./arrays.js";
2
+ import blocksAndStatements from "./blocks-and-statements.js";
3
+ import classes from "./classes.js";
4
+ import expressions from "./expressions.js";
5
+ import interfaces from "./interfaces.js";
6
+ import lexicalStructure from "./lexical-structure.js";
7
+ import names from "./names.js";
8
+ import packagesAndModules from "./packages-and-modules.js";
9
+ import typesValuesAndVariables from "./types-values-and-variables.js";
10
+ const printersByNodeType = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, arrays), blocksAndStatements), classes), expressions), interfaces), lexicalStructure), names), packagesAndModules), typesValuesAndVariables);
11
+ export function printerForNodeType(type) {
12
+ return printersByNodeType[type];
13
+ }
@@ -0,0 +1,62 @@
1
+ import { builders } from "prettier/doc";
2
+ import { printClassPermits, printSingle } from "./helpers.js";
3
+ declare const _default: {
4
+ interfaceDeclaration(path: import("prettier").AstPath<import("../../../java-parser/api.js").InterfaceDeclarationCstNode & {
5
+ comments?: import("../comments.js").JavaComment[];
6
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
7
+ normalInterfaceDeclaration(path: import("prettier").AstPath<import("../../../java-parser/api.js").NormalInterfaceDeclarationCstNode & {
8
+ comments?: import("../comments.js").JavaComment[];
9
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
10
+ interfaceModifier: typeof printSingle;
11
+ interfaceExtends(path: import("prettier").AstPath<import("../../../java-parser/api.js").InterfaceExtendsCstNode & {
12
+ comments?: import("../comments.js").JavaComment[];
13
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group;
14
+ interfacePermits: typeof printClassPermits;
15
+ interfaceBody(path: import("prettier").AstPath<import("../../../java-parser/api.js").InterfaceBodyCstNode & {
16
+ comments?: import("../comments.js").JavaComment[];
17
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
18
+ interfaceMemberDeclaration(path: import("prettier").AstPath<import("../../../java-parser/api.js").InterfaceMemberDeclarationCstNode & {
19
+ comments?: import("../comments.js").JavaComment[];
20
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc;
21
+ constantDeclaration(path: import("prettier").AstPath<import("../../../java-parser/api.js").ConstantDeclarationCstNode & {
22
+ comments?: import("../comments.js").JavaComment[];
23
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
24
+ constantModifier: typeof printSingle;
25
+ interfaceMethodDeclaration(path: import("prettier").AstPath<import("../../../java-parser/api.js").InterfaceMethodDeclarationCstNode & {
26
+ comments?: import("../comments.js").JavaComment[];
27
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
28
+ interfaceMethodModifier: typeof printSingle;
29
+ annotationInterfaceDeclaration(path: import("prettier").AstPath<import("../../../java-parser/api.js").AnnotationInterfaceDeclarationCstNode & {
30
+ comments?: import("../comments.js").JavaComment[];
31
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
32
+ annotationInterfaceBody(path: import("prettier").AstPath<import("../../../java-parser/api.js").AnnotationInterfaceBodyCstNode & {
33
+ comments?: import("../comments.js").JavaComment[];
34
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
35
+ annotationInterfaceMemberDeclaration(path: import("prettier").AstPath<import("../../../java-parser/api.js").AnnotationInterfaceMemberDeclarationCstNode & {
36
+ comments?: import("../comments.js").JavaComment[];
37
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc;
38
+ annotationInterfaceElementDeclaration(path: import("prettier").AstPath<import("../../../java-parser/api.js").AnnotationInterfaceElementDeclarationCstNode & {
39
+ comments?: import("../comments.js").JavaComment[];
40
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
41
+ annotationInterfaceElementModifier: typeof printSingle;
42
+ defaultValue(path: import("prettier").AstPath<import("../../../java-parser/api.js").DefaultValueCstNode & {
43
+ comments?: import("../comments.js").JavaComment[];
44
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
45
+ annotation(path: import("prettier").AstPath<import("../../../java-parser/api.js").AnnotationCstNode & {
46
+ comments?: import("../comments.js").JavaComment[];
47
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
48
+ elementValuePairList(path: import("prettier").AstPath<import("../../../java-parser/api.js").ElementValuePairListCstNode & {
49
+ comments?: import("../comments.js").JavaComment[];
50
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
51
+ elementValuePair(path: import("prettier").AstPath<import("../../../java-parser/api.js").ElementValuePairCstNode & {
52
+ comments?: import("../comments.js").JavaComment[];
53
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
54
+ elementValue: typeof printSingle;
55
+ elementValueArrayInitializer(path: import("prettier").AstPath<import("../../../java-parser/api.js").ElementValueArrayInitializerCstNode & {
56
+ comments?: import("../comments.js").JavaComment[];
57
+ }>, print: import("./helpers.js").JavaPrintFn, options: import("./helpers.js").JavaParserOptions): builders.Group | "{}";
58
+ elementValueList(path: import("prettier").AstPath<import("../../../java-parser/api.js").ElementValueListCstNode & {
59
+ comments?: import("../comments.js").JavaComment[];
60
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group;
61
+ };
62
+ export default _default;