prettier-plugin-java 2.6.8 → 2.7.0

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.
Files changed (45) hide show
  1. package/dist/comments.d.ts +17 -0
  2. package/dist/comments.js +199 -0
  3. package/dist/index.d.ts +543 -0
  4. package/dist/index.js +26 -63
  5. package/dist/options.d.ts +23 -0
  6. package/dist/options.js +247 -239
  7. package/dist/parser.d.ts +9 -0
  8. package/dist/parser.js +24 -4
  9. package/dist/printer.d.ts +18 -0
  10. package/dist/printer.js +39 -5
  11. package/dist/printers/arrays.d.ts +9 -0
  12. package/dist/printers/arrays.js +8 -24
  13. package/dist/printers/blocks-and-statements.d.ts +117 -0
  14. package/dist/printers/blocks-and-statements.js +316 -412
  15. package/dist/printers/classes.d.ts +157 -0
  16. package/dist/printers/classes.js +422 -685
  17. package/dist/printers/expressions.d.ts +134 -0
  18. package/dist/printers/expressions.js +549 -555
  19. package/dist/printers/helpers.d.ts +71 -0
  20. package/dist/printers/helpers.js +233 -0
  21. package/dist/printers/index.d.ts +2 -0
  22. package/dist/printers/index.js +13 -0
  23. package/dist/printers/interfaces.d.ts +62 -0
  24. package/dist/printers/interfaces.js +146 -211
  25. package/dist/printers/lexical-structure.d.ts +14 -0
  26. package/dist/printers/lexical-structure.js +26 -28
  27. package/dist/printers/names.d.ts +12 -0
  28. package/dist/printers/names.js +11 -29
  29. package/dist/printers/packages-and-modules.d.ts +46 -0
  30. package/dist/printers/packages-and-modules.js +157 -159
  31. package/dist/printers/types-values-and-variables.d.ts +46 -0
  32. package/dist/printers/types-values-and-variables.js +86 -149
  33. package/package.json +5 -8
  34. package/dist/base-cst-printer.js +0 -55
  35. package/dist/cst-printer.js +0 -29
  36. package/dist/printers/comments/comments-utils.js +0 -21
  37. package/dist/printers/comments/format-comments.js +0 -171
  38. package/dist/printers/comments/handle-comments.js +0 -102
  39. package/dist/printers/prettier-builder.js +0 -45
  40. package/dist/printers/printer-utils.js +0 -598
  41. package/dist/types/utils.js +0 -20
  42. package/dist/utils/index.js +0 -2
  43. package/dist/utils/isEmptyDoc.js +0 -4
  44. package/dist/utils/printArgumentListWithBraces.js +0 -37
  45. package/index.d.ts +0 -4
@@ -1,171 +1,169 @@
1
- import { concat, join } from "./prettier-builder.js";
2
- import { printTokenWithComments } from "./comments/format-comments.js";
3
- import { buildFqn, displaySemicolon, getBlankLinesSeparator, putIntoBraces, rejectAndConcat, rejectAndJoin, rejectAndJoinSeps, sortImports } from "./printer-utils.js";
4
1
  import { builders } from "prettier/doc";
5
- import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
6
- import { isOrdinaryCompilationUnitCtx } from "../types/utils.js";
7
- const { line, hardline, indent, group } = builders;
8
- export class PackagesAndModulesPrettierVisitor extends BaseCstPrettierPrinter {
9
- compilationUnit(ctx) {
10
- const compilationUnit = isOrdinaryCompilationUnitCtx(ctx)
11
- ? ctx.ordinaryCompilationUnit
12
- : ctx.modularCompilationUnit;
13
- return concat([this.visit(compilationUnit[0]), line]);
14
- }
15
- ordinaryCompilationUnit(ctx) {
16
- const packageDecl = this.visit(ctx.packageDeclaration);
17
- const sortedImportsDecl = sortImports(ctx.importDeclaration);
18
- const nonStaticImports = this.mapVisit(sortedImportsDecl.nonStaticImports);
19
- const staticImports = this.mapVisit(sortedImportsDecl.staticImports);
20
- const typesDecl = this.mapVisit(ctx.typeDeclaration);
21
- // TODO: utility to add item+line (or multiple lines) but only if an item exists
22
- return rejectAndConcat([
23
- rejectAndJoin(concat([hardline, hardline]), [
24
- packageDecl,
25
- rejectAndJoin(hardline, staticImports),
26
- rejectAndJoin(hardline, nonStaticImports),
27
- rejectAndJoin(concat([hardline, hardline]), typesDecl)
28
- ])
29
- ]);
30
- }
31
- modularCompilationUnit(ctx) {
32
- const sortedImportsDecl = sortImports(ctx.importDeclaration);
33
- const nonStaticImports = this.mapVisit(sortedImportsDecl.nonStaticImports);
34
- const staticImports = this.mapVisit(sortedImportsDecl.staticImports);
35
- const moduleDeclaration = this.visit(ctx.moduleDeclaration);
36
- return rejectAndConcat([
37
- rejectAndJoin(concat([hardline, hardline]), [
38
- rejectAndJoin(hardline, staticImports),
39
- rejectAndJoin(hardline, nonStaticImports),
40
- moduleDeclaration
41
- ])
42
- ]);
43
- }
44
- packageDeclaration(ctx) {
45
- const modifiers = this.mapVisit(ctx.packageModifier);
46
- const name = buildFqn(ctx.Identifier, ctx.Dot);
47
- return rejectAndJoin(hardline, [
48
- rejectAndJoin(hardline, modifiers),
49
- concat([ctx.Package[0], " ", name, ctx.Semicolon[0]])
50
- ]);
51
- }
52
- packageModifier(ctx) {
53
- return this.visitSingle(ctx);
54
- }
55
- importDeclaration(ctx) {
56
- if (ctx.emptyStatement !== undefined) {
57
- return this.visit(ctx.emptyStatement);
2
+ import { call, lineEndWithComments, lineStartWithComments, map, printBlock, printDanglingComments, printName, printSingle } from "./helpers.js";
3
+ const { group, hardline, indent, join, line } = builders;
4
+ export default {
5
+ compilationUnit(path, print) {
6
+ return [...printDanglingComments(path), printSingle(path, print), hardline];
7
+ },
8
+ ordinaryCompilationUnit(path, print) {
9
+ const { children } = path.node;
10
+ const declarations = [];
11
+ if (children.packageDeclaration) {
12
+ declarations.push(call(path, print, "packageDeclaration"));
58
13
  }
59
- const optionalStatic = ctx.Static ? ctx.Static[0] : "";
60
- const packageOrTypeName = this.visit(ctx.packageOrTypeName);
61
- const optionalDotStar = ctx.Dot ? concat([ctx.Dot[0], ctx.Star[0]]) : "";
62
- return rejectAndJoin(" ", [
63
- ctx.Import[0],
64
- optionalStatic,
65
- rejectAndConcat([packageOrTypeName, optionalDotStar, ctx.Semicolon[0]])
66
- ]);
67
- }
68
- typeDeclaration(ctx) {
69
- if (ctx.Semicolon) {
70
- return displaySemicolon(ctx.Semicolon[0]);
14
+ if (children.importDeclaration) {
15
+ const staticCount = sortImports(children.importDeclaration);
16
+ const importDeclarations = map(path, print, "importDeclaration").filter(doc => doc !== "");
17
+ const staticDeclarations = importDeclarations.slice(0, staticCount);
18
+ const nonStaticDeclarations = importDeclarations.slice(staticCount);
19
+ declarations.push(...[staticDeclarations, nonStaticDeclarations]
20
+ .filter(({ length }) => length)
21
+ .map(declarations => join(hardline, declarations)));
71
22
  }
72
- return this.visitSingle(ctx);
73
- }
74
- moduleDeclaration(ctx) {
75
- const annotations = this.mapVisit(ctx.annotation);
76
- const optionalOpen = ctx.Open ? ctx.Open[0] : "";
77
- const name = buildFqn(ctx.Identifier, ctx.Dot);
78
- const moduleDirectives = this.mapVisit(ctx.moduleDirective);
79
- const content = rejectAndJoinSeps(getBlankLinesSeparator(ctx.moduleDirective), moduleDirectives);
80
- return rejectAndJoin(" ", [
81
- join(" ", annotations),
82
- optionalOpen,
83
- ctx.Module[0],
84
- name,
85
- putIntoBraces(content, hardline, ctx.LCurly[0], ctx.RCurly[0])
86
- ]);
87
- }
88
- moduleDirective(ctx) {
89
- return this.visitSingle(ctx);
90
- }
91
- requiresModuleDirective(ctx) {
92
- const modifiers = this.mapVisit(ctx.requiresModifier);
93
- const moduleName = this.visit(ctx.moduleName);
94
- return rejectAndJoin(" ", [
95
- ctx.Requires[0],
96
- join(" ", modifiers),
97
- concat([moduleName, ctx.Semicolon[0]])
98
- ]);
99
- }
100
- exportsModuleDirective(ctx) {
101
- const packageName = this.visit(ctx.packageName);
102
- const moduleNames = this.mapVisit(ctx.moduleName);
103
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
104
- if (ctx.To) {
105
- return group(rejectAndConcat([
106
- indent(rejectAndJoin(line, [
107
- rejectAndJoin(" ", [ctx.Exports[0], packageName]),
108
- group(indent(rejectAndJoin(line, [
109
- ctx.To[0],
110
- rejectAndJoinSeps(commas, moduleNames)
111
- ])))
112
- ])),
113
- ctx.Semicolon[0]
114
- ]));
23
+ if (children.typeDeclaration) {
24
+ declarations.push(...map(path, print, "typeDeclaration").filter(declaration => declaration !== ""));
115
25
  }
116
- return rejectAndConcat([
117
- concat([ctx.Exports[0], " "]),
118
- packageName,
119
- ctx.Semicolon[0]
26
+ return join([hardline, hardline], declarations);
27
+ },
28
+ modularCompilationUnit(path, print) {
29
+ const { children } = path.node;
30
+ const declarations = [];
31
+ if (children.importDeclaration) {
32
+ const staticCount = sortImports(children.importDeclaration);
33
+ const importDeclarations = map(path, print, "importDeclaration").filter(doc => doc !== "");
34
+ const staticDeclarations = importDeclarations.slice(0, staticCount);
35
+ const nonStaticDeclarations = importDeclarations.slice(staticCount);
36
+ declarations.push(...[staticDeclarations, nonStaticDeclarations]
37
+ .filter(({ length }) => length)
38
+ .map(declarations => join(hardline, declarations)));
39
+ }
40
+ declarations.push(call(path, print, "moduleDeclaration"));
41
+ return join([hardline, hardline], declarations);
42
+ },
43
+ packageDeclaration(path, print) {
44
+ return join(hardline, [
45
+ ...map(path, print, "packageModifier"),
46
+ ["package ", printName(path, print), ";"]
120
47
  ]);
121
- }
122
- opensModuleDirective(ctx) {
123
- const packageName = this.visit(ctx.packageName);
124
- const to = ctx.To ? ctx.To[0] : "";
125
- const moduleNames = this.mapVisit(ctx.moduleName);
126
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
127
- if (ctx.To) {
128
- return group(rejectAndConcat([
129
- indent(rejectAndJoin(line, [
130
- rejectAndJoin(" ", [ctx.Opens[0], packageName]),
131
- group(indent(rejectAndJoin(line, [
132
- ctx.To[0],
133
- rejectAndJoinSeps(commas, moduleNames)
134
- ])))
135
- ])),
136
- ctx.Semicolon[0]
137
- ]));
48
+ },
49
+ packageModifier: printSingle,
50
+ importDeclaration(path, print) {
51
+ const { children } = path.node;
52
+ if (children.emptyStatement) {
53
+ return call(path, print, "emptyStatement");
54
+ }
55
+ const declaration = ["import "];
56
+ if (children.Static) {
57
+ declaration.push("static ");
58
+ }
59
+ declaration.push(call(path, print, "packageOrTypeName"));
60
+ if (children.Star) {
61
+ declaration.push(".*");
138
62
  }
139
- return rejectAndConcat([
140
- concat([ctx.Opens[0], " "]),
141
- packageName,
142
- ctx.Semicolon[0]
63
+ declaration.push(";");
64
+ return declaration;
65
+ },
66
+ typeDeclaration(path, print) {
67
+ return path.node.children.Semicolon ? "" : printSingle(path, print);
68
+ },
69
+ moduleDeclaration(path, print) {
70
+ const { annotation, Open } = path.node.children;
71
+ const prefix = [];
72
+ if (annotation) {
73
+ prefix.push(...map(path, print, "annotation"));
74
+ }
75
+ if (Open) {
76
+ prefix.push("open");
77
+ }
78
+ const declarations = map(path, declarationPath => {
79
+ const declaration = print(declarationPath);
80
+ const { node, previous } = declarationPath;
81
+ return !previous ||
82
+ lineStartWithComments(node) <= lineEndWithComments(previous) + 1
83
+ ? declaration
84
+ : [hardline, declaration];
85
+ }, "moduleDirective");
86
+ return join(" ", [
87
+ ...prefix,
88
+ "module",
89
+ printName(path, print),
90
+ printBlock(path, declarations)
143
91
  ]);
144
- }
145
- usesModuleDirective(ctx) {
146
- const typeName = this.visit(ctx.typeName);
147
- return rejectAndConcat([
148
- concat([ctx.Uses[0], " "]),
149
- typeName,
150
- ctx.Semicolon[0]
92
+ },
93
+ moduleDirective: printSingle,
94
+ requiresModuleDirective(path, print) {
95
+ return join(" ", [
96
+ "requires",
97
+ ...map(path, print, "requiresModifier"),
98
+ [call(path, print, "moduleName"), ";"]
151
99
  ]);
152
- }
153
- providesModuleDirective(ctx) {
154
- const firstTypeName = this.visit(ctx.typeName[0]);
155
- const otherTypeNames = this.mapVisit(ctx.typeName.slice(1));
156
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
157
- return group(rejectAndConcat([
158
- indent(rejectAndJoin(line, [
159
- rejectAndJoin(" ", [ctx.Provides[0], firstTypeName]),
160
- group(indent(rejectAndJoin(line, [
161
- ctx.With[0],
162
- rejectAndJoinSeps(commas, otherTypeNames)
163
- ])))
100
+ },
101
+ exportsModuleDirective(path, print) {
102
+ return printToModuleNamesDirective(path, print, "exports");
103
+ },
104
+ opensModuleDirective(path, print) {
105
+ return printToModuleNamesDirective(path, print, "opens");
106
+ },
107
+ usesModuleDirective(path, print) {
108
+ return ["uses ", call(path, print, "typeName"), ";"];
109
+ },
110
+ providesModuleDirective(path, print) {
111
+ const [firstTypeName, ...restTypeNames] = map(path, print, "typeName");
112
+ return [
113
+ "provides ",
114
+ firstTypeName,
115
+ group(indent([
116
+ line,
117
+ group(indent(["with", line, ...join([",", line], restTypeNames)]))
164
118
  ])),
165
- ctx.Semicolon[0]
166
- ]));
119
+ ";"
120
+ ];
121
+ },
122
+ requiresModifier: printSingle
123
+ };
124
+ function sortImports(importDeclarations) {
125
+ importDeclarations.sort(({ children: a }, { children: b }) => {
126
+ if (a.Static && !b.Static) {
127
+ return -1;
128
+ }
129
+ else if (b.Static && !a.Static) {
130
+ return 1;
131
+ }
132
+ if (!b.packageOrTypeName) {
133
+ if (a.packageOrTypeName) {
134
+ return -1;
135
+ }
136
+ return 0;
137
+ }
138
+ else if (!a.packageOrTypeName) {
139
+ return 1;
140
+ }
141
+ return compareFqn(a.packageOrTypeName[0], b.packageOrTypeName[0]);
142
+ });
143
+ return importDeclarations.reduce((staticCount, importDeclaration) => importDeclaration.children.Static ? staticCount + 1 : staticCount, 0);
144
+ }
145
+ function compareFqn(a, b) {
146
+ const identifiersA = a.children.Identifier;
147
+ const identifiersB = b.children.Identifier;
148
+ const minParts = Math.min(identifiersA.length, identifiersB.length);
149
+ for (let i = 0; i < minParts; i++) {
150
+ const imageA = identifiersA[i].image;
151
+ const imageB = identifiersB[i].image;
152
+ if (imageA < imageB) {
153
+ return -1;
154
+ }
155
+ else if (imageA > imageB) {
156
+ return 1;
157
+ }
167
158
  }
168
- requiresModifier(ctx) {
169
- return printTokenWithComments(this.getSingle(ctx));
159
+ return identifiersA.length - identifiersB.length;
160
+ }
161
+ function printToModuleNamesDirective(path, print, prefix) {
162
+ const directive = [prefix, " ", call(path, print, "packageName")];
163
+ if (path.node.children.moduleName) {
164
+ const moduleNames = join([",", line], map(path, print, "moduleName"));
165
+ directive.push(group(indent([line, group(indent(["to", line, ...moduleNames]))])));
170
166
  }
167
+ directive.push(";");
168
+ return directive;
171
169
  }
@@ -0,0 +1,46 @@
1
+ import { builders } from "prettier/doc";
2
+ import { printClassType, printSingle } from "./helpers.js";
3
+ declare const _default: {
4
+ primitiveType(path: import("prettier").AstPath<import("../../../java-parser/api.js").PrimitiveTypeCstNode & {
5
+ comments?: import("../comments.js").JavaComment[];
6
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
7
+ numericType: typeof printSingle;
8
+ integralType: typeof printSingle;
9
+ floatingPointType: typeof printSingle;
10
+ referenceType(path: import("prettier").AstPath<import("../../../java-parser/api.js").ReferenceTypeCstNode & {
11
+ comments?: import("../comments.js").JavaComment[];
12
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
13
+ classOrInterfaceType: typeof printSingle;
14
+ classType: typeof printClassType;
15
+ interfaceType: typeof printSingle;
16
+ typeVariable(path: import("prettier").AstPath<import("../../../java-parser/api.js").TypeVariableCstNode & {
17
+ comments?: import("../comments.js").JavaComment[];
18
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
19
+ dims(path: import("prettier").AstPath<import("../../../java-parser/api.js").DimsCstNode & {
20
+ comments?: import("../comments.js").JavaComment[];
21
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
22
+ typeParameter(path: import("prettier").AstPath<import("../../../java-parser/api.js").TypeParameterCstNode & {
23
+ comments?: import("../comments.js").JavaComment[];
24
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
25
+ typeParameterModifier: typeof printSingle;
26
+ typeBound(path: import("prettier").AstPath<import("../../../java-parser/api.js").TypeBoundCstNode & {
27
+ comments?: import("../comments.js").JavaComment[];
28
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
29
+ additionalBound(path: import("prettier").AstPath<import("../../../java-parser/api.js").AdditionalBoundCstNode & {
30
+ comments?: import("../comments.js").JavaComment[];
31
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
32
+ typeArguments(path: import("prettier").AstPath<import("../../../java-parser/api.js").TypeArgumentsCstNode & {
33
+ comments?: import("../comments.js").JavaComment[];
34
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Group;
35
+ typeArgumentList(path: import("prettier").AstPath<import("../../../java-parser/api.js").TypeArgumentListCstNode & {
36
+ comments?: import("../comments.js").JavaComment[];
37
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
38
+ typeArgument: typeof printSingle;
39
+ wildcard(path: import("prettier").AstPath<import("../../../java-parser/api.js").WildcardCstNode & {
40
+ comments?: import("../comments.js").JavaComment[];
41
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
42
+ wildcardBounds(path: import("prettier").AstPath<import("../../../java-parser/api.js").WildcardBoundsCstNode & {
43
+ comments?: import("../comments.js").JavaComment[];
44
+ }>, print: import("./helpers.js").JavaPrintFn): builders.Doc[];
45
+ };
46
+ export default _default;
@@ -1,153 +1,90 @@
1
- import forEach from "lodash/forEach.js";
2
1
  import { builders } from "prettier/doc";
3
- import { concat, group, indent, join } from "./prettier-builder.js";
4
- import { printTokenWithComments } from "./comments/format-comments.js";
5
- import { putIntoBraces, rejectAndConcat, rejectAndJoin, rejectAndJoinSeps, sortClassTypeChildren } from "./printer-utils.js";
6
- import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
7
- import { isAnnotationCstNode, isCstNode, isTypeArgumentsCstNode } from "../types/utils.js";
8
- const { line, softline } = builders;
9
- export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrinter {
10
- primitiveType(ctx) {
11
- const annotations = this.mapVisit(ctx.annotation);
12
- const type = ctx.numericType
13
- ? this.visit(ctx.numericType)
14
- : this.getSingle(ctx);
15
- return rejectAndJoin(" ", [join(" ", annotations), type]);
16
- }
17
- numericType(ctx) {
18
- return this.visitSingle(ctx);
19
- }
20
- integralType(ctx) {
21
- return printTokenWithComments(this.getSingle(ctx));
22
- }
23
- floatingPointType(ctx) {
24
- return printTokenWithComments(this.getSingle(ctx));
25
- }
26
- referenceType(ctx) {
27
- const annotations = this.mapVisit(ctx.annotation);
28
- const type = ctx.primitiveType
29
- ? this.visit(ctx.primitiveType)
30
- : this.visit(ctx.classOrInterfaceType);
31
- const dims = this.visit(ctx.dims);
32
- return rejectAndJoin(" ", [join(" ", annotations), concat([type, dims])]);
33
- }
34
- classOrInterfaceType(ctx) {
35
- return this.visitSingle(ctx);
36
- }
37
- classType(ctx) {
38
- const tokens = sortClassTypeChildren(ctx.annotation, ctx.typeArguments, ctx.Identifier);
39
- const segments = [];
40
- let currentSegment = [];
41
- forEach(tokens, (token, i) => {
42
- if (isTypeArgumentsCstNode(token)) {
43
- currentSegment.push(this.visit([token]));
44
- segments.push(rejectAndConcat(currentSegment));
45
- currentSegment = [];
46
- }
47
- else if (isAnnotationCstNode(token)) {
48
- currentSegment.push(this.visit([token]), " ");
49
- }
50
- else {
51
- currentSegment.push(token);
52
- if ((i + 1 < tokens.length && !isTypeArgumentsCstNode(tokens[i + 1])) ||
53
- i + 1 === tokens.length) {
54
- segments.push(rejectAndConcat(currentSegment));
55
- currentSegment = [];
56
- }
57
- }
58
- });
59
- return rejectAndJoinSeps(ctx.Dot, segments);
60
- }
61
- interfaceType(ctx) {
62
- return this.visitSingle(ctx);
63
- }
64
- typeVariable(ctx) {
65
- const annotations = this.mapVisit(ctx.annotation);
66
- const identifier = this.getSingle(ctx);
67
- return rejectAndJoin(" ", [join(" ", annotations), identifier]);
68
- }
69
- dims(ctx) {
70
- let tokens = [...ctx.LSquare];
71
- if (ctx.annotation) {
72
- tokens = [...tokens, ...ctx.annotation];
73
- }
74
- tokens = tokens.sort((a, b) => {
75
- const startOffset1 = isCstNode(a)
76
- ? a.children.At[0].startOffset
77
- : a.startOffset;
78
- const startOffset2 = isCstNode(b)
79
- ? b.children.At[0].startOffset
80
- : b.startOffset;
81
- return startOffset1 - startOffset2;
82
- });
83
- const segments = [];
84
- let currentSegment = [];
85
- forEach(tokens, token => {
86
- if (isCstNode(token)) {
87
- currentSegment.push(this.visit([token]));
88
- }
89
- else {
90
- segments.push(rejectAndConcat([
91
- rejectAndJoin(" ", currentSegment),
92
- concat([ctx.LSquare[0], ctx.RSquare[0]])
93
- ]));
94
- currentSegment = [];
95
- }
96
- });
97
- return rejectAndConcat(segments);
98
- }
99
- typeParameter(ctx) {
100
- const typeParameterModifiers = this.mapVisit(ctx.typeParameterModifier);
101
- const typeIdentifier = this.visit(ctx.typeIdentifier);
102
- const typeBound = this.visit(ctx.typeBound);
103
- return rejectAndJoin(" ", [
104
- join(" ", typeParameterModifiers),
105
- typeIdentifier,
106
- typeBound
2
+ import { call, definedKeys, flatMap, isNonTerminal, map, onlyDefinedKey, printClassType, printList, printSingle } from "./helpers.js";
3
+ const { group, indent, join, line, softline } = builders;
4
+ export default {
5
+ primitiveType(path, print) {
6
+ const { children } = path.node;
7
+ const typeKey = onlyDefinedKey(children, ["Boolean", "numericType"]);
8
+ return join(" ", [
9
+ ...map(path, print, "annotation"),
10
+ call(path, print, typeKey)
107
11
  ]);
108
- }
109
- typeParameterModifier(ctx) {
110
- return this.visitSingle(ctx);
111
- }
112
- typeBound(ctx) {
113
- const classOrInterfaceType = this.visit(ctx.classOrInterfaceType);
114
- const additionalBound = this.mapVisit(ctx.additionalBound);
115
- return concat([
116
- rejectAndJoin(" ", [ctx.Extends[0], classOrInterfaceType]),
117
- indent(group(concat([
118
- additionalBound.length ? line : "",
119
- rejectAndJoin(line, additionalBound)
120
- ])))
12
+ },
13
+ numericType: printSingle,
14
+ integralType: printSingle,
15
+ floatingPointType: printSingle,
16
+ referenceType(path, print) {
17
+ const { children } = path.node;
18
+ const typeKey = onlyDefinedKey(children, [
19
+ "primitiveType",
20
+ "classOrInterfaceType"
121
21
  ]);
122
- }
123
- additionalBound(ctx) {
124
- const interfaceType = this.visit(ctx.interfaceType);
125
- return join(" ", [ctx.And[0], interfaceType]);
126
- }
127
- typeArguments(ctx) {
128
- const typeArgumentList = this.visit(ctx.typeArgumentList);
129
- return putIntoBraces(typeArgumentList, softline, ctx.Less[0], ctx.Greater[0]);
130
- }
131
- typeArgumentList(ctx) {
132
- const typeArguments = this.mapVisit(ctx.typeArgument);
133
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
134
- return rejectAndJoinSeps(commas, typeArguments);
135
- }
136
- typeArgument(ctx) {
137
- return this.visitSingle(ctx);
138
- }
139
- wildcard(ctx) {
140
- const annotations = this.mapVisit(ctx.annotation);
141
- const wildcardBounds = this.visit(ctx.wildcardBounds);
142
- return rejectAndJoin(" ", [
143
- join(" ", annotations),
144
- ctx.QuestionMark[0],
145
- wildcardBounds
22
+ const type = call(path, print, typeKey);
23
+ return join(" ", [
24
+ ...map(path, print, "annotation"),
25
+ children.dims ? [type, call(path, print, "dims")] : type
146
26
  ]);
147
- }
148
- wildcardBounds(ctx) {
149
- const keyWord = ctx.Extends ? ctx.Extends[0] : ctx.Super[0];
150
- const referenceType = this.visit(ctx.referenceType);
151
- return join(" ", [keyWord, referenceType]);
152
- }
153
- }
27
+ },
28
+ classOrInterfaceType: printSingle,
29
+ classType: printClassType,
30
+ interfaceType: printSingle,
31
+ typeVariable(path, print) {
32
+ return join(" ", [
33
+ ...map(path, print, "annotation"),
34
+ call(path, print, "Identifier")
35
+ ]);
36
+ },
37
+ dims(path, print) {
38
+ return flatMap(path, childPath => {
39
+ const child = print(childPath);
40
+ return isNonTerminal(childPath.node) ? [child, " "] : child;
41
+ }, definedKeys(path.node.children, ["annotation", "LSquare", "RSquare"]));
42
+ },
43
+ typeParameter(path, print) {
44
+ const parameter = [
45
+ ...map(path, print, "typeParameterModifier"),
46
+ call(path, print, "typeIdentifier")
47
+ ];
48
+ if (path.node.children.typeBound) {
49
+ parameter.push(call(path, print, "typeBound"));
50
+ }
51
+ return join(" ", parameter);
52
+ },
53
+ typeParameterModifier: printSingle,
54
+ typeBound(path, print) {
55
+ const bound = ["extends ", call(path, print, "classOrInterfaceType")];
56
+ if (path.node.children.additionalBound) {
57
+ bound.push(group(indent([line, ...join(line, map(path, print, "additionalBound"))])));
58
+ }
59
+ return bound;
60
+ },
61
+ additionalBound(path, print) {
62
+ return ["& ", call(path, print, "interfaceType")];
63
+ },
64
+ typeArguments(path, print) {
65
+ return group([
66
+ "<",
67
+ indent([softline, call(path, print, "typeArgumentList")]),
68
+ softline,
69
+ ">"
70
+ ]);
71
+ },
72
+ typeArgumentList(path, print) {
73
+ return printList(path, print, "typeArgument");
74
+ },
75
+ typeArgument: printSingle,
76
+ wildcard(path, print) {
77
+ const wildcard = [...map(path, print, "annotation"), "?"];
78
+ if (path.node.children.wildcardBounds) {
79
+ wildcard.push(call(path, print, "wildcardBounds"));
80
+ }
81
+ return join(" ", wildcard);
82
+ },
83
+ wildcardBounds(path, print) {
84
+ return [
85
+ path.node.children.Extends ? "extends" : "super",
86
+ " ",
87
+ call(path, print, "referenceType")
88
+ ];
89
+ }
90
+ };
package/package.json CHANGED
@@ -1,22 +1,20 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "2.6.8",
3
+ "version": "2.7.0",
4
4
  "description": "Prettier Java Plugin",
5
5
  "type": "module",
6
6
  "exports": {
7
- "types": "./index.d.ts",
7
+ "types": "./dist/index.d.ts",
8
8
  "default": "./dist/index.js"
9
9
  },
10
10
  "files": [
11
- "dist",
12
- "index.d.ts"
11
+ "dist"
13
12
  ],
14
13
  "homepage": "https://jhipster.github.io/prettier-java/",
15
14
  "repository": "https://github.com/jhipster/prettier-java",
16
15
  "license": "Apache-2.0",
17
16
  "dependencies": {
18
- "java-parser": "2.3.4",
19
- "lodash": "4.17.21"
17
+ "java-parser": "3.0.0"
20
18
  },
21
19
  "scripts": {
22
20
  "test": "yarn run test:unit && yarn run test:e2e-core",
@@ -35,7 +33,6 @@
35
33
  "@types/fs-extra": "11.0.4",
36
34
  "@types/jest": "29.5.14",
37
35
  "@types/klaw-sync": "6.0.5",
38
- "@types/lodash": "4.17.13",
39
36
  "@types/node": "18.19.64",
40
37
  "@types/sinon": "17.0.3",
41
38
  "ts-node": "10.9.2",
@@ -44,5 +41,5 @@
44
41
  "peerDependencies": {
45
42
  "prettier": "^3.0.0"
46
43
  },
47
- "gitHead": "a1b05b474a157165133bff7a86c1d2b88bbfa5b4"
44
+ "gitHead": "593b07bbefbda0141f3816b8e0cf24af91a987fb"
48
45
  }