prettier-plugin-java 2.6.6 → 2.6.8

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.
@@ -706,7 +706,4 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
706
706
  isDims() {
707
707
  return "isDims";
708
708
  }
709
- isFollowingVariableDeclarator() {
710
- return "isFollowingVariableDeclarator";
711
- }
712
709
  }
@@ -3,6 +3,7 @@ import { builders, utils } from "prettier/doc";
3
3
  import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
4
4
  import { isAnnotationCstNode } from "../types/utils.js";
5
5
  import { printArgumentListWithBraces } from "../utils/index.js";
6
+ import { hasLeadingComments } from "./comments/comments-utils.js";
6
7
  import { printTokenWithComments } from "./comments/format-comments.js";
7
8
  import { handleCommentsBinaryExpression, handleCommentsParameters } from "./comments/handle-comments.js";
8
9
  import { concat, dedent, group, indent, indentIfBreak } from "./prettier-builder.js";
@@ -25,7 +26,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
25
26
  ctx.Arrow[0],
26
27
  ...(isLambdaBodyABlock
27
28
  ? [" ", lambdaBody]
28
- : [group(indent([line, lambdaBody]))])
29
+ : [group([indent([line, lambdaBody]), (params === null || params === void 0 ? void 0 : params.hug) ? softline : ""])])
29
30
  ];
30
31
  if (params === null || params === void 0 ? void 0 : params.hug) {
31
32
  return willBreak(lambdaParameters)
@@ -209,47 +210,23 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
209
210
  const newExpression = (_a = ctx.primaryPrefix[0].children.newExpression) === null || _a === void 0 ? void 0 : _a[0].children;
210
211
  const isBreakableNewExpression = countMethodInvocation <= 1 &&
211
212
  this.isBreakableNewExpression(newExpression);
212
- const fqnOrRefType = (_b = ctx.primaryPrefix[0].children.fqnOrRefType) === null || _b === void 0 ? void 0 : _b[0].children;
213
- const firstMethodInvocation = (_c = ctx.primarySuffix) === null || _c === void 0 ? void 0 : _c.map(suffix => { var _a; return (_a = suffix.children.methodInvocationSuffix) === null || _a === void 0 ? void 0 : _a[0].children; }).find(methodInvocationSuffix => methodInvocationSuffix);
213
+ const firstMethodInvocation = (_b = ctx.primarySuffix) === null || _b === void 0 ? void 0 : _b.map(suffix => { var _a; return (_a = suffix.children.methodInvocationSuffix) === null || _a === void 0 ? void 0 : _a[0].children; }).find(methodInvocationSuffix => methodInvocationSuffix);
214
+ const fqnOrRefType = (_c = ctx.primaryPrefix[0].children.fqnOrRefType) === null || _c === void 0 ? void 0 : _c[0].children;
215
+ const hasFqnRefPart = (fqnOrRefType === null || fqnOrRefType === void 0 ? void 0 : fqnOrRefType.fqnOrRefTypePartRest) !== undefined;
214
216
  const isCapitalizedIdentifier = this.isCapitalizedIdentifier(fqnOrRefType);
215
- const shouldBreakBeforeFirstMethodInvocation = countMethodInvocation > 1 &&
216
- !(isCapitalizedIdentifier !== null && isCapitalizedIdentifier !== void 0 ? isCapitalizedIdentifier : true) &&
217
- firstMethodInvocation !== undefined;
217
+ const shouldBreakBeforeFirstMethodInvocation = countMethodInvocation > 1 && hasFqnRefPart && !isCapitalizedIdentifier;
218
218
  const shouldBreakBeforeMethodInvocations = shouldBreakBeforeFirstMethodInvocation ||
219
219
  countMethodInvocation > 2 ||
220
- (countMethodInvocation > 1 && newExpression) ||
220
+ (countMethodInvocation > 1 && !!newExpression) ||
221
221
  !(firstMethodInvocation === null || firstMethodInvocation === void 0 ? void 0 : firstMethodInvocation.argumentList);
222
222
  const primaryPrefix = this.visit(ctx.primaryPrefix, Object.assign(Object.assign({}, params), { shouldBreakBeforeFirstMethodInvocation }));
223
- const suffixes = [];
224
- if (ctx.primarySuffix !== undefined) {
225
- // edge case: https://github.com/jhipster/prettier-java/issues/381
226
- let hasFirstInvocationArg = true;
227
- if (ctx.primarySuffix.length > 1 &&
228
- ctx.primarySuffix[1].children.methodInvocationSuffix &&
229
- Object.keys(ctx.primarySuffix[1].children.methodInvocationSuffix[0].children).length === 2) {
230
- hasFirstInvocationArg = false;
231
- }
232
- if (newExpression &&
233
- !isBreakableNewExpression &&
234
- ctx.primarySuffix[0].children.Dot !== undefined) {
235
- suffixes.push(softline);
236
- }
237
- suffixes.push(this.visit(ctx.primarySuffix[0]));
238
- for (let i = 1; i < ctx.primarySuffix.length; i++) {
239
- if (shouldBreakBeforeMethodInvocations &&
240
- ctx.primarySuffix[i].children.Dot !== undefined &&
241
- ctx.primarySuffix[i - 1].children.methodInvocationSuffix !== undefined) {
242
- suffixes.push(softline);
243
- }
244
- suffixes.push(this.visit(ctx.primarySuffix[i]));
245
- }
246
- if (!newExpression && countMethodInvocation === 1) {
247
- return group(rejectAndConcat([
248
- primaryPrefix,
249
- hasFirstInvocationArg ? suffixes[0] : indent(suffixes[0]),
250
- indent(rejectAndConcat(suffixes.slice(1)))
251
- ]));
252
- }
223
+ const suffixes = this.getPrimarySuffixes(ctx, newExpression, isBreakableNewExpression, shouldBreakBeforeMethodInvocations);
224
+ if (!newExpression && countMethodInvocation === 1) {
225
+ return group(rejectAndConcat([
226
+ primaryPrefix,
227
+ suffixes[0],
228
+ indent(rejectAndConcat(suffixes.slice(1)))
229
+ ]));
253
230
  }
254
231
  const methodInvocation = (_d = ctx.primarySuffix) === null || _d === void 0 ? void 0 : _d[0].children.methodInvocationSuffix;
255
232
  const isMethodInvocationWithArguments = (methodInvocation === null || methodInvocation === void 0 ? void 0 : methodInvocation[0].children.argumentList) !== undefined;
@@ -290,40 +267,25 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
290
267
  const fqnOrRefTypePartFirst = this.visit(ctx.fqnOrRefTypePartFirst);
291
268
  const fqnOrRefTypePartRest = this.mapVisit(ctx.fqnOrRefTypePartRest);
292
269
  const dims = this.visit(ctx.dims);
293
- const dots = ctx.Dot ? ctx.Dot : [];
294
- const isMethodInvocation = ctx.Dot && ctx.Dot.length === 1;
295
- if (params !== undefined &&
296
- params.shouldBreakBeforeFirstMethodInvocation === true) {
297
- // when fqnOrRefType is a method call from an object
298
- if (isMethodInvocation) {
299
- return rejectAndConcat([
300
- indent(rejectAndJoin(concat([softline, dots[0]]), [
301
- fqnOrRefTypePartFirst,
302
- rejectAndJoinSeps(dots.slice(1), fqnOrRefTypePartRest),
303
- dims
304
- ]))
305
- ]);
306
- // otherwise it is a fully qualified name but we need to exclude when it is just a method call
307
- }
308
- else if (ctx.Dot) {
309
- return indent(rejectAndConcat([
310
- rejectAndJoinSeps(dots.slice(0, dots.length - 1), [
311
- fqnOrRefTypePartFirst,
312
- ...fqnOrRefTypePartRest.slice(0, fqnOrRefTypePartRest.length - 1)
313
- ]),
314
- softline,
315
- rejectAndConcat([
316
- dots[dots.length - 1],
317
- fqnOrRefTypePartRest[fqnOrRefTypePartRest.length - 1]
318
- ]),
319
- dims
320
- ]));
321
- }
270
+ const dots = ctx.Dot
271
+ ? ctx.Dot.map(dot => {
272
+ if (hasLeadingComments(dot)) {
273
+ return concat([softline, dot]);
274
+ }
275
+ return dot;
276
+ })
277
+ : [];
278
+ if ((params === null || params === void 0 ? void 0 : params.shouldBreakBeforeFirstMethodInvocation) === true &&
279
+ ctx.Dot !== undefined) {
280
+ dots[dots.length - 1] = concat([softline, ctx.Dot[ctx.Dot.length - 1]]);
322
281
  }
323
- return rejectAndConcat([
324
- rejectAndJoinSeps(dots, [fqnOrRefTypePartFirst, ...fqnOrRefTypePartRest]),
282
+ return indent(rejectAndConcat([
283
+ rejectAndJoinSeps(dots, [
284
+ fqnOrRefTypePartFirst,
285
+ ...fqnOrRefTypePartRest
286
+ ]),
325
287
  dims
326
- ]);
288
+ ]));
327
289
  }
328
290
  fqnOrRefTypePartFirst(ctx) {
329
291
  const annotation = this.mapVisit(ctx.annotation);
@@ -430,7 +392,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
430
392
  return printArgumentListWithBraces.call(this, ctx.argumentList, ctx.RBrace[0], ctx.LBrace[0]);
431
393
  }
432
394
  argumentList(ctx) {
433
- var _a, _b;
395
+ var _a;
434
396
  const headArgs = this.mapVisit(ctx.expression.slice(0, -1)).map((expression, index) => concat([expression, ctx.Comma[index], line]));
435
397
  const lastExpression = ctx.expression.at(-1);
436
398
  const lastArg = this.visit(lastExpression);
@@ -445,13 +407,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
445
407
  shouldBreak: true
446
408
  });
447
409
  }
448
- const suffix = ((_b = lastExpression === null || lastExpression === void 0 ? void 0 : lastExpression.children.lambdaExpression) === null || _b === void 0 ? void 0 : _b[0].children.lambdaBody[0].children.block)
449
- ? ""
450
- : line;
451
- const hugged = [
452
- ...headArgs,
453
- group([huggedLastArg, suffix], { shouldBreak: true })
454
- ];
410
+ const hugged = [...headArgs, group(huggedLastArg, { shouldBreak: true })];
455
411
  const expanded = group([indent([line, ...headArgs, lastArg]), line], {
456
412
  shouldBreak: true
457
413
  });
@@ -608,7 +564,28 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
608
564
  ...((_a = fqnOrRefType === null || fqnOrRefType === void 0 ? void 0 : fqnOrRefType.fqnOrRefTypePartRest) !== null && _a !== void 0 ? _a : [])
609
565
  ];
610
566
  const nextToLastIdentifier = (_c = (_b = fqnOrRefTypeParts[fqnOrRefTypeParts.length - 2]) === null || _b === void 0 ? void 0 : _b.children.fqnOrRefTypePartCommon[0].children.Identifier) === null || _c === void 0 ? void 0 : _c[0].image;
611
- return (nextToLastIdentifier &&
567
+ return (!!nextToLastIdentifier &&
612
568
  /^\p{Uppercase_Letter}/u.test(nextToLastIdentifier));
613
569
  }
570
+ getPrimarySuffixes(ctx, newExpression, isBreakableNewExpression, shouldBreakBeforeMethodInvocations) {
571
+ if (ctx.primarySuffix === undefined) {
572
+ return [];
573
+ }
574
+ const suffixes = [];
575
+ if (newExpression &&
576
+ !isBreakableNewExpression &&
577
+ ctx.primarySuffix[0].children.Dot !== undefined) {
578
+ suffixes.push(softline);
579
+ }
580
+ suffixes.push(this.visit(ctx.primarySuffix[0]));
581
+ for (let i = 1; i < ctx.primarySuffix.length; i++) {
582
+ if (shouldBreakBeforeMethodInvocations &&
583
+ ctx.primarySuffix[i].children.Dot !== undefined &&
584
+ ctx.primarySuffix[i - 1].children.methodInvocationSuffix !== undefined) {
585
+ suffixes.push(softline);
586
+ }
587
+ suffixes.push(this.visit(ctx.primarySuffix[i]));
588
+ }
589
+ return suffixes;
590
+ }
614
591
  }
package/package.json CHANGED
@@ -1,13 +1,11 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "2.6.6",
3
+ "version": "2.6.8",
4
4
  "description": "Prettier Java Plugin",
5
5
  "type": "module",
6
6
  "exports": {
7
- ".": {
8
- "types": "./index.d.ts",
9
- "import": "./dist/index.js"
10
- }
7
+ "types": "./index.d.ts",
8
+ "default": "./dist/index.js"
11
9
  },
12
10
  "files": [
13
11
  "dist",
@@ -17,7 +15,7 @@
17
15
  "repository": "https://github.com/jhipster/prettier-java",
18
16
  "license": "Apache-2.0",
19
17
  "dependencies": {
20
- "java-parser": "2.3.3",
18
+ "java-parser": "2.3.4",
21
19
  "lodash": "4.17.21"
22
20
  },
23
21
  "scripts": {
@@ -46,5 +44,5 @@
46
44
  "peerDependencies": {
47
45
  "prettier": "^3.0.0"
48
46
  },
49
- "gitHead": "b80f28061b45d46df03c441ff3af3d68e84b3c23"
47
+ "gitHead": "a1b05b474a157165133bff7a86c1d2b88bbfa5b4"
50
48
  }
@@ -1,25 +0,0 @@
1
- export function isArgumentListSingleLambda(argumentList) {
2
- if (argumentList === undefined) {
3
- return false;
4
- }
5
- const args = argumentList[0].children.expression;
6
- if (args.length !== 1) {
7
- return false;
8
- }
9
- const argument = args[0];
10
- return argument.children.lambdaExpression !== undefined;
11
- }
12
- export const isSingleArgumentLambdaExpressionWithBlock = (argumentList) => {
13
- if (argumentList === undefined) {
14
- return false;
15
- }
16
- const args = argumentList[0].children.expression;
17
- if (args.length !== 1) {
18
- return false;
19
- }
20
- const argument = args[0];
21
- return (argument.children.lambdaExpression !== undefined &&
22
- argument.children.lambdaExpression[0].children.lambdaBody[0].children
23
- .block !== undefined);
24
- };
25
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhwcmVzc2lvbnMtdXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdXRpbHMvZXhwcmVzc2lvbnMtdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUEsTUFBTSxVQUFVLDBCQUEwQixDQUN4QyxZQUErQztJQUUvQyxJQUFJLFlBQVksS0FBSyxTQUFTLEVBQUU7UUFDOUIsT0FBTyxLQUFLLENBQUM7S0FDZDtJQUVELE1BQU0sSUFBSSxHQUFHLFlBQVksQ0FBQyxDQUFDLENBQUMsQ0FBQyxRQUFRLENBQUMsVUFBVSxDQUFDO0lBQ2pELElBQUksSUFBSSxDQUFDLE1BQU0sS0FBSyxDQUFDLEVBQUU7UUFDckIsT0FBTyxLQUFLLENBQUM7S0FDZDtJQUVELE1BQU0sUUFBUSxHQUFHLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQztJQUN6QixPQUFPLFFBQVEsQ0FBQyxRQUFRLENBQUMsZ0JBQWdCLEtBQUssU0FBUyxDQUFDO0FBQzFELENBQUM7QUFFRCxNQUFNLENBQUMsTUFBTSx5Q0FBeUMsR0FBRyxDQUN2RCxZQUErQyxFQUMvQyxFQUFFO0lBQ0YsSUFBSSxZQUFZLEtBQUssU0FBUyxFQUFFO1FBQzlCLE9BQU8sS0FBSyxDQUFDO0tBQ2Q7SUFFRCxNQUFNLElBQUksR0FBRyxZQUFZLENBQUMsQ0FBQyxDQUFDLENBQUMsUUFBUSxDQUFDLFVBQVUsQ0FBQztJQUNqRCxJQUFJLElBQUksQ0FBQyxNQUFNLEtBQUssQ0FBQyxFQUFFO1FBQ3JCLE9BQU8sS0FBSyxDQUFDO0tBQ2Q7SUFFRCxNQUFNLFFBQVEsR0FBRyxJQUFJLENBQUMsQ0FBQyxDQUFDLENBQUM7SUFDekIsT0FBTyxDQUNMLFFBQVEsQ0FBQyxRQUFRLENBQUMsZ0JBQWdCLEtBQUssU0FBUztRQUNoRCxRQUFRLENBQUMsUUFBUSxDQUFDLGdCQUFnQixDQUFDLENBQUMsQ0FBQyxDQUFDLFFBQVEsQ0FBQyxVQUFVLENBQUMsQ0FBQyxDQUFDLENBQUMsUUFBUTthQUNsRSxLQUFLLEtBQUssU0FBUyxDQUN2QixDQUFDO0FBQ0osQ0FBQyxDQUFDIn0=
@@ -1,18 +0,0 @@
1
- import { builders } from "prettier/doc";
2
- import { isSingleArgumentLambdaExpressionWithBlock } from "./expressions-utils.js";
3
- import { printTokenWithComments } from "../printers/comments/format-comments.js";
4
- import { concat, dedent, indent } from "../printers/prettier-builder.js";
5
- import { putIntoBraces } from "../printers/printer-utils.js";
6
- const { softline, ifBreak } = builders;
7
- export default function printSingleLambdaInvocation(argumentListCtx, rBrace, lBrace) {
8
- const lambdaParametersGroupId = Symbol("lambdaParameters");
9
- const argumentList = this.visit(argumentListCtx, {
10
- lambdaParametersGroupId,
11
- isInsideMethodInvocationSuffix: true
12
- });
13
- const formattedRBrace = isSingleArgumentLambdaExpressionWithBlock(argumentListCtx)
14
- ? ifBreak(indent(concat([softline, rBrace])), printTokenWithComments(rBrace), { groupId: lambdaParametersGroupId })
15
- : indent(concat([softline, rBrace]));
16
- return dedent(putIntoBraces(argumentList, "", lBrace, formattedRBrace));
17
- }
18
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJpbnRTaW5nbGVMYW1iZGFJbnZvY2F0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3V0aWxzL3ByaW50U2luZ2xlTGFtYmRhSW52b2NhdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFDQSxPQUFPLEVBQUUsUUFBUSxFQUFFLE1BQU0sY0FBYyxD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