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