prettier-plugin-java 2.7.4 → 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.
@@ -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
  },
@@ -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[];
@@ -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]) {
@@ -193,7 +193,7 @@ export function printClassType(path, print) {
193
193
  const node = children[child][childIndex];
194
194
  const next = array.at(index + 1);
195
195
  const nextNode = next && children[next.child][next.index];
196
- const docs = [call(path, print, child, childIndex)];
196
+ const docs = [path.call(print, "children", child, childIndex)];
197
197
  if (nextNode) {
198
198
  if (isNonTerminal(node)) {
199
199
  docs.push(node.name === "annotation" ? " " : ".");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "2.7.4",
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": "306a608c6ff6aee2010120ffb47a8c8a13fe7ef1"
44
+ "gitHead": "dfec507ed2e6d4cea3b2b8d012b2bcc3c626ba23"
45
45
  }