prettier-plugin-java 1.3.1 → 1.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.
Files changed (46) hide show
  1. package/dist/base-cst-printer.js +77 -0
  2. package/dist/cst-printer.js +37 -0
  3. package/dist/index.js +67 -0
  4. package/dist/options.js +256 -0
  5. package/dist/parser.js +7 -0
  6. package/dist/printer.js +28 -0
  7. package/dist/printers/arrays.js +49 -0
  8. package/dist/printers/blocks-and-statements.js +493 -0
  9. package/dist/printers/classes.js +724 -0
  10. package/dist/printers/comments/comments-utils.js +29 -0
  11. package/dist/printers/comments/format-comments.js +179 -0
  12. package/dist/printers/comments/handle-comments.js +38 -0
  13. package/dist/printers/expressions.js +549 -0
  14. package/dist/printers/interfaces.js +251 -0
  15. package/dist/printers/lexical-structure.js +43 -0
  16. package/dist/printers/names.js +53 -0
  17. package/dist/printers/packages-and-modules.js +185 -0
  18. package/dist/printers/prettier-builder.js +44 -0
  19. package/dist/printers/printer-utils.js +565 -0
  20. package/dist/printers/types-values-and-variables.js +183 -0
  21. package/dist/types/utils.js +29 -0
  22. package/dist/utils/constants.js +4 -0
  23. package/dist/utils/expressions-utils.js +29 -0
  24. package/dist/utils/index.js +10 -0
  25. package/dist/utils/printArgumentListWithBraces.js +21 -0
  26. package/dist/utils/printSingleLambdaInvocation.js +20 -0
  27. package/package.json +32 -10
  28. package/src/cst-printer.js +0 -145
  29. package/src/index.js +0 -79
  30. package/src/options.js +0 -256
  31. package/src/parser.js +0 -10
  32. package/src/printer.js +0 -31
  33. package/src/printers/arrays.js +0 -38
  34. package/src/printers/blocks-and-statements.js +0 -588
  35. package/src/printers/classes.js +0 -940
  36. package/src/printers/comments/comments-utils.js +0 -38
  37. package/src/printers/comments/format-comments.js +0 -223
  38. package/src/printers/comments/handle-comments.js +0 -50
  39. package/src/printers/expressions.js +0 -703
  40. package/src/printers/interfaces.js +0 -324
  41. package/src/printers/lexical-structure.js +0 -27
  42. package/src/printers/names.js +0 -42
  43. package/src/printers/packages-and-modules.js +0 -231
  44. package/src/printers/prettier-builder.js +0 -60
  45. package/src/printers/printer-utils.js +0 -715
  46. package/src/printers/types-values-and-variables.js +0 -202
@@ -1,940 +0,0 @@
1
- "use strict";
2
- const forEach = require("lodash/forEach");
3
- const { line, softline, hardline } = require("prettier").doc.builders;
4
- const {
5
- getBlankLinesSeparator,
6
- reject,
7
- rejectAndConcat,
8
- rejectAndJoin,
9
- sortClassTypeChildren,
10
- sortModifiers,
11
- rejectAndJoinSeps,
12
- displaySemicolon,
13
- putIntoBraces,
14
- getClassBodyDeclarationsSeparator,
15
- isStatementEmptyStatement
16
- } = require("./printer-utils");
17
- const { concat, join, group, indent } = require("./prettier-builder");
18
- const { printTokenWithComments } = require("./comments/format-comments");
19
- const {
20
- hasLeadingLineComments,
21
- hasLeadingComments
22
- } = require("./comments/comments-utils");
23
-
24
- class ClassesPrettierVisitor {
25
- classDeclaration(ctx) {
26
- const modifiers = sortModifiers(ctx.classModifier);
27
- const firstAnnotations = this.mapVisit(modifiers[0]);
28
- const otherModifiers = this.mapVisit(modifiers[1]);
29
-
30
- let classCST;
31
- if (ctx.normalClassDeclaration !== undefined) {
32
- classCST = ctx.normalClassDeclaration;
33
- } else if (ctx.enumDeclaration !== undefined) {
34
- classCST = ctx.enumDeclaration;
35
- } else {
36
- classCST = ctx.recordDeclaration;
37
- }
38
-
39
- const classDoc = this.visit(classCST);
40
-
41
- return rejectAndJoin(hardline, [
42
- rejectAndJoin(hardline, firstAnnotations),
43
- rejectAndJoin(" ", [join(" ", otherModifiers), classDoc])
44
- ]);
45
- }
46
-
47
- normalClassDeclaration(ctx) {
48
- const name = this.visit(ctx.typeIdentifier);
49
- const optionalTypeParams = this.visit(ctx.typeParameters);
50
- const optionalSuperClasses = this.visit(ctx.superclass);
51
- const optionalSuperInterfaces = this.visit(ctx.superinterfaces);
52
- const optionalClassPermits = this.visit(ctx.classPermits);
53
- const body = this.visit(ctx.classBody, { isNormalClassDeclaration: true });
54
-
55
- let superClassesPart = "";
56
- if (optionalSuperClasses) {
57
- superClassesPart = indent(rejectAndConcat([line, optionalSuperClasses]));
58
- }
59
-
60
- let superInterfacesPart = "";
61
- if (optionalSuperInterfaces) {
62
- superInterfacesPart = indent(
63
- rejectAndConcat([line, optionalSuperInterfaces])
64
- );
65
- }
66
-
67
- let classPermits = "";
68
- if (optionalClassPermits) {
69
- classPermits = indent(rejectAndConcat([line, optionalClassPermits]));
70
- }
71
-
72
- return rejectAndJoin(" ", [
73
- group(
74
- rejectAndConcat([
75
- rejectAndJoin(" ", [ctx.Class[0], name]),
76
- optionalTypeParams,
77
- superClassesPart,
78
- superInterfacesPart,
79
- classPermits
80
- ])
81
- ),
82
- body
83
- ]);
84
- }
85
-
86
- classModifier(ctx) {
87
- if (ctx.annotation) {
88
- return this.visit(ctx.annotation);
89
- }
90
- // public | protected | private | ...
91
- return printTokenWithComments(this.getSingle(ctx));
92
- }
93
-
94
- typeParameters(ctx) {
95
- const typeParameterList = this.visit(ctx.typeParameterList);
96
-
97
- return rejectAndConcat([ctx.Less[0], typeParameterList, ctx.Greater[0]]);
98
- }
99
-
100
- typeParameterList(ctx) {
101
- const typeParameter = this.mapVisit(ctx.typeParameter);
102
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
103
-
104
- return rejectAndJoinSeps(commas, typeParameter);
105
- }
106
-
107
- superclass(ctx) {
108
- return join(" ", [ctx.Extends[0], this.visit(ctx.classType)]);
109
- }
110
-
111
- superinterfaces(ctx) {
112
- const interfaceTypeList = this.visit(ctx.interfaceTypeList);
113
-
114
- return group(
115
- rejectAndConcat([
116
- ctx.Implements[0],
117
- indent(rejectAndConcat([line, interfaceTypeList]))
118
- ])
119
- );
120
- }
121
-
122
- classPermits(ctx) {
123
- const typeNames = this.mapVisit(ctx.typeName);
124
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
125
-
126
- return group(
127
- rejectAndConcat([
128
- ctx.Permits[0],
129
- indent(
130
- rejectAndConcat([line, group(rejectAndJoinSeps(commas, typeNames))])
131
- )
132
- ])
133
- );
134
- }
135
-
136
- interfaceTypeList(ctx) {
137
- const interfaceType = this.mapVisit(ctx.interfaceType);
138
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
139
-
140
- return group(rejectAndJoinSeps(commas, interfaceType));
141
- }
142
-
143
- classBody(ctx, param) {
144
- let content = "";
145
- if (ctx.classBodyDeclaration !== undefined) {
146
- const classBodyDeclsVisited = reject(
147
- this.mapVisit(ctx.classBodyDeclaration)
148
- );
149
-
150
- const separators = getClassBodyDeclarationsSeparator(
151
- ctx.classBodyDeclaration
152
- );
153
-
154
- content = rejectAndJoinSeps(separators, classBodyDeclsVisited);
155
-
156
- // edge case when we have SemiColons
157
- let shouldHardline = false;
158
- ctx.classBodyDeclaration.forEach(elt => {
159
- if (
160
- (elt.children.classMemberDeclaration &&
161
- !elt.children.classMemberDeclaration[0].children.Semicolon) ||
162
- elt.children.constructorDeclaration
163
- ) {
164
- shouldHardline = true;
165
- }
166
- });
167
-
168
- if (
169
- (ctx.classBodyDeclaration[0].children.classMemberDeclaration ||
170
- ctx.classBodyDeclaration[0].children.constructorDeclaration) &&
171
- shouldHardline &&
172
- param &&
173
- param.isNormalClassDeclaration
174
- ) {
175
- content = rejectAndConcat([hardline, content]);
176
- }
177
- }
178
-
179
- return putIntoBraces(content, hardline, ctx.LCurly[0], ctx.RCurly[0]);
180
- }
181
-
182
- classBodyDeclaration(ctx) {
183
- return this.visitSingle(ctx);
184
- }
185
-
186
- classMemberDeclaration(ctx) {
187
- if (ctx.Semicolon) {
188
- return displaySemicolon(ctx.Semicolon[0]);
189
- }
190
-
191
- return this.visitSingle(ctx);
192
- }
193
-
194
- fieldDeclaration(ctx) {
195
- const modifiers = sortModifiers(ctx.fieldModifier);
196
- const firstAnnotations = this.mapVisit(modifiers[0]);
197
- const otherModifiers = this.mapVisit(modifiers[1]);
198
-
199
- const unannType = this.visit(ctx.unannType);
200
- const variableDeclaratorList = this.visit(ctx.variableDeclaratorList);
201
-
202
- return rejectAndJoin(hardline, [
203
- rejectAndJoin(hardline, firstAnnotations),
204
- rejectAndJoin(" ", [
205
- rejectAndJoin(" ", otherModifiers),
206
- unannType,
207
- concat([variableDeclaratorList, ctx.Semicolon[0]])
208
- ])
209
- ]);
210
- }
211
-
212
- fieldModifier(ctx) {
213
- if (ctx.annotation) {
214
- return this.visit(ctx.annotation);
215
- }
216
- // public | protected | private | ...
217
- return printTokenWithComments(this.getSingle(ctx));
218
- }
219
-
220
- variableDeclaratorList(ctx) {
221
- const variableDeclarators = this.mapVisit(ctx.variableDeclarator);
222
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
223
- return rejectAndJoinSeps(commas, variableDeclarators);
224
- }
225
-
226
- variableDeclarator(ctx) {
227
- const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
228
- if (ctx.Equals) {
229
- const variableInitializer = this.visit(ctx.variableInitializer);
230
-
231
- if (hasLeadingLineComments(ctx.variableInitializer[0])) {
232
- return group(
233
- indent(
234
- rejectAndJoin(hardline, [
235
- rejectAndJoin(" ", [variableDeclaratorId, ctx.Equals[0]]),
236
- variableInitializer
237
- ])
238
- )
239
- );
240
- }
241
-
242
- if (
243
- // Array Initialisation
244
- ctx.variableInitializer[0].children.arrayInitializer !== undefined ||
245
- // Lambda expression
246
- ctx.variableInitializer[0].children.expression[0].children
247
- .lambdaExpression !== undefined ||
248
- // Ternary Expression
249
- (ctx.variableInitializer[0].children.expression[0].children
250
- .ternaryExpression !== undefined &&
251
- ctx.variableInitializer[0].children.expression[0].children
252
- .ternaryExpression[0].children.QuestionMark !== undefined)
253
- ) {
254
- return rejectAndJoin(" ", [
255
- variableDeclaratorId,
256
- ctx.Equals[0],
257
- variableInitializer
258
- ]);
259
- }
260
-
261
- if (
262
- ctx.variableInitializer[0].children.expression[0].children
263
- .ternaryExpression !== undefined
264
- ) {
265
- const firstPrimary =
266
- ctx.variableInitializer[0].children.expression[0].children
267
- .ternaryExpression[0].children.binaryExpression[0].children
268
- .unaryExpression[0].children.primary[0];
269
-
270
- // Cast Expression
271
- if (
272
- firstPrimary.children.primaryPrefix[0].children.castExpression !==
273
- undefined
274
- ) {
275
- return rejectAndJoin(" ", [
276
- variableDeclaratorId,
277
- ctx.Equals[0],
278
- variableInitializer
279
- ]);
280
- }
281
-
282
- // New Expression
283
- if (
284
- firstPrimary.children.primaryPrefix[0].children.newExpression !==
285
- undefined
286
- ) {
287
- return rejectAndJoin(" ", [
288
- variableDeclaratorId,
289
- ctx.Equals[0],
290
- variableInitializer
291
- ]);
292
- }
293
-
294
- // Method Invocation
295
- const isMethodInvocation =
296
- firstPrimary.children.primarySuffix !== undefined &&
297
- firstPrimary.children.primarySuffix[0].children
298
- .methodInvocationSuffix !== undefined;
299
- const isUniqueUnaryExpression =
300
- ctx.variableInitializer[0].children.expression[0].children
301
- .ternaryExpression[0].children.binaryExpression[0].children
302
- .unaryExpression.length === 1;
303
-
304
- const isUniqueMethodInvocation =
305
- isMethodInvocation && isUniqueUnaryExpression;
306
- if (isUniqueMethodInvocation) {
307
- return rejectAndJoin(" ", [
308
- variableDeclaratorId,
309
- ctx.Equals[0],
310
- variableInitializer
311
- ]);
312
- }
313
- }
314
-
315
- return group(
316
- indent(
317
- rejectAndJoin(line, [
318
- rejectAndJoin(" ", [variableDeclaratorId, ctx.Equals[0]]),
319
- variableInitializer
320
- ])
321
- )
322
- );
323
- }
324
- return variableDeclaratorId;
325
- }
326
-
327
- variableDeclaratorId(ctx) {
328
- const identifier = ctx.Identifier[0];
329
- const dims = this.visit(ctx.dims);
330
-
331
- return rejectAndConcat([identifier, dims]);
332
- }
333
-
334
- variableInitializer(ctx) {
335
- return this.visitSingle(ctx);
336
- }
337
-
338
- unannType(ctx) {
339
- return this.visitSingle(ctx);
340
- }
341
-
342
- unannPrimitiveTypeWithOptionalDimsSuffix(ctx) {
343
- const unannPrimitiveType = this.visit(ctx.unannPrimitiveType);
344
- const dims = this.visit(ctx.dims);
345
-
346
- return rejectAndConcat([unannPrimitiveType, dims]);
347
- }
348
-
349
- unannPrimitiveType(ctx) {
350
- if (ctx.numericType) {
351
- return this.visitSingle(ctx);
352
- }
353
- return printTokenWithComments(this.getSingle(ctx));
354
- }
355
-
356
- unannReferenceType(ctx) {
357
- const unannClassOrInterfaceType = this.visit(ctx.unannClassOrInterfaceType);
358
- const dims = this.visit(ctx.dims);
359
-
360
- return rejectAndConcat([unannClassOrInterfaceType, dims]);
361
- }
362
-
363
- unannClassOrInterfaceType(ctx) {
364
- return this.visit(ctx.unannClassType);
365
- }
366
-
367
- unannClassType(ctx) {
368
- const tokens = sortClassTypeChildren(
369
- ctx.annotation,
370
- ctx.typeArguments,
371
- ctx.Identifier
372
- );
373
-
374
- const segments = [];
375
- let currentSegment = [];
376
-
377
- forEach(tokens, (token, i) => {
378
- if (token.name === "typeArguments") {
379
- currentSegment.push(this.visit([token]));
380
- segments.push(rejectAndConcat(currentSegment));
381
- currentSegment = [];
382
- } else if (token.name === "annotation") {
383
- currentSegment.push(this.visit([token]));
384
- currentSegment.push(" ");
385
- } else {
386
- currentSegment.push(token);
387
- if (
388
- (i + 1 < tokens.length && tokens[i + 1].name !== "typeArguments") ||
389
- i + 1 === tokens.length
390
- ) {
391
- segments.push(rejectAndConcat(currentSegment));
392
- currentSegment = [];
393
- }
394
- }
395
- });
396
-
397
- return rejectAndJoinSeps(ctx.Dot, segments);
398
- }
399
-
400
- unannInterfaceType(ctx) {
401
- return this.visit(ctx.unannClassType);
402
- }
403
-
404
- unannTypeVariable(ctx) {
405
- return printTokenWithComments(this.getSingle(ctx));
406
- }
407
-
408
- methodDeclaration(ctx) {
409
- const modifiers = sortModifiers(ctx.methodModifier);
410
- const firstAnnotations = this.mapVisit(modifiers[0]);
411
- const otherModifiers = this.mapVisit(modifiers[1]);
412
-
413
- const header = this.visit(ctx.methodHeader);
414
- const body = this.visit(ctx.methodBody);
415
-
416
- const headerBodySeparator = isStatementEmptyStatement(body) ? "" : " ";
417
-
418
- return rejectAndJoin(hardline, [
419
- rejectAndJoin(hardline, firstAnnotations),
420
- rejectAndJoin(" ", [
421
- rejectAndJoin(" ", otherModifiers),
422
- rejectAndJoin(headerBodySeparator, [header, body])
423
- ])
424
- ]);
425
- }
426
-
427
- methodModifier(ctx) {
428
- if (ctx.annotation) {
429
- return this.visit(ctx.annotation);
430
- }
431
- // public | protected | private | Synchronized | ...
432
- return printTokenWithComments(this.getSingle(ctx));
433
- }
434
-
435
- methodHeader(ctx) {
436
- const typeParameters = this.visit(ctx.typeParameters);
437
- const annotations = this.mapVisit(ctx.annotation);
438
- const result = this.visit(ctx.result);
439
- const declarator = this.visit(ctx.methodDeclarator);
440
- const throws = this.visit(ctx.throws);
441
-
442
- return group(
443
- concat([
444
- rejectAndJoin(" ", [
445
- typeParameters,
446
- rejectAndJoin(line, annotations),
447
- result,
448
- declarator,
449
- throws
450
- ])
451
- ])
452
- );
453
- }
454
-
455
- result(ctx) {
456
- if (ctx.unannType) {
457
- return this.visit(ctx.unannType);
458
- }
459
- // void
460
- return printTokenWithComments(this.getSingle(ctx));
461
- }
462
-
463
- methodDeclarator(ctx) {
464
- const identifier = printTokenWithComments(ctx.Identifier[0]);
465
- const formalParameterList = this.visit(ctx.formalParameterList);
466
- const dims = this.visit(ctx.dims);
467
-
468
- return rejectAndConcat([
469
- identifier,
470
- putIntoBraces(
471
- formalParameterList,
472
- softline,
473
- ctx.LBrace[0],
474
- ctx.RBrace[0]
475
- ),
476
- dims
477
- ]);
478
- }
479
-
480
- receiverParameter(ctx) {
481
- const annotations = this.mapVisit(ctx.annotation);
482
- const unannType = this.visit(ctx.unannType);
483
- const identifier = ctx.Identifier
484
- ? concat([ctx.Identifier[0], ctx.Dot[0]])
485
- : "";
486
-
487
- return rejectAndJoin("", [
488
- rejectAndJoin(" ", annotations),
489
- unannType,
490
- identifier,
491
- ctx.This[0]
492
- ]);
493
- }
494
-
495
- formalParameterList(ctx) {
496
- const formalParameter = this.mapVisit(ctx.formalParameter);
497
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
498
- return rejectAndJoinSeps(commas, formalParameter);
499
- }
500
-
501
- formalParameter(ctx) {
502
- return this.visitSingle(ctx);
503
- }
504
-
505
- variableParaRegularParameter(ctx) {
506
- const variableModifier = this.mapVisit(ctx.variableModifier);
507
- const unannType = this.visit(ctx.unannType);
508
- const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
509
-
510
- return rejectAndJoin(" ", [
511
- rejectAndJoin(" ", variableModifier),
512
- unannType,
513
- variableDeclaratorId
514
- ]);
515
- }
516
-
517
- variableArityParameter(ctx) {
518
- const variableModifier = this.mapVisit(ctx.variableModifier);
519
- const unannType = this.visit(ctx.unannType);
520
- const annotations = this.mapVisit(ctx.annotation);
521
- const identifier = ctx.Identifier[0];
522
-
523
- const unannTypePrinted =
524
- ctx.annotation === undefined
525
- ? concat([unannType, ctx.DotDotDot[0]])
526
- : unannType;
527
- const annotationsPrinted =
528
- ctx.annotation === undefined
529
- ? annotations
530
- : concat([rejectAndJoin(" ", annotations), ctx.DotDotDot[0]]);
531
-
532
- return rejectAndJoin(" ", [
533
- join(" ", variableModifier),
534
- unannTypePrinted,
535
- annotationsPrinted,
536
- identifier
537
- ]);
538
- }
539
-
540
- variableModifier(ctx) {
541
- if (ctx.annotation) {
542
- return this.visit(ctx.annotation);
543
- }
544
- return printTokenWithComments(this.getSingle(ctx));
545
- }
546
-
547
- throws(ctx) {
548
- const exceptionTypeList = this.visit(ctx.exceptionTypeList);
549
- const throwsDeclaration = join(" ", [ctx.Throws[0], exceptionTypeList]);
550
- return group(indent(rejectAndConcat([softline, throwsDeclaration])));
551
- }
552
-
553
- exceptionTypeList(ctx) {
554
- const exceptionTypes = this.mapVisit(ctx.exceptionType);
555
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
556
- return rejectAndJoinSeps(commas, exceptionTypes);
557
- }
558
-
559
- exceptionType(ctx) {
560
- return this.visitSingle(ctx);
561
- }
562
-
563
- methodBody(ctx) {
564
- if (ctx.block) {
565
- return this.visit(ctx.block);
566
- }
567
-
568
- return printTokenWithComments(this.getSingle(ctx));
569
- }
570
-
571
- instanceInitializer(ctx) {
572
- return this.visitSingle(ctx);
573
- }
574
-
575
- staticInitializer(ctx) {
576
- const block = this.visit(ctx.block);
577
-
578
- return join(" ", [ctx.Static[0], block]);
579
- }
580
-
581
- constructorDeclaration(ctx) {
582
- const modifiers = sortModifiers(ctx.constructorModifier);
583
- const firstAnnotations = this.mapVisit(modifiers[0]);
584
- const otherModifiers = this.mapVisit(modifiers[1]);
585
-
586
- const constructorDeclarator = this.visit(ctx.constructorDeclarator);
587
- const throws = this.visit(ctx.throws);
588
- const constructorBody = this.visit(ctx.constructorBody);
589
-
590
- return rejectAndJoin(" ", [
591
- group(
592
- rejectAndJoin(hardline, [
593
- rejectAndJoin(hardline, firstAnnotations),
594
- rejectAndJoin(" ", [
595
- join(" ", otherModifiers),
596
- constructorDeclarator,
597
- throws
598
- ])
599
- ])
600
- ),
601
- constructorBody
602
- ]);
603
- }
604
-
605
- constructorModifier(ctx) {
606
- if (ctx.annotation) {
607
- return this.visit(ctx.annotation);
608
- }
609
- // public | protected | private | Synchronized | ...
610
- return printTokenWithComments(this.getSingle(ctx));
611
- }
612
-
613
- constructorDeclarator(ctx) {
614
- const typeParameters = this.visit(ctx.typeParameters);
615
- const simpleTypeName = this.visit(ctx.simpleTypeName);
616
- const receiverParameter = this.visit(ctx.receiverParameter);
617
- const formalParameterList = this.visit(ctx.formalParameterList);
618
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
619
-
620
- return rejectAndJoin(" ", [
621
- typeParameters,
622
- concat([
623
- simpleTypeName,
624
- putIntoBraces(
625
- rejectAndJoinSeps(commas, [receiverParameter, formalParameterList]),
626
- softline,
627
- ctx.LBrace[0],
628
- ctx.RBrace[0]
629
- )
630
- ])
631
- ]);
632
- }
633
-
634
- simpleTypeName(ctx) {
635
- return printTokenWithComments(this.getSingle(ctx));
636
- }
637
-
638
- constructorBody(ctx) {
639
- const explicitConstructorInvocation = this.visit(
640
- ctx.explicitConstructorInvocation
641
- );
642
-
643
- const blockStatements = this.visit(ctx.blockStatements);
644
-
645
- return putIntoBraces(
646
- rejectAndJoin(hardline, [explicitConstructorInvocation, blockStatements]),
647
- hardline,
648
- ctx.LCurly[0],
649
- ctx.RCurly[0]
650
- );
651
- }
652
-
653
- explicitConstructorInvocation(ctx) {
654
- return this.visitSingle(ctx);
655
- }
656
-
657
- unqualifiedExplicitConstructorInvocation(ctx) {
658
- const typeArguments = this.visit(ctx.typeArguments);
659
- const keyWord = ctx.This ? ctx.This[0] : ctx.Super[0];
660
- const argumentList = this.visit(ctx.argumentList);
661
- return rejectAndConcat([
662
- typeArguments,
663
- keyWord,
664
- group(
665
- rejectAndConcat([
666
- putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0]),
667
- ctx.Semicolon[0]
668
- ])
669
- )
670
- ]);
671
- }
672
-
673
- qualifiedExplicitConstructorInvocation(ctx) {
674
- const expressionName = this.visit(ctx.expressionName);
675
- const typeArguments = this.visit(ctx.typeArguments);
676
- const argumentList = this.visit(ctx.argumentList);
677
-
678
- return rejectAndConcat([
679
- expressionName,
680
- ctx.Dot[0],
681
- typeArguments,
682
- ctx.Super[0],
683
- group(
684
- rejectAndConcat([
685
- putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0]),
686
- ctx.Semicolon[0]
687
- ])
688
- )
689
- ]);
690
- }
691
-
692
- enumDeclaration(ctx) {
693
- const classModifier = this.mapVisit(ctx.classModifier);
694
- const typeIdentifier = this.visit(ctx.typeIdentifier);
695
- const superinterfaces = this.visit(ctx.superinterfaces);
696
- const enumBody = this.visit(ctx.enumBody);
697
-
698
- return rejectAndJoin(" ", [
699
- join(" ", classModifier),
700
- ctx.Enum[0],
701
- typeIdentifier,
702
- superinterfaces,
703
- enumBody
704
- ]);
705
- }
706
-
707
- enumBody(ctx) {
708
- const enumConstantList = this.visit(ctx.enumConstantList);
709
- const enumBodyDeclarations = this.visit(ctx.enumBodyDeclarations);
710
-
711
- const hasEnumConstants = ctx.enumConstantList !== undefined;
712
- const hasNoClassBodyDeclarations =
713
- ctx.enumBodyDeclarations === undefined ||
714
- ctx.enumBodyDeclarations[0].children.classBodyDeclaration === undefined;
715
-
716
- // edge case: https://github.com/jhipster/prettier-java/issues/383
717
- const handleEnumBodyDeclarationsLeadingComments =
718
- !hasNoClassBodyDeclarations &&
719
- hasLeadingComments(ctx.enumBodyDeclarations[0])
720
- ? hardline
721
- : "";
722
-
723
- let optionalComma;
724
- if (
725
- hasEnumConstants &&
726
- hasNoClassBodyDeclarations &&
727
- this.prettierOptions.trailingComma !== "none"
728
- ) {
729
- optionalComma = ctx.Comma ? ctx.Comma[0] : ",";
730
- } else {
731
- optionalComma = ctx.Comma ? { ...ctx.Comma[0], image: "" } : "";
732
- }
733
-
734
- return putIntoBraces(
735
- rejectAndConcat([
736
- enumConstantList,
737
- optionalComma,
738
- handleEnumBodyDeclarationsLeadingComments,
739
- enumBodyDeclarations
740
- ]),
741
- hardline,
742
- ctx.LCurly[0],
743
- ctx.RCurly[0]
744
- );
745
- }
746
-
747
- enumConstantList(ctx) {
748
- const enumConstants = this.mapVisit(ctx.enumConstant);
749
-
750
- const blankLineSeparators = getBlankLinesSeparator(ctx.enumConstant);
751
- const commas = ctx.Comma
752
- ? ctx.Comma.map((elt, index) => concat([elt, blankLineSeparators[index]]))
753
- : [];
754
-
755
- return group(rejectAndJoinSeps(commas, enumConstants));
756
- }
757
-
758
- enumConstant(ctx) {
759
- const modifiers = sortModifiers(ctx.enumConstantModifier);
760
- const firstAnnotations = this.mapVisit(modifiers[0]);
761
- const otherModifiers = this.mapVisit(modifiers[1]);
762
-
763
- const identifier = ctx.Identifier[0];
764
- const argumentList = this.visit(ctx.argumentList);
765
- const classBody = this.visit(ctx.classBody);
766
-
767
- const optionnalBracesAndArgumentList = ctx.LBrace
768
- ? putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0])
769
- : "";
770
-
771
- return rejectAndJoin(hardline, [
772
- rejectAndJoin(hardline, firstAnnotations),
773
- rejectAndJoin(" ", [
774
- rejectAndJoin(" ", otherModifiers),
775
- rejectAndConcat([identifier, optionnalBracesAndArgumentList]),
776
- classBody
777
- ])
778
- ]);
779
- }
780
-
781
- enumConstantModifier(ctx) {
782
- return this.visitSingle(ctx);
783
- }
784
-
785
- enumBodyDeclarations(ctx) {
786
- if (ctx.classBodyDeclaration !== undefined) {
787
- const classBodyDeclaration = this.mapVisit(ctx.classBodyDeclaration);
788
-
789
- const separators = getClassBodyDeclarationsSeparator(
790
- ctx.classBodyDeclaration
791
- );
792
-
793
- return rejectAndJoin(concat([hardline, hardline]), [
794
- ctx.Semicolon[0],
795
- rejectAndJoinSeps(separators, classBodyDeclaration)
796
- ]);
797
- }
798
-
799
- return { ...ctx.Semicolon[0], image: "" };
800
- }
801
-
802
- recordDeclaration(ctx) {
803
- const name = this.visit(ctx.typeIdentifier);
804
- const optionalTypeParams = this.visit(ctx.typeParameters);
805
-
806
- const recordHeader = this.visit(ctx.recordHeader);
807
-
808
- let superInterfacesPart = "";
809
- const optionalSuperInterfaces = this.visit(ctx.superinterfaces);
810
- if (optionalSuperInterfaces) {
811
- superInterfacesPart = indent(
812
- rejectAndConcat([line, optionalSuperInterfaces])
813
- );
814
- }
815
-
816
- const body = this.visit(ctx.recordBody);
817
-
818
- return rejectAndJoin(" ", [
819
- group(
820
- rejectAndConcat([
821
- rejectAndJoin(" ", [ctx.Record[0], name]),
822
- optionalTypeParams,
823
- recordHeader,
824
- superInterfacesPart
825
- ])
826
- ),
827
- body
828
- ]);
829
- }
830
- recordHeader(ctx) {
831
- const recordComponentList = this.visit(ctx.recordComponentList);
832
- return putIntoBraces(
833
- recordComponentList,
834
- softline,
835
- ctx.LBrace[0],
836
- ctx.RBrace[0]
837
- );
838
- }
839
- recordComponentList(ctx) {
840
- const recordComponents = this.mapVisit(ctx.recordComponent);
841
- const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
842
-
843
- return rejectAndJoinSeps(commas, recordComponents);
844
- }
845
- recordComponent(ctx) {
846
- const modifiers = this.mapVisit(ctx.recordComponentModifier);
847
- const unannType = this.visit(ctx.unannType);
848
-
849
- if (ctx.Identifier !== undefined) {
850
- return rejectAndJoin(" ", [
851
- join(" ", modifiers),
852
- unannType,
853
- ctx.Identifier[0]
854
- ]);
855
- }
856
-
857
- const variableArityRecordComponent = this.visit(
858
- ctx.variableArityRecordComponent
859
- );
860
- if (ctx.variableArityRecordComponent[0].children.annotation !== undefined) {
861
- return rejectAndJoin(" ", [
862
- join(" ", modifiers),
863
- join(" ", [unannType, variableArityRecordComponent])
864
- ]);
865
- }
866
-
867
- return rejectAndJoin(" ", [
868
- join(" ", modifiers),
869
- concat([unannType, variableArityRecordComponent])
870
- ]);
871
- }
872
- variableArityRecordComponent(ctx) {
873
- const annotations = this.mapVisit(ctx.annotation);
874
- const identifier = ctx.Identifier[0];
875
-
876
- return rejectAndJoin(" ", [
877
- rejectAndConcat([rejectAndJoin(" ", annotations), ctx.DotDotDot[0]]),
878
- identifier
879
- ]);
880
- }
881
-
882
- recordComponentModifier(ctx) {
883
- return this.visitSingle(ctx);
884
- }
885
-
886
- recordBody(ctx) {
887
- return putIntoBraces(
888
- rejectAndJoinSeps(
889
- getBlankLinesSeparator(ctx.recordBodyDeclaration),
890
- this.mapVisit(ctx.recordBodyDeclaration)
891
- ),
892
- hardline,
893
- ctx.LCurly[0],
894
- ctx.RCurly[0]
895
- );
896
- }
897
-
898
- recordBodyDeclaration(ctx) {
899
- return this.visitSingle(ctx);
900
- }
901
-
902
- compactConstructorDeclaration(ctx) {
903
- const modifiers = sortModifiers(ctx.constructorModifier);
904
- const firstAnnotations = this.mapVisit(modifiers[0]);
905
- const otherModifiers = this.mapVisit(modifiers[1]);
906
-
907
- const name = this.visit(ctx.simpleTypeName);
908
- const constructorBody = this.visit(ctx.constructorBody);
909
-
910
- return rejectAndJoin(" ", [
911
- group(
912
- rejectAndJoin(hardline, [
913
- rejectAndJoin(hardline, firstAnnotations),
914
- rejectAndJoin(" ", [join(" ", otherModifiers), name])
915
- ])
916
- ),
917
- constructorBody
918
- ]);
919
- }
920
-
921
- isClassDeclaration() {
922
- return "isClassDeclaration";
923
- }
924
-
925
- identifyClassBodyDeclarationType() {
926
- return "identifyClassBodyDeclarationType";
927
- }
928
-
929
- isDims() {
930
- return "isDims";
931
- }
932
-
933
- isCompactConstructorDeclaration() {
934
- return "isCompactConstructorDeclaration";
935
- }
936
- }
937
-
938
- module.exports = {
939
- ClassesPrettierVisitor
940
- };