prettier-plugin-java 2.7.5 → 2.7.6

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.
@@ -12,6 +12,6 @@ export type JavaComment = IToken & {
12
12
  trailing: boolean;
13
13
  printed: boolean;
14
14
  enclosingNode?: JavaNonTerminal;
15
- precedingNode?: JavaNonTerminal;
16
- followingNode?: JavaNonTerminal;
15
+ precedingNode?: JavaNode;
16
+ followingNode?: JavaNode;
17
17
  };
package/dist/comments.js CHANGED
@@ -67,6 +67,7 @@ export function canAttachComment(node) {
67
67
  export function handleLineComment(commentNode, _, options) {
68
68
  return [
69
69
  handleBinaryExpressionComments,
70
+ handleConditionalExpressionComments,
70
71
  handleFqnOrRefTypeComments,
71
72
  handleIfStatementComments,
72
73
  handleJumpStatementComments,
@@ -84,9 +85,7 @@ export function handleRemainingComment(commentNode) {
84
85
  }
85
86
  function handleBinaryExpressionComments(commentNode, options) {
86
87
  const { enclosingNode, precedingNode, followingNode } = commentNode;
87
- if (enclosingNode &&
88
- isNonTerminal(enclosingNode) &&
89
- enclosingNode.name === "binaryExpression") {
88
+ if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.name) === "binaryExpression") {
90
89
  if (isBinaryOperator(followingNode)) {
91
90
  if (options.experimentalOperatorPosition === "start") {
92
91
  util.addLeadingComment(followingNode, commentNode);
@@ -104,12 +103,23 @@ function handleBinaryExpressionComments(commentNode, options) {
104
103
  }
105
104
  return false;
106
105
  }
106
+ function handleConditionalExpressionComments(commentNode) {
107
+ const { startLine, endLine, enclosingNode, precedingNode, followingNode } = commentNode;
108
+ if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.name) === "conditionalExpression" &&
109
+ precedingNode &&
110
+ followingNode &&
111
+ isNonTerminal(precedingNode) &&
112
+ isNonTerminal(followingNode) &&
113
+ precedingNode.location.endLine < startLine &&
114
+ endLine < followingNode.location.startLine) {
115
+ util.addLeadingComment(followingNode, commentNode);
116
+ return true;
117
+ }
118
+ return false;
119
+ }
107
120
  function handleFqnOrRefTypeComments(commentNode) {
108
121
  const { enclosingNode, followingNode } = commentNode;
109
- if (enclosingNode &&
110
- isNonTerminal(enclosingNode) &&
111
- enclosingNode.name === "fqnOrRefType" &&
112
- followingNode) {
122
+ if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.name) === "fqnOrRefType" && followingNode) {
113
123
  util.addLeadingComment(followingNode, commentNode);
114
124
  return true;
115
125
  }
@@ -117,9 +127,7 @@ function handleFqnOrRefTypeComments(commentNode) {
117
127
  }
118
128
  function handleIfStatementComments(commentNode) {
119
129
  const { enclosingNode, precedingNode } = commentNode;
120
- if (enclosingNode &&
121
- isNonTerminal(enclosingNode) &&
122
- enclosingNode.name === "ifStatement" &&
130
+ if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.name) === "ifStatement" &&
123
131
  precedingNode &&
124
132
  isNonTerminal(precedingNode) &&
125
133
  precedingNode.name === "statement") {
@@ -133,7 +141,6 @@ function handleJumpStatementComments(commentNode) {
133
141
  if (enclosingNode &&
134
142
  !precedingNode &&
135
143
  !followingNode &&
136
- isNonTerminal(enclosingNode) &&
137
144
  ["breakStatement", "continueStatement", "returnStatement"].includes(enclosingNode.name)) {
138
145
  util.addTrailingComment(enclosingNode, commentNode);
139
146
  return true;
@@ -142,10 +149,8 @@ function handleJumpStatementComments(commentNode) {
142
149
  }
143
150
  function handleLabeledStatementComments(commentNode) {
144
151
  const { enclosingNode, precedingNode } = commentNode;
145
- if (enclosingNode &&
152
+ if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.name) === "labeledStatement" &&
146
153
  precedingNode &&
147
- isNonTerminal(enclosingNode) &&
148
- enclosingNode.name === "labeledStatement" &&
149
154
  isTerminal(precedingNode) &&
150
155
  precedingNode.tokenType.name === "Identifier") {
151
156
  util.addLeadingComment(precedingNode, commentNode);
@@ -155,9 +160,7 @@ function handleLabeledStatementComments(commentNode) {
155
160
  }
156
161
  function handleMethodDeclaratorComments(commentNode) {
157
162
  const { enclosingNode } = commentNode;
158
- if (enclosingNode &&
159
- isNonTerminal(enclosingNode) &&
160
- enclosingNode.name === "methodDeclarator" &&
163
+ if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.name) === "methodDeclarator" &&
161
164
  !enclosingNode.children.receiverParameter &&
162
165
  !enclosingNode.children.formalParameterList &&
163
166
  enclosingNode.children.LBrace[0].startOffset < commentNode.startOffset &&
@@ -171,7 +174,6 @@ function handleNameComments(commentNode) {
171
174
  const { enclosingNode, precedingNode } = commentNode;
172
175
  if (enclosingNode &&
173
176
  precedingNode &&
174
- isNonTerminal(enclosingNode) &&
175
177
  isTerminal(precedingNode) &&
176
178
  precedingNode.tokenType.name === "Identifier" &&
177
179
  [
@@ -1,7 +1,7 @@
1
1
  declare const _default: {
2
2
  arrayInitializer(path: import("prettier").AstPath<import("java-parser").ArrayInitializerCstNode & {
3
3
  comments?: import("../comments.js").JavaComment[];
4
- }>, print: import("./helpers.js").JavaPrintFn, options: import("./helpers.js").JavaParserOptions): import("prettier/doc.js").builders.Group | "{}";
4
+ }>, print: import("./helpers.js").JavaPrintFn, options: import("./helpers.js").JavaParserOptions): import("prettier/doc.js").builders.Group | (string | import("prettier/doc.js").builders.Indent | import("prettier/doc.js").builders.Hardline)[] | "{}";
5
5
  variableInitializerList(path: import("prettier").AstPath<import("java-parser").VariableInitializerListCstNode & {
6
6
  comments?: import("../comments.js").JavaComment[];
7
7
  }>, print: import("./helpers.js").JavaPrintFn): import("prettier/doc.js").builders.Doc[];
@@ -3,7 +3,7 @@ import { printSingle } from "./helpers.js";
3
3
  declare const _default: {
4
4
  block(path: import("prettier").AstPath<import("java-parser").BlockCstNode & {
5
5
  comments?: import("../comments.js").JavaComment[];
6
- }>, print: import("./helpers.js").JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
6
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
7
7
  blockStatements(path: import("prettier").AstPath<import("java-parser").BlockStatementsCstNode & {
8
8
  comments?: import("../comments.js").JavaComment[];
9
9
  }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
@@ -36,7 +36,7 @@ declare const _default: {
36
36
  }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
37
37
  switchBlock(path: import("prettier").AstPath<import("java-parser").SwitchBlockCstNode & {
38
38
  comments?: import("../comments.js").JavaComment[];
39
- }>, print: import("./helpers.js").JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
39
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
40
40
  switchBlockStatementGroup(path: import("prettier").AstPath<import("java-parser").SwitchBlockStatementGroupCstNode & {
41
41
  comments?: import("../comments.js").JavaComment[];
42
42
  }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
@@ -1,5 +1,5 @@
1
1
  import { builders } from "prettier/doc";
2
- import { call, definedKeys, indentInParentheses, isBinaryExpression, isEmptyStatement, lineEndWithComments, lineStartWithComments, map, onlyDefinedKey, printBlock, printDanglingComments, printSingle, printWithModifiers } from "./helpers.js";
2
+ import { call, definedKeys, hasLeadingComments, indentInParentheses, isBinaryExpression, isEmptyStatement, lineEndWithComments, lineStartWithComments, map, onlyDefinedKey, printBlock, printDanglingComments, printSingle, printWithModifiers } from "./helpers.js";
3
3
  const { group, hardline, ifBreak, indent, join, line, softline } = builders;
4
4
  export default {
5
5
  block(path, print) {
@@ -148,11 +148,14 @@ export default {
148
148
  "expression",
149
149
  "throwStatement"
150
150
  ]);
151
- const parts = [
152
- call(path, print, "switchLabel"),
153
- " -> ",
154
- call(path, print, bodyKey)
155
- ];
151
+ const body = call(path, print, bodyKey);
152
+ const parts = [call(path, print, "switchLabel"), " ->"];
153
+ if (bodyKey !== "block" && hasLeadingComments(children[bodyKey][0])) {
154
+ parts.push(indent([hardline, body]));
155
+ }
156
+ else {
157
+ parts.push(" ", body);
158
+ }
156
159
  if (children.Semicolon) {
157
160
  parts.push(";");
158
161
  }
@@ -28,7 +28,7 @@ declare const _default: {
28
28
  }>, print: JavaPrintFn): builders.Group;
29
29
  classBody(path: AstPath<ClassBodyCstNode & {
30
30
  comments?: import("../comments.js").JavaComment[];
31
- }>, print: JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
31
+ }>, print: JavaPrintFn): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
32
32
  classBodyDeclaration: typeof printSingle;
33
33
  classMemberDeclaration(path: AstPath<import("java-parser").ClassMemberDeclarationCstNode & {
34
34
  comments?: import("../comments.js").JavaComment[];
@@ -106,7 +106,7 @@ declare const _default: {
106
106
  simpleTypeName: typeof printSingle;
107
107
  constructorBody(path: AstPath<import("java-parser").ConstructorBodyCstNode & {
108
108
  comments?: import("../comments.js").JavaComment[];
109
- }>, print: JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
109
+ }>, print: JavaPrintFn): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
110
110
  explicitConstructorInvocation: typeof printSingle;
111
111
  unqualifiedExplicitConstructorInvocation(path: AstPath<import("java-parser").UnqualifiedExplicitConstructorInvocationCstNode & {
112
112
  comments?: import("../comments.js").JavaComment[];
@@ -119,7 +119,7 @@ declare const _default: {
119
119
  }>, print: JavaPrintFn): builders.Doc[];
120
120
  enumBody(path: AstPath<import("java-parser").EnumBodyCstNode & {
121
121
  comments?: import("../comments.js").JavaComment[];
122
- }>, print: JavaPrintFn, options: import("./helpers.js").JavaParserOptions): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
122
+ }>, print: JavaPrintFn, options: import("./helpers.js").JavaParserOptions): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
123
123
  enumConstantList(path: AstPath<import("java-parser").EnumConstantListCstNode & {
124
124
  comments?: import("../comments.js").JavaComment[];
125
125
  }>, print: JavaPrintFn): builders.Doc[];
@@ -148,7 +148,7 @@ declare const _default: {
148
148
  recordComponentModifier: typeof printSingle;
149
149
  recordBody(path: AstPath<import("java-parser").RecordBodyCstNode & {
150
150
  comments?: import("../comments.js").JavaComment[];
151
- }>, print: JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
151
+ }>, print: JavaPrintFn): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
152
152
  recordBodyDeclaration: typeof printSingle;
153
153
  compactConstructorDeclaration(path: AstPath<import("java-parser").CompactConstructorDeclarationCstNode & {
154
154
  comments?: import("../comments.js").JavaComment[];
@@ -30,7 +30,7 @@ declare const _default: {
30
30
  lambdaBody: typeof printSingle;
31
31
  conditionalExpression(path: AstPath<import("java-parser").ConditionalExpressionCstNode & {
32
32
  comments?: JavaComment[];
33
- }>, print: JavaPrintFn): builders.Doc;
33
+ }>, print: JavaPrintFn, options: import("./helpers.js").JavaParserOptions): builders.Doc;
34
34
  binaryExpression(path: AstPath<import("java-parser").BinaryExpressionCstNode & {
35
35
  comments?: JavaComment[];
36
36
  }>, print: JavaPrintFn, options: import("./helpers.js").JavaParserOptions): builders.Doc;
@@ -1,6 +1,6 @@
1
1
  import { builders, utils } from "prettier/doc";
2
2
  import { call, definedKeys, each, findBaseIndent, flatMap, hasLeadingComments, indentInParentheses, isBinaryExpression, isNonTerminal, isTerminal, map, onlyDefinedKey, printDanglingComments, printList, printName, printSingle } from "./helpers.js";
3
- const { breakParent, conditionalGroup, group, hardline, ifBreak, indent, indentIfBreak, join, line, lineSuffixBoundary, softline } = builders;
3
+ const { align, breakParent, conditionalGroup, group, hardline, ifBreak, indent, indentIfBreak, join, line, lineSuffixBoundary, softline } = builders;
4
4
  const { removeLines, willBreak } = utils;
5
5
  export default {
6
6
  expression: printSingle,
@@ -61,21 +61,26 @@ export default {
61
61
  lambdaParameterType: printSingle,
62
62
  conciseLambdaParameter: printSingle,
63
63
  lambdaBody: printSingle,
64
- conditionalExpression(path, print) {
64
+ conditionalExpression(path, print, options) {
65
65
  var _a;
66
66
  const binaryExpression = call(path, print, "binaryExpression");
67
67
  if (!path.node.children.QuestionMark) {
68
68
  return binaryExpression;
69
69
  }
70
- const expressions = map(path, print, "expression");
71
- const contents = indent(join(line, [
72
- binaryExpression,
73
- ["? ", expressions[0]],
74
- [": ", expressions[1]]
75
- ]));
70
+ const [consequent, alternate] = map(path, print, "expression");
71
+ const parts = [binaryExpression];
72
+ const part = [
73
+ line,
74
+ ["? ", options.useTabs ? indent(consequent) : align(2, consequent)],
75
+ line,
76
+ [": ", options.useTabs ? indent(alternate) : align(2, alternate)]
77
+ ];
76
78
  const isNestedTernary = ((_a = path.getNode(4)) === null || _a === void 0 ? void 0 : _a.name) ===
77
79
  "conditionalExpression";
78
- return isNestedTernary ? contents : group(contents);
80
+ parts.push(!isNestedTernary || options.useTabs
81
+ ? part
82
+ : align(Math.max(0, options.tabWidth - 2), part));
83
+ return isNestedTernary ? parts : group(indent(parts));
79
84
  },
80
85
  binaryExpression(path, print, options) {
81
86
  var _a, _b;
@@ -15,12 +15,12 @@ export declare function lineStartWithComments(node: JavaNonTerminal): number;
15
15
  export declare function lineEndWithComments(node: JavaNonTerminal): number;
16
16
  export declare function printDanglingComments(path: AstPath<JavaNonTerminal>): builders.Doc[];
17
17
  export declare function printComment(node: JavaTerminal): string | builders.Doc[];
18
- export declare function hasLeadingComments(node: JavaNode): boolean | undefined;
18
+ export declare function hasLeadingComments(node: JavaNode): boolean;
19
19
  export declare function indentInParentheses(contents: Doc, opts?: {
20
20
  shouldBreak?: boolean;
21
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)[];
22
+ export declare function printArrayInitializer<T extends JavaNonTerminal, P extends IterProperties<T["children"]>>(path: AstPath<T>, print: JavaPrintFn, options: JavaParserOptions, child: P): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
23
+ export declare function printBlock(path: AstPath<JavaNonTerminal>, contents: Doc[]): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
24
24
  export declare function printName(path: AstPath<JavaNonTerminal & {
25
25
  children: {
26
26
  Identifier: IToken[];
@@ -135,8 +135,8 @@ export function printComment(node) {
135
135
  : image;
136
136
  }
137
137
  export function hasLeadingComments(node) {
138
- var _a;
139
- return (_a = node.comments) === null || _a === void 0 ? void 0 : _a.some(({ leading }) => leading);
138
+ var _a, _b;
139
+ return (_b = (_a = node.comments) === null || _a === void 0 ? void 0 : _a.some(({ leading }) => leading)) !== null && _b !== void 0 ? _b : false;
140
140
  }
141
141
  export function indentInParentheses(contents, opts) {
142
142
  return !Array.isArray(contents) || contents.length
@@ -144,14 +144,16 @@ export function indentInParentheses(contents, opts) {
144
144
  : "()";
145
145
  }
146
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
- }
147
+ if (!(child && child in path.node.children)) {
148
+ const danglingComments = printDanglingComments(path);
149
+ return danglingComments.length
150
+ ? ["{", indent([hardline, ...danglingComments]), hardline, "}"]
151
+ : "{}";
152
+ }
153
+ const list = [call(path, print, child)];
154
+ if (options.trailingComma !== "none") {
155
+ list.push(ifBreak(","));
153
156
  }
154
- list.push(...printDanglingComments(path));
155
157
  return list.length ? group(["{", indent([line, ...list]), line, "}"]) : "{}";
156
158
  }
157
159
  export function printBlock(path, contents) {
@@ -14,7 +14,7 @@ declare const _default: {
14
14
  interfacePermits: typeof printClassPermits;
15
15
  interfaceBody(path: import("prettier").AstPath<import("java-parser").InterfaceBodyCstNode & {
16
16
  comments?: import("../comments.js").JavaComment[];
17
- }>, print: import("./helpers.js").JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
17
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
18
18
  interfaceMemberDeclaration(path: import("prettier").AstPath<import("java-parser").InterfaceMemberDeclarationCstNode & {
19
19
  comments?: import("../comments.js").JavaComment[];
20
20
  }>, print: import("./helpers.js").JavaPrintFn): builders.Doc;
@@ -31,7 +31,7 @@ declare const _default: {
31
31
  }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
32
32
  annotationInterfaceBody(path: import("prettier").AstPath<import("java-parser").AnnotationInterfaceBodyCstNode & {
33
33
  comments?: import("../comments.js").JavaComment[];
34
- }>, print: import("./helpers.js").JavaPrintFn): builders.Group | "{}" | (string | builders.Indent | builders.Hardline)[];
34
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
35
35
  annotationInterfaceMemberDeclaration(path: import("prettier").AstPath<import("java-parser").AnnotationInterfaceMemberDeclarationCstNode & {
36
36
  comments?: import("../comments.js").JavaComment[];
37
37
  }>, print: import("./helpers.js").JavaPrintFn): builders.Doc;
@@ -54,7 +54,7 @@ declare const _default: {
54
54
  elementValue: typeof printSingle;
55
55
  elementValueArrayInitializer(path: import("prettier").AstPath<import("java-parser").ElementValueArrayInitializerCstNode & {
56
56
  comments?: import("../comments.js").JavaComment[];
57
- }>, print: import("./helpers.js").JavaPrintFn, options: import("./helpers.js").JavaParserOptions): builders.Group | "{}";
57
+ }>, print: import("./helpers.js").JavaPrintFn, options: import("./helpers.js").JavaParserOptions): builders.Group | (string | builders.Indent | builders.Hardline)[] | "{}";
58
58
  elementValueList(path: import("prettier").AstPath<import("java-parser").ElementValueListCstNode & {
59
59
  comments?: import("../comments.js").JavaComment[];
60
60
  }>, print: import("./helpers.js").JavaPrintFn): builders.Group;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "2.7.5",
3
+ "version": "2.7.6",
4
4
  "description": "Prettier Java Plugin",
5
5
  "type": "module",
6
6
  "exports": {
@@ -41,5 +41,5 @@
41
41
  "peerDependencies": {
42
42
  "prettier": "^3.0.0"
43
43
  },
44
- "gitHead": "dfec507ed2e6d4cea3b2b8d012b2bcc3c626ba23"
44
+ "gitHead": "9f2ba5e304a7e2d744fa20f1f3617cc4a4651f88"
45
45
  }