prettier-plugin-java 2.6.0 → 2.6.1

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.
@@ -1,698 +0,0 @@
1
- import forEach from "lodash/forEach.js";
2
- import { displaySemicolon, getBlankLinesSeparator, getClassBodyDeclarationsSeparator, isStatementEmptyStatement, putIntoBraces, reject, rejectAndConcat, rejectAndJoin, rejectAndJoinSeps, sortClassTypeChildren, sortModifiers } from "./printer-utils.js";
3
- import { concat, group, indent, join, indentIfBreak } from "./prettier-builder.js";
4
- import { printTokenWithComments } from "./comments/format-comments.js";
5
- import { hasLeadingComments, hasLeadingLineComments } from "./comments/comments-utils.js";
6
- import { builders } from "prettier/doc";
7
- import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
8
- import { isAnnotationCstNode, isTypeArgumentsCstNode } from "../types/utils.js";
9
- import { printArgumentListWithBraces } from "../utils/index.js";
10
- const { line, softline, hardline, lineSuffixBoundary } = builders;
11
- export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
12
- classDeclaration(ctx) {
13
- const modifiers = sortModifiers(ctx.classModifier);
14
- const firstAnnotations = this.mapVisit(modifiers[0]);
15
- const otherModifiers = this.mapVisit(modifiers[1]);
16
- let classCST;
17
- if (ctx.normalClassDeclaration !== undefined) {
18
- classCST = ctx.normalClassDeclaration;
19
- }
20
- else if (ctx.enumDeclaration !== undefined) {
21
- classCST = ctx.enumDeclaration;
22
- }
23
- else {
24
- classCST = ctx.recordDeclaration;
25
- }
26
- const classDoc = this.visit(classCST);
27
- return rejectAndJoin(hardline, [
28
- rejectAndJoin(hardline, firstAnnotations),
29
- rejectAndJoin(" ", [join(" ", otherModifiers), classDoc])
30
- ]);
31
- }
32
- normalClassDeclaration(ctx) {
33
- const name = this.visit(ctx.typeIdentifier);
34
- const optionalTypeParams = this.visit(ctx.typeParameters);
35
- const optionalSuperClasses = this.visit(ctx.superclass);
36
- const optionalSuperInterfaces = this.visit(ctx.superinterfaces);
37
- const optionalClassPermits = this.visit(ctx.classPermits);
38
- const body = this.visit(ctx.classBody, { isNormalClassDeclaration: true });
39
- let superClassesPart = "";
40
- if (optionalSuperClasses) {
41
- superClassesPart = indent(rejectAndConcat([line, optionalSuperClasses]));
42
- }
43
- let superInterfacesPart = "";
44
- if (optionalSuperInterfaces) {
45
- superInterfacesPart = indent(rejectAndConcat([line, optionalSuperInterfaces]));
46
- }
47
- let classPermits = "";
48
- if (optionalClassPermits) {
49
- classPermits = indent(rejectAndConcat([line, optionalClassPermits]));
50
- }
51
- return rejectAndJoin(" ", [
52
- group(rejectAndConcat([
53
- rejectAndJoin(" ", [ctx.Class[0], name]),
54
- optionalTypeParams,
55
- superClassesPart,
56
- superInterfacesPart,
57
- classPermits
58
- ])),
59
- body
60
- ]);
61
- }
62
- classModifier(ctx) {
63
- if (ctx.annotation) {
64
- return this.visit(ctx.annotation);
65
- }
66
- // public | protected | private | ...
67
- return printTokenWithComments(this.getSingle(ctx));
68
- }
69
- typeParameters(ctx) {
70
- const typeParameterList = this.visit(ctx.typeParameterList);
71
- return putIntoBraces(typeParameterList, softline, ctx.Less[0], ctx.Greater[0]);
72
- }
73
- typeParameterList(ctx) {
74
- const typeParameter = this.mapVisit(ctx.typeParameter);
75
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
76
- return group(rejectAndJoinSeps(commas, typeParameter));
77
- }
78
- superclass(ctx) {
79
- return join(" ", [ctx.Extends[0], this.visit(ctx.classType)]);
80
- }
81
- superinterfaces(ctx) {
82
- const interfaceTypeList = this.visit(ctx.interfaceTypeList);
83
- return group(rejectAndConcat([
84
- ctx.Implements[0],
85
- indent(rejectAndConcat([line, interfaceTypeList]))
86
- ]));
87
- }
88
- classPermits(ctx) {
89
- const typeNames = this.mapVisit(ctx.typeName);
90
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
91
- return group(rejectAndConcat([
92
- ctx.Permits[0],
93
- indent(rejectAndConcat([line, group(rejectAndJoinSeps(commas, typeNames))]))
94
- ]));
95
- }
96
- interfaceTypeList(ctx) {
97
- const interfaceType = this.mapVisit(ctx.interfaceType);
98
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
99
- return group(rejectAndJoinSeps(commas, interfaceType));
100
- }
101
- classBody(ctx, param) {
102
- let content = "";
103
- if (ctx.classBodyDeclaration !== undefined) {
104
- const classBodyDeclsVisited = reject(this.mapVisit(ctx.classBodyDeclaration));
105
- const separators = getClassBodyDeclarationsSeparator(ctx.classBodyDeclaration);
106
- content = rejectAndJoinSeps(separators, classBodyDeclsVisited);
107
- // edge case when we have SemiColons
108
- let shouldHardline = false;
109
- ctx.classBodyDeclaration.forEach(elt => {
110
- if ((elt.children.classMemberDeclaration &&
111
- !elt.children.classMemberDeclaration[0].children.Semicolon) ||
112
- elt.children.constructorDeclaration) {
113
- shouldHardline = true;
114
- }
115
- });
116
- if ((ctx.classBodyDeclaration[0].children.classMemberDeclaration ||
117
- ctx.classBodyDeclaration[0].children.constructorDeclaration) &&
118
- shouldHardline &&
119
- param &&
120
- param.isNormalClassDeclaration) {
121
- content = rejectAndConcat([hardline, content]);
122
- }
123
- }
124
- return putIntoBraces(content, hardline, ctx.LCurly[0], ctx.RCurly[0]);
125
- }
126
- classBodyDeclaration(ctx) {
127
- return this.visitSingle(ctx);
128
- }
129
- classMemberDeclaration(ctx) {
130
- if (ctx.Semicolon) {
131
- return displaySemicolon(ctx.Semicolon[0]);
132
- }
133
- return this.visitSingle(ctx);
134
- }
135
- fieldDeclaration(ctx) {
136
- const modifiers = sortModifiers(ctx.fieldModifier);
137
- const firstAnnotations = this.mapVisit(modifiers[0]);
138
- const otherModifiers = this.mapVisit(modifiers[1]);
139
- const unannType = this.visit(ctx.unannType);
140
- const variableDeclaratorList = this.visit(ctx.variableDeclaratorList);
141
- return rejectAndJoin(hardline, [
142
- rejectAndJoin(hardline, firstAnnotations),
143
- rejectAndJoin(" ", [
144
- rejectAndJoin(" ", otherModifiers),
145
- unannType,
146
- concat([variableDeclaratorList, ctx.Semicolon[0]])
147
- ])
148
- ]);
149
- }
150
- fieldModifier(ctx) {
151
- if (ctx.annotation) {
152
- return this.visit(ctx.annotation);
153
- }
154
- // public | protected | private | ...
155
- return printTokenWithComments(this.getSingle(ctx));
156
- }
157
- variableDeclaratorList(ctx) {
158
- const variableDeclarators = this.mapVisit(ctx.variableDeclarator);
159
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
160
- return rejectAndJoinSeps(commas, variableDeclarators);
161
- }
162
- variableDeclarator(ctx) {
163
- const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
164
- if (ctx.Equals) {
165
- const variableInitializer = this.visit(ctx.variableInitializer);
166
- if (hasLeadingLineComments(ctx.variableInitializer[0])) {
167
- return group(indent(rejectAndJoin(hardline, [
168
- rejectAndJoin(" ", [variableDeclaratorId, ctx.Equals[0]]),
169
- variableInitializer
170
- ])));
171
- }
172
- if (
173
- // Array Initialisation
174
- ctx.variableInitializer[0].children.arrayInitializer !== undefined ||
175
- // Lambda expression
176
- ctx.variableInitializer[0].children.expression[0].children
177
- .lambdaExpression !== undefined ||
178
- // Ternary Expression
179
- (ctx.variableInitializer[0].children.expression[0].children
180
- .ternaryExpression !== undefined &&
181
- ctx.variableInitializer[0].children.expression[0].children
182
- .ternaryExpression[0].children.QuestionMark !== undefined)) {
183
- const groupId = Symbol("assignment");
184
- return group([
185
- group(variableDeclaratorId),
186
- " ",
187
- ctx.Equals[0],
188
- group(indent(line), { id: groupId }),
189
- lineSuffixBoundary,
190
- indentIfBreak(variableInitializer, { groupId })
191
- ]);
192
- }
193
- if (ctx.variableInitializer[0].children.expression[0].children
194
- .ternaryExpression !== undefined) {
195
- const unaryExpressions = ctx.variableInitializer[0].children.expression[0].children
196
- .ternaryExpression[0].children.binaryExpression[0].children
197
- .unaryExpression;
198
- const firstPrimary = unaryExpressions[0].children.primary[0];
199
- // Cast Expression
200
- if (firstPrimary.children.primaryPrefix[0].children.castExpression !==
201
- undefined &&
202
- unaryExpressions.length === 1) {
203
- const groupId = Symbol("assignment");
204
- return group([
205
- group(variableDeclaratorId),
206
- " ",
207
- ctx.Equals[0],
208
- group(indent(line), { id: groupId }),
209
- lineSuffixBoundary,
210
- indentIfBreak(variableInitializer, { groupId })
211
- ]);
212
- }
213
- // New Expression
214
- if (firstPrimary.children.primaryPrefix[0].children.newExpression !==
215
- undefined) {
216
- const groupId = Symbol("assignment");
217
- return group([
218
- group(variableDeclaratorId),
219
- " ",
220
- ctx.Equals[0],
221
- group(indent(line), { id: groupId }),
222
- lineSuffixBoundary,
223
- indentIfBreak(variableInitializer, { groupId })
224
- ]);
225
- }
226
- // Method Invocation
227
- const isMethodInvocation = firstPrimary.children.primarySuffix !== undefined &&
228
- firstPrimary.children.primarySuffix[0].children
229
- .methodInvocationSuffix !== undefined;
230
- const isUniqueUnaryExpression = ctx.variableInitializer[0].children.expression[0].children
231
- .ternaryExpression[0].children.binaryExpression[0].children
232
- .unaryExpression.length === 1;
233
- const isUniqueMethodInvocation = isMethodInvocation && isUniqueUnaryExpression;
234
- if (isUniqueMethodInvocation) {
235
- const groupId = Symbol("assignment");
236
- return group([
237
- group(variableDeclaratorId),
238
- " ",
239
- ctx.Equals[0],
240
- group(indent(line), { id: groupId }),
241
- lineSuffixBoundary,
242
- indentIfBreak(variableInitializer, { groupId })
243
- ]);
244
- }
245
- }
246
- return group(indent(rejectAndJoin(line, [
247
- rejectAndJoin(" ", [variableDeclaratorId, ctx.Equals[0]]),
248
- variableInitializer
249
- ])));
250
- }
251
- return variableDeclaratorId;
252
- }
253
- variableDeclaratorId(ctx) {
254
- if (ctx.Underscore) {
255
- return printTokenWithComments(ctx.Underscore[0]);
256
- }
257
- const identifier = ctx.Identifier[0];
258
- const dims = this.visit(ctx.dims);
259
- return rejectAndConcat([identifier, dims]);
260
- }
261
- variableInitializer(ctx) {
262
- return this.visitSingle(ctx);
263
- }
264
- unannType(ctx) {
265
- return this.visitSingle(ctx);
266
- }
267
- unannPrimitiveTypeWithOptionalDimsSuffix(ctx) {
268
- const unannPrimitiveType = this.visit(ctx.unannPrimitiveType);
269
- const dims = this.visit(ctx.dims);
270
- return rejectAndConcat([unannPrimitiveType, dims]);
271
- }
272
- unannPrimitiveType(ctx) {
273
- if (ctx.numericType) {
274
- return this.visitSingle(ctx);
275
- }
276
- return printTokenWithComments(this.getSingle(ctx));
277
- }
278
- unannReferenceType(ctx) {
279
- const unannClassOrInterfaceType = this.visit(ctx.unannClassOrInterfaceType);
280
- const dims = this.visit(ctx.dims);
281
- return rejectAndConcat([unannClassOrInterfaceType, dims]);
282
- }
283
- unannClassOrInterfaceType(ctx) {
284
- return this.visit(ctx.unannClassType);
285
- }
286
- unannClassType(ctx) {
287
- const tokens = sortClassTypeChildren(ctx.annotation, ctx.typeArguments, ctx.Identifier);
288
- const segments = [];
289
- let currentSegment = [];
290
- forEach(tokens, (token, i) => {
291
- if (isTypeArgumentsCstNode(token)) {
292
- currentSegment.push(this.visit([token]));
293
- segments.push(rejectAndConcat(currentSegment));
294
- currentSegment = [];
295
- }
296
- else if (isAnnotationCstNode(token)) {
297
- currentSegment.push(this.visit([token]));
298
- currentSegment.push(" ");
299
- }
300
- else {
301
- currentSegment.push(token);
302
- if ((i + 1 < tokens.length && !isTypeArgumentsCstNode(tokens[i + 1])) ||
303
- i + 1 === tokens.length) {
304
- segments.push(rejectAndConcat(currentSegment));
305
- currentSegment = [];
306
- }
307
- }
308
- });
309
- return rejectAndJoinSeps(ctx.Dot, segments);
310
- }
311
- unannInterfaceType(ctx) {
312
- return this.visit(ctx.unannClassType);
313
- }
314
- unannTypeVariable(ctx) {
315
- return printTokenWithComments(this.getSingle(ctx));
316
- }
317
- methodDeclaration(ctx) {
318
- const modifiers = sortModifiers(ctx.methodModifier);
319
- const firstAnnotations = this.mapVisit(modifiers[0]);
320
- const otherModifiers = this.mapVisit(modifiers[1]);
321
- const header = this.visit(ctx.methodHeader);
322
- const body = this.visit(ctx.methodBody);
323
- const headerBodySeparator = isStatementEmptyStatement(body) ? "" : " ";
324
- return rejectAndJoin(hardline, [
325
- rejectAndJoin(hardline, firstAnnotations),
326
- rejectAndJoin(" ", [
327
- rejectAndJoin(" ", otherModifiers),
328
- rejectAndJoin(headerBodySeparator, [header, body])
329
- ])
330
- ]);
331
- }
332
- methodModifier(ctx) {
333
- if (ctx.annotation) {
334
- return this.visit(ctx.annotation);
335
- }
336
- // public | protected | private | Synchronized | ...
337
- return printTokenWithComments(this.getSingle(ctx));
338
- }
339
- methodHeader(ctx) {
340
- const typeParameters = this.visit(ctx.typeParameters);
341
- const annotations = this.mapVisit(ctx.annotation);
342
- const result = this.visit(ctx.result);
343
- const declarator = this.visit(ctx.methodDeclarator);
344
- const throws = this.visit(ctx.throws);
345
- return group(concat([
346
- rejectAndJoin(" ", [
347
- typeParameters,
348
- rejectAndJoin(line, annotations),
349
- result,
350
- declarator,
351
- throws
352
- ])
353
- ]));
354
- }
355
- result(ctx) {
356
- if (ctx.unannType) {
357
- return this.visit(ctx.unannType);
358
- }
359
- // void
360
- return printTokenWithComments(this.getSingle(ctx));
361
- }
362
- methodDeclarator(ctx) {
363
- var _a;
364
- const identifier = printTokenWithComments(ctx.Identifier[0]);
365
- const receiverParameter = this.visit(ctx.receiverParameter);
366
- const formalParameterList = this.visit(ctx.formalParameterList);
367
- const dims = this.visit(ctx.dims);
368
- return rejectAndConcat([
369
- identifier,
370
- putIntoBraces(rejectAndJoin(line, [
371
- rejectAndConcat([receiverParameter, (_a = ctx.Comma) === null || _a === void 0 ? void 0 : _a[0]]),
372
- formalParameterList
373
- ]), softline, ctx.LBrace[0], ctx.RBrace[0]),
374
- dims
375
- ]);
376
- }
377
- receiverParameter(ctx) {
378
- var _a, _b;
379
- const annotations = this.mapVisit(ctx.annotation);
380
- const unannType = this.visit(ctx.unannType);
381
- return rejectAndJoin(" ", [
382
- ...annotations,
383
- unannType,
384
- rejectAndConcat([(_a = ctx.Identifier) === null || _a === void 0 ? void 0 : _a[0], (_b = ctx.Dot) === null || _b === void 0 ? void 0 : _b[0], ctx.This[0]])
385
- ]);
386
- }
387
- formalParameterList(ctx) {
388
- const formalParameter = this.mapVisit(ctx.formalParameter);
389
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
390
- return rejectAndJoinSeps(commas, formalParameter);
391
- }
392
- formalParameter(ctx) {
393
- return this.visitSingle(ctx);
394
- }
395
- variableParaRegularParameter(ctx) {
396
- const variableModifier = this.mapVisit(ctx.variableModifier);
397
- const unannType = this.visit(ctx.unannType);
398
- const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
399
- return rejectAndJoin(" ", [
400
- rejectAndJoin(" ", variableModifier),
401
- unannType,
402
- variableDeclaratorId
403
- ]);
404
- }
405
- variableArityParameter(ctx) {
406
- const variableModifier = this.mapVisit(ctx.variableModifier);
407
- const unannType = this.visit(ctx.unannType);
408
- const annotations = this.mapVisit(ctx.annotation);
409
- const identifier = ctx.Identifier[0];
410
- const unannTypePrinted = ctx.annotation === undefined
411
- ? concat([unannType, ctx.DotDotDot[0]])
412
- : unannType;
413
- const annotationsPrinted = ctx.annotation === undefined
414
- ? annotations
415
- : concat([rejectAndJoin(" ", annotations), ctx.DotDotDot[0]]);
416
- return rejectAndJoin(" ", [
417
- join(" ", variableModifier),
418
- unannTypePrinted,
419
- annotationsPrinted,
420
- identifier
421
- ]);
422
- }
423
- variableModifier(ctx) {
424
- if (ctx.annotation) {
425
- return this.visit(ctx.annotation);
426
- }
427
- return printTokenWithComments(this.getSingle(ctx));
428
- }
429
- throws(ctx) {
430
- const exceptionTypeList = this.visit(ctx.exceptionTypeList);
431
- const throwsDeclaration = join(" ", [ctx.Throws[0], exceptionTypeList]);
432
- return group(indent(rejectAndConcat([softline, throwsDeclaration])));
433
- }
434
- exceptionTypeList(ctx) {
435
- const exceptionTypes = this.mapVisit(ctx.exceptionType);
436
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
437
- return rejectAndJoinSeps(commas, exceptionTypes);
438
- }
439
- exceptionType(ctx) {
440
- return this.visitSingle(ctx);
441
- }
442
- methodBody(ctx) {
443
- if (ctx.block) {
444
- return this.visit(ctx.block);
445
- }
446
- return printTokenWithComments(this.getSingle(ctx));
447
- }
448
- instanceInitializer(ctx) {
449
- return this.visitSingle(ctx);
450
- }
451
- staticInitializer(ctx) {
452
- const block = this.visit(ctx.block);
453
- return join(" ", [ctx.Static[0], block]);
454
- }
455
- constructorDeclaration(ctx) {
456
- const modifiers = sortModifiers(ctx.constructorModifier);
457
- const firstAnnotations = this.mapVisit(modifiers[0]);
458
- const otherModifiers = this.mapVisit(modifiers[1]);
459
- const constructorDeclarator = this.visit(ctx.constructorDeclarator);
460
- const throws = this.visit(ctx.throws);
461
- const constructorBody = this.visit(ctx.constructorBody);
462
- return rejectAndJoin(" ", [
463
- group(rejectAndJoin(hardline, [
464
- rejectAndJoin(hardline, firstAnnotations),
465
- rejectAndJoin(" ", [
466
- join(" ", otherModifiers),
467
- constructorDeclarator,
468
- throws
469
- ])
470
- ])),
471
- constructorBody
472
- ]);
473
- }
474
- constructorModifier(ctx) {
475
- if (ctx.annotation) {
476
- return this.visit(ctx.annotation);
477
- }
478
- // public | protected | private | Synchronized | ...
479
- return printTokenWithComments(this.getSingle(ctx));
480
- }
481
- constructorDeclarator(ctx) {
482
- var _a;
483
- const typeParameters = this.visit(ctx.typeParameters);
484
- const simpleTypeName = this.visit(ctx.simpleTypeName);
485
- const receiverParameter = this.visit(ctx.receiverParameter);
486
- const formalParameterList = this.visit(ctx.formalParameterList);
487
- return rejectAndJoin(" ", [
488
- typeParameters,
489
- concat([
490
- simpleTypeName,
491
- putIntoBraces(rejectAndJoin(line, [
492
- rejectAndConcat([receiverParameter, (_a = ctx.Comma) === null || _a === void 0 ? void 0 : _a[0]]),
493
- formalParameterList
494
- ]), softline, ctx.LBrace[0], ctx.RBrace[0])
495
- ])
496
- ]);
497
- }
498
- simpleTypeName(ctx) {
499
- return printTokenWithComments(this.getSingle(ctx));
500
- }
501
- constructorBody(ctx) {
502
- const explicitConstructorInvocation = this.visit(ctx.explicitConstructorInvocation);
503
- const blockStatements = this.visit(ctx.blockStatements);
504
- return putIntoBraces(rejectAndJoin(hardline, [explicitConstructorInvocation, blockStatements]), hardline, ctx.LCurly[0], ctx.RCurly[0]);
505
- }
506
- explicitConstructorInvocation(ctx) {
507
- return this.visitSingle(ctx);
508
- }
509
- unqualifiedExplicitConstructorInvocation(ctx) {
510
- const typeArguments = this.visit(ctx.typeArguments);
511
- const keyWord = ctx.This ? ctx.This[0] : ctx.Super[0];
512
- const argumentList = printArgumentListWithBraces.call(this, ctx.argumentList, ctx.RBrace[0], ctx.LBrace[0]);
513
- return rejectAndConcat([
514
- typeArguments,
515
- keyWord,
516
- group(rejectAndConcat([argumentList, ctx.Semicolon[0]]))
517
- ]);
518
- }
519
- qualifiedExplicitConstructorInvocation(ctx) {
520
- const expressionName = this.visit(ctx.expressionName);
521
- const typeArguments = this.visit(ctx.typeArguments);
522
- const argumentList = printArgumentListWithBraces.call(this, ctx.argumentList, ctx.RBrace[0], ctx.LBrace[0]);
523
- return rejectAndConcat([
524
- expressionName,
525
- ctx.Dot[0],
526
- typeArguments,
527
- ctx.Super[0],
528
- group(rejectAndConcat([argumentList, ctx.Semicolon[0]]))
529
- ]);
530
- }
531
- enumDeclaration(ctx) {
532
- const classModifier = this.mapVisit(ctx.classModifier);
533
- const typeIdentifier = this.visit(ctx.typeIdentifier);
534
- const superinterfaces = this.visit(ctx.superinterfaces);
535
- const enumBody = this.visit(ctx.enumBody);
536
- return rejectAndJoin(" ", [
537
- join(" ", classModifier),
538
- ctx.Enum[0],
539
- typeIdentifier,
540
- superinterfaces,
541
- enumBody
542
- ]);
543
- }
544
- enumBody(ctx) {
545
- const enumConstantList = this.visit(ctx.enumConstantList);
546
- const enumBodyDeclarations = this.visit(ctx.enumBodyDeclarations);
547
- const hasEnumConstants = ctx.enumConstantList !== undefined;
548
- const hasNoClassBodyDeclarations = ctx.enumBodyDeclarations === undefined ||
549
- ctx.enumBodyDeclarations[0].children.classBodyDeclaration === undefined;
550
- // edge case: https://github.com/jhipster/prettier-java/issues/383
551
- const handleEnumBodyDeclarationsLeadingComments = !hasNoClassBodyDeclarations &&
552
- hasLeadingComments(ctx.enumBodyDeclarations[0])
553
- ? hardline
554
- : "";
555
- let optionalComma;
556
- if (hasEnumConstants &&
557
- hasNoClassBodyDeclarations &&
558
- this.prettierOptions.trailingComma !== "none") {
559
- optionalComma = ctx.Comma ? ctx.Comma[0] : ",";
560
- }
561
- else {
562
- optionalComma = ctx.Comma ? Object.assign(Object.assign({}, ctx.Comma[0]), { image: "" }) : "";
563
- }
564
- return putIntoBraces(rejectAndConcat([
565
- enumConstantList,
566
- optionalComma,
567
- handleEnumBodyDeclarationsLeadingComments,
568
- enumBodyDeclarations
569
- ]), hardline, ctx.LCurly[0], ctx.RCurly[0]);
570
- }
571
- enumConstantList(ctx) {
572
- const enumConstants = this.mapVisit(ctx.enumConstant);
573
- const blankLineSeparators = getBlankLinesSeparator(ctx.enumConstant);
574
- const commas = ctx.Comma
575
- ? ctx.Comma.map((elt, index) => concat([elt, blankLineSeparators[index]]))
576
- : [];
577
- return group(rejectAndJoinSeps(commas, enumConstants));
578
- }
579
- enumConstant(ctx) {
580
- const modifiers = sortModifiers(ctx.enumConstantModifier);
581
- const firstAnnotations = this.mapVisit(modifiers[0]);
582
- const otherModifiers = this.mapVisit(modifiers[1]);
583
- const identifier = ctx.Identifier[0];
584
- const classBody = this.visit(ctx.classBody);
585
- const optionalBracesAndArgumentList = ctx.LBrace
586
- ? printArgumentListWithBraces.call(this, ctx.argumentList, ctx.RBrace[0], ctx.LBrace[0])
587
- : "";
588
- return rejectAndJoin(hardline, [
589
- rejectAndJoin(hardline, firstAnnotations),
590
- rejectAndJoin(" ", [
591
- rejectAndJoin(" ", otherModifiers),
592
- rejectAndConcat([identifier, optionalBracesAndArgumentList]),
593
- classBody
594
- ])
595
- ]);
596
- }
597
- enumConstantModifier(ctx) {
598
- return this.visitSingle(ctx);
599
- }
600
- enumBodyDeclarations(ctx) {
601
- if (ctx.classBodyDeclaration !== undefined) {
602
- const classBodyDeclaration = this.mapVisit(ctx.classBodyDeclaration);
603
- const separators = getClassBodyDeclarationsSeparator(ctx.classBodyDeclaration);
604
- return rejectAndJoin(concat([hardline, hardline]), [
605
- ctx.Semicolon[0],
606
- rejectAndJoinSeps(separators, classBodyDeclaration)
607
- ]);
608
- }
609
- return printTokenWithComments(Object.assign(Object.assign({}, ctx.Semicolon[0]), { image: "" }));
610
- }
611
- recordDeclaration(ctx) {
612
- const name = this.visit(ctx.typeIdentifier);
613
- const optionalTypeParams = this.visit(ctx.typeParameters);
614
- const recordHeader = this.visit(ctx.recordHeader);
615
- let superInterfacesPart = "";
616
- const optionalSuperInterfaces = this.visit(ctx.superinterfaces);
617
- if (optionalSuperInterfaces) {
618
- superInterfacesPart = indent(rejectAndConcat([line, optionalSuperInterfaces]));
619
- }
620
- const body = this.visit(ctx.recordBody);
621
- return rejectAndJoin(" ", [
622
- group(rejectAndConcat([
623
- rejectAndJoin(" ", [ctx.Record[0], name]),
624
- optionalTypeParams,
625
- recordHeader,
626
- superInterfacesPart
627
- ])),
628
- body
629
- ]);
630
- }
631
- recordHeader(ctx) {
632
- const recordComponentList = this.visit(ctx.recordComponentList);
633
- return putIntoBraces(recordComponentList, softline, ctx.LBrace[0], ctx.RBrace[0]);
634
- }
635
- recordComponentList(ctx) {
636
- const recordComponents = this.mapVisit(ctx.recordComponent);
637
- const blankLineSeparators = getBlankLinesSeparator(ctx.recordComponent, line);
638
- const commas = ctx.Comma
639
- ? ctx.Comma.map((elt, index) => concat([elt, blankLineSeparators[index]]))
640
- : [];
641
- return rejectAndJoinSeps(commas, recordComponents);
642
- }
643
- recordComponent(ctx) {
644
- const modifiers = this.mapVisit(ctx.recordComponentModifier);
645
- const unannType = this.visit(ctx.unannType);
646
- if (ctx.Identifier !== undefined) {
647
- return group(rejectAndJoin(line, [
648
- join(line, modifiers),
649
- join(" ", [unannType, ctx.Identifier[0]])
650
- ]));
651
- }
652
- const variableArityRecordComponent = this.visit(ctx.variableArityRecordComponent);
653
- if (ctx.variableArityRecordComponent[0].children.annotation !== undefined) {
654
- return group(rejectAndJoin(line, [
655
- join(line, modifiers),
656
- join(" ", [unannType, variableArityRecordComponent])
657
- ]));
658
- }
659
- return group(rejectAndJoin(line, [
660
- join(line, modifiers),
661
- concat([unannType, variableArityRecordComponent])
662
- ]));
663
- }
664
- variableArityRecordComponent(ctx) {
665
- const annotations = this.mapVisit(ctx.annotation);
666
- const identifier = ctx.Identifier[0];
667
- return rejectAndJoin(" ", [
668
- rejectAndConcat([rejectAndJoin(" ", annotations), ctx.DotDotDot[0]]),
669
- identifier
670
- ]);
671
- }
672
- recordComponentModifier(ctx) {
673
- return this.visitSingle(ctx);
674
- }
675
- recordBody(ctx) {
676
- return putIntoBraces(rejectAndJoinSeps(getBlankLinesSeparator(ctx.recordBodyDeclaration), this.mapVisit(ctx.recordBodyDeclaration)), hardline, ctx.LCurly[0], ctx.RCurly[0]);
677
- }
678
- recordBodyDeclaration(ctx) {
679
- return this.visitSingle(ctx);
680
- }
681
- compactConstructorDeclaration(ctx) {
682
- const modifiers = sortModifiers(ctx.constructorModifier);
683
- const firstAnnotations = this.mapVisit(modifiers[0]);
684
- const otherModifiers = this.mapVisit(modifiers[1]);
685
- const name = this.visit(ctx.simpleTypeName);
686
- const constructorBody = this.visit(ctx.constructorBody);
687
- return rejectAndJoin(" ", [
688
- group(rejectAndJoin(hardline, [
689
- rejectAndJoin(hardline, firstAnnotations),
690
- rejectAndJoin(" ", [join(" ", otherModifiers), name])
691
- ])),
692
- constructorBody
693
- ]);
694
- }
695
- isDims() {
696
- return "isDims";
697
- }
698
- }