prettier-plugin-java 2.7.3 → 2.7.5

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.
@@ -104,7 +104,7 @@ declare const _default: {
104
104
  }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
105
105
  resourceSpecification(path: import("prettier").AstPath<import("java-parser").ResourceSpecificationCstNode & {
106
106
  comments?: import("../comments.js").JavaComment[];
107
- }>, print: import("./helpers.js").JavaPrintFn, options: import("./helpers.js").JavaParserOptions): builders.Group | "()";
107
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group | "()";
108
108
  resourceList(path: import("prettier").AstPath<import("java-parser").ResourceListCstNode & {
109
109
  comments?: import("../comments.js").JavaComment[];
110
110
  }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
@@ -50,10 +50,11 @@ export default {
50
50
  var _a;
51
51
  const { children } = path.node;
52
52
  const hasEmptyStatement = isEmptyStatement(children.statement[0]);
53
+ const statements = map(path, print, "statement");
53
54
  const statement = [
54
55
  "if ",
55
56
  indentInParentheses(call(path, print, "expression")),
56
- hasEmptyStatement ? ";" : [" ", call(path, print, "statement", 0)]
57
+ hasEmptyStatement ? ";" : [" ", statements[0]]
57
58
  ];
58
59
  if (children.Else) {
59
60
  const danglingComments = printDanglingComments(path);
@@ -67,7 +68,7 @@ export default {
67
68
  statement.push(elseHasBlock ? " " : hardline);
68
69
  }
69
70
  const elseHasEmptyStatement = isEmptyStatement(children.statement[1]);
70
- statement.push("else", elseHasEmptyStatement ? ";" : [" ", call(path, print, "statement", 1)]);
71
+ statement.push("else", elseHasEmptyStatement ? ";" : [" ", statements[1]]);
71
72
  }
72
73
  return statement;
73
74
  },
@@ -318,9 +319,9 @@ export default {
318
319
  }
319
320
  return join(" ", blocks);
320
321
  },
321
- resourceSpecification(path, print, options) {
322
+ resourceSpecification(path, print) {
322
323
  const resources = [call(path, print, "resourceList")];
323
- if (options.trailingComma !== "none") {
324
+ if (path.node.children.Semicolon) {
324
325
  resources.push(ifBreak(";"));
325
326
  }
326
327
  return indentInParentheses(resources);
@@ -112,10 +112,10 @@ declare const _default: {
112
112
  template: typeof printSingle;
113
113
  stringTemplate(path: AstPath<StringTemplateCstNode & {
114
114
  comments?: JavaComment[];
115
- }>, print: JavaPrintFn): (builders.Group | builders.Doc[])[];
115
+ }>, print: JavaPrintFn): builders.Indent;
116
116
  textBlockTemplate(path: AstPath<TextBlockTemplateCstNode & {
117
117
  comments?: JavaComment[];
118
- }>, print: JavaPrintFn): (builders.Group | builders.Doc[])[];
118
+ }>, print: JavaPrintFn): builders.Indent;
119
119
  embeddedExpression: typeof printSingle;
120
120
  pattern: typeof printSingle;
121
121
  typePattern: typeof printSingle;
@@ -1,5 +1,5 @@
1
1
  import { builders, utils } from "prettier/doc";
2
- import { call, definedKeys, each, findBaseIndent, flatMap, indentInParentheses, isBinaryExpression, isNonTerminal, isTerminal, map, onlyDefinedKey, printDanglingComments, printList, printName, printSingle } from "./helpers.js";
2
+ import { call, definedKeys, each, findBaseIndent, flatMap, hasLeadingComments, indentInParentheses, isBinaryExpression, isNonTerminal, isTerminal, map, onlyDefinedKey, printDanglingComments, printList, printName, printSingle } from "./helpers.js";
3
3
  const { breakParent, conditionalGroup, group, hardline, ifBreak, indent, indentIfBreak, join, line, lineSuffixBoundary, softline } = builders;
4
4
  const { removeLines, willBreak } = utils;
5
5
  export default {
@@ -62,16 +62,23 @@ export default {
62
62
  conciseLambdaParameter: printSingle,
63
63
  lambdaBody: printSingle,
64
64
  conditionalExpression(path, print) {
65
+ var _a;
65
66
  const binaryExpression = call(path, print, "binaryExpression");
66
- return path.node.children.QuestionMark
67
- ? group(indent(join(line, [
68
- binaryExpression,
69
- ["? ", call(path, print, "expression", 0)],
70
- [": ", call(path, print, "expression", 1)]
71
- ])))
72
- : binaryExpression;
67
+ if (!path.node.children.QuestionMark) {
68
+ return binaryExpression;
69
+ }
70
+ const expressions = map(path, print, "expression");
71
+ const contents = indent(join(line, [
72
+ binaryExpression,
73
+ ["? ", expressions[0]],
74
+ [": ", expressions[1]]
75
+ ]));
76
+ const isNestedTernary = ((_a = path.getNode(4)) === null || _a === void 0 ? void 0 : _a.name) ===
77
+ "conditionalExpression";
78
+ return isNestedTernary ? contents : group(contents);
73
79
  },
74
80
  binaryExpression(path, print, options) {
81
+ var _a, _b;
75
82
  const { children } = path.node;
76
83
  const operands = flatMap(path, print, definedKeys(children, [
77
84
  "expression",
@@ -101,8 +108,11 @@ export default {
101
108
  const hasNonAssignmentOperators = (operators.length > 0 && !children.AssignmentOperator) ||
102
109
  (children.expression !== undefined &&
103
110
  isBinaryExpression(children.expression[0]));
111
+ const isInList = ((_a = path.getNode(4)) === null || _a === void 0 ? void 0 : _a.name) === "elementValue" ||
112
+ ((_b = path.getNode(6)) === null || _b === void 0 ? void 0 : _b.name) === "argumentList";
104
113
  return binary(operands, operators, {
105
114
  hasNonAssignmentOperators,
115
+ isInList,
106
116
  isRoot: true,
107
117
  operatorPosition: options.experimentalOperatorPosition
108
118
  });
@@ -181,7 +191,8 @@ export default {
181
191
  : suffix);
182
192
  }
183
193
  }, "primarySuffix");
184
- return group(canBreakForCallExpressions || willBreak(suffixes)
194
+ const hasSuffixComments = children.primarySuffix.some(suffix => hasLeadingComments(suffix));
195
+ return group(canBreakForCallExpressions || hasSuffixComments
185
196
  ? [prefix, indent(suffixes)]
186
197
  : [prefix, ...suffixes]);
187
198
  },
@@ -340,7 +351,7 @@ export default {
340
351
  return allArgsExpandable;
341
352
  }
342
353
  const headArgs = args.slice(0, -1);
343
- const huggedLastArg = call(path, argPath => print(argPath, { hug: true }), "expression", args.length - 1);
354
+ const huggedLastArg = path.call(argPath => print(argPath, { hug: true }), "children", "expression", args.length - 1);
344
355
  const lastArgExpanded = join(", ", [
345
356
  ...headArgs,
346
357
  group(huggedLastArg, { shouldBreak: true })
@@ -451,7 +462,7 @@ export default {
451
462
  ];
452
463
  }
453
464
  };
454
- function binary(operands, operators, { hasNonAssignmentOperators = false, isRoot = false, operatorPosition }) {
465
+ function binary(operands, operators, { hasNonAssignmentOperators = false, isInList = false, isRoot = false, operatorPosition }) {
455
466
  let levelOperator;
456
467
  let levelPrecedence;
457
468
  let level = [];
@@ -496,7 +507,10 @@ function binary(operands, operators, { hasNonAssignmentOperators = false, isRoot
496
507
  }
497
508
  }
498
509
  level.push(operands.shift());
499
- if (!levelOperator || !isAssignmentOperator(levelOperator)) {
510
+ if (!levelOperator ||
511
+ (!isInList &&
512
+ !isAssignmentOperator(levelOperator) &&
513
+ levelOperator !== "instanceof")) {
500
514
  return group(level);
501
515
  }
502
516
  if (!isRoot || hasNonAssignmentOperators) {
@@ -563,7 +577,6 @@ function isCapitalizedIdentifier(fqnOrRefType) {
563
577
  return /^\p{Uppercase_Letter}/u.test(nextToLastIdentifier !== null && nextToLastIdentifier !== void 0 ? nextToLastIdentifier : "");
564
578
  }
565
579
  function printTemplate(path, print, beginKey, midKey, endKey) {
566
- const { children } = path.node;
567
580
  const begin = call(path, ({ node }) => node.image, beginKey);
568
581
  const mids = map(path, ({ node }) => node.image, midKey);
569
582
  const end = call(path, ({ node }) => node.image, endKey);
@@ -571,7 +584,7 @@ function printTemplate(path, print, beginKey, midKey, endKey) {
571
584
  const baseIndent = findBaseIndent(lines);
572
585
  const prefix = "\n" + " ".repeat(baseIndent);
573
586
  const parts = [begin, ...mids, end].map(image => join(hardline, image.split(prefix)));
574
- return [
587
+ return indent([
575
588
  parts[0],
576
589
  ...map(path, (expressionPath, index) => {
577
590
  const expression = group([
@@ -581,5 +594,5 @@ function printTemplate(path, print, beginKey, midKey, endKey) {
581
594
  return index === 0 ? expression : [parts[index], expression];
582
595
  }, "embeddedExpression"),
583
596
  parts.at(-1)
584
- ];
597
+ ]);
585
598
  }
@@ -6,7 +6,7 @@ export declare function onlyDefinedKey<T extends Record<string, any>, K extends
6
6
  export declare function definedKeys<T extends Record<string, any>, K extends Key<T> & string>(obj: T, options?: K[]): K[];
7
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
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;
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): U;
10
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
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
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[];
@@ -30,7 +30,7 @@ export declare function printList<T extends JavaNonTerminal, P extends IterPrope
30
30
  export declare function printClassPermits(path: AstPath<ClassPermitsCstNode | InterfacePermitsCstNode>, print: JavaPrintFn): builders.Group;
31
31
  export declare function printClassType(path: AstPath<JavaNonTerminal & {
32
32
  children: ClassTypeCtx;
33
- }>, print: JavaPrintFn): builders.Doc[][];
33
+ }>, print: JavaPrintFn): builders.Doc[];
34
34
  export declare function isBinaryExpression(expression: ExpressionCstNode): boolean;
35
35
  export declare function findBaseIndent(lines: string[]): number;
36
36
  export declare function isEmptyStatement(statement: StatementCstNode): boolean;
@@ -71,8 +71,8 @@ export function hasDeclarationAnnotations(modifiers) {
71
71
  }
72
72
  return hasAnnotation && !hasNonAnnotation;
73
73
  }
74
- export function call(path, callback, child, index = 0) {
75
- return path.map(callback, "children", child)[index];
74
+ export function call(path, callback, child) {
75
+ return path.map(callback, "children", child)[0];
76
76
  }
77
77
  export function each(path, callback, child) {
78
78
  if (path.node.children[child]) {
@@ -181,23 +181,29 @@ export function printClassPermits(path, print) {
181
181
  ]);
182
182
  }
183
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(".");
184
+ const { children } = path.node;
185
+ return definedKeys(children, ["annotation", "Identifier", "typeArguments"])
186
+ .flatMap(child => children[child].map((node, index) => ({
187
+ child,
188
+ index,
189
+ startOffset: parser.locStart(node)
190
+ })))
191
+ .sort((a, b) => a.startOffset - b.startOffset)
192
+ .flatMap(({ child, index: childIndex }, index, array) => {
193
+ const node = children[child][childIndex];
194
+ const next = array.at(index + 1);
195
+ const nextNode = next && children[next.child][next.index];
196
+ const docs = [path.call(print, "children", child, childIndex)];
197
+ if (nextNode) {
198
+ if (isNonTerminal(node)) {
199
+ docs.push(node.name === "annotation" ? " " : ".");
200
+ }
201
+ else if (isTerminal(nextNode) || nextNode.name === "annotation") {
202
+ docs.push(".");
190
203
  }
191
204
  }
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
- ]));
205
+ return docs;
206
+ });
201
207
  }
202
208
  export function isBinaryExpression(expression) {
203
209
  var _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "2.7.3",
3
+ "version": "2.7.5",
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": "ec18b2fe1bcecd353d32cdcba7483cd0f88cb8a2"
44
+ "gitHead": "dfec507ed2e6d4cea3b2b8d012b2bcc3c626ba23"
45
45
  }