prettier-plugin-java 2.6.5 → 2.6.7

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.
package/LICENSE CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- Copyright 2018-2024 the original author or authors from the JHipster project
2
+ Copyright 2018-2025 the original author or authors from the JHipster project
3
3
 
4
4
  Apache License
5
5
  Version 2.0, January 2004
package/dist/index.js CHANGED
@@ -6,8 +6,8 @@ const languages = [
6
6
  name: "Java",
7
7
  parsers: ["java"],
8
8
  group: "Java",
9
- tmScope: "text.html.vue",
10
- aceMode: "html",
9
+ tmScope: "text.html.vue", // FIXME
10
+ aceMode: "html", // FIXME
11
11
  codemirrorMode: "clike",
12
12
  codemirrorMimeType: "text/x-java",
13
13
  extensions: [".java"],
@@ -706,4 +706,7 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
706
706
  isDims() {
707
707
  return "isDims";
708
708
  }
709
+ isFollowingVariableDeclarator() {
710
+ return "isFollowingVariableDeclarator";
711
+ }
709
712
  }
@@ -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";
@@ -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);
@@ -608,7 +570,28 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
608
570
  ...((_a = fqnOrRefType === null || fqnOrRefType === void 0 ? void 0 : fqnOrRefType.fqnOrRefTypePartRest) !== null && _a !== void 0 ? _a : [])
609
571
  ];
610
572
  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 &&
573
+ return (!!nextToLastIdentifier &&
612
574
  /^\p{Uppercase_Letter}/u.test(nextToLastIdentifier));
613
575
  }
576
+ getPrimarySuffixes(ctx, newExpression, isBreakableNewExpression, shouldBreakBeforeMethodInvocations) {
577
+ if (ctx.primarySuffix === undefined) {
578
+ return [];
579
+ }
580
+ const suffixes = [];
581
+ if (newExpression &&
582
+ !isBreakableNewExpression &&
583
+ ctx.primarySuffix[0].children.Dot !== undefined) {
584
+ suffixes.push(softline);
585
+ }
586
+ suffixes.push(this.visit(ctx.primarySuffix[0]));
587
+ for (let i = 1; i < ctx.primarySuffix.length; i++) {
588
+ if (shouldBreakBeforeMethodInvocations &&
589
+ ctx.primarySuffix[i].children.Dot !== undefined &&
590
+ ctx.primarySuffix[i - 1].children.methodInvocationSuffix !== undefined) {
591
+ suffixes.push(softline);
592
+ }
593
+ suffixes.push(this.visit(ctx.primarySuffix[i]));
594
+ }
595
+ return suffixes;
596
+ }
614
597
  }
@@ -0,0 +1,25 @@
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=
@@ -0,0 +1,18 @@
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJpbnRTaW5nbGVMYW1iZGFJbnZvY2F0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3V0aWxzL3ByaW50U2luZ2xlTGFtYmRhSW52b2NhdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFDQ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dkMsT0FBTyxNQUFNLENBQUMsYUFBYSxDQUFDLFlBQVksRUFBRSxFQUFFLEVBQUUsTUFBTSxFQUFFLGVBQWUsQ0FBQyxDQUFDLENBQUM7QUFDMUUsQ0FBQyJ9
package/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Plugin } from "prettier";
2
+
3
+ declare const plugin: Plugin;
4
+ export default plugin;
package/package.json CHANGED
@@ -1,19 +1,22 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "2.6.5",
3
+ "version": "2.6.7",
4
4
  "description": "Prettier Java Plugin",
5
5
  "type": "module",
6
- "exports": "./dist/index.js",
6
+ "exports": {
7
+ "types": "./index.d.ts",
8
+ "default": "./dist/index.js"
9
+ },
7
10
  "files": [
8
- "dist"
11
+ "dist",
12
+ "index.d.ts"
9
13
  ],
10
14
  "homepage": "https://jhipster.github.io/prettier-java/",
11
15
  "repository": "https://github.com/jhipster/prettier-java",
12
16
  "license": "Apache-2.0",
13
17
  "dependencies": {
14
- "java-parser": "2.3.2",
15
- "lodash": "4.17.21",
16
- "prettier": "3.2.5"
18
+ "java-parser": "2.3.3",
19
+ "lodash": "4.17.21"
17
20
  },
18
21
  "scripts": {
19
22
  "test": "yarn run test:unit && yarn run test:e2e-core",
@@ -28,15 +31,18 @@
28
31
  },
29
32
  "devDependencies": {
30
33
  "@chevrotain/types": "11.0.3",
31
- "@types/chai": "4.3.4",
32
- "@types/fs-extra": "9.0.13",
33
- "@types/jest": "29.2.3",
34
- "@types/klaw-sync": "6.0.1",
35
- "@types/lodash": "4.14.190",
36
- "@types/node": "18.11.9",
37
- "@types/sinon": "10.0.13",
38
- "ts-node": "10.9.1",
39
- "typescript": "4.9.3"
34
+ "@types/chai": "5.0.1",
35
+ "@types/fs-extra": "11.0.4",
36
+ "@types/jest": "29.5.14",
37
+ "@types/klaw-sync": "6.0.5",
38
+ "@types/lodash": "4.17.13",
39
+ "@types/node": "18.19.64",
40
+ "@types/sinon": "17.0.3",
41
+ "ts-node": "10.9.2",
42
+ "typescript": "5.6.3"
43
+ },
44
+ "peerDependencies": {
45
+ "prettier": "^3.0.0"
40
46
  },
41
- "gitHead": "1c4691a0b302c4cceed4b30d8266d3235fca2de8"
47
+ "gitHead": "79c4d9f960374cf7a2c67fff83efb6e605c41163"
42
48
  }