prettier-plugin-java 2.6.3 → 2.6.4

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.
@@ -0,0 +1,432 @@
1
+ import { builders } from "prettier/doc";
2
+ import { concat, group, indent, join } from "./prettier-builder.js";
3
+ import { printTokenWithComments } from "./comments/format-comments.js";
4
+ import { hasLeadingLineComments, hasTrailingLineComments } from "./comments/comments-utils.js";
5
+ import { displaySemicolon, getBlankLinesSeparator, isStatementEmptyStatement, putIntoBraces, rejectAndConcat, rejectAndJoin, rejectAndJoinSeps, rejectSeparators, sortModifiers } from "./printer-utils.js";
6
+ import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
7
+ const { line, softline, hardline } = builders;
8
+ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
9
+ block(ctx) {
10
+ const blockStatements = this.visit(ctx.blockStatements);
11
+ return putIntoBraces(blockStatements, hardline, ctx.LCurly[0], ctx.RCurly[0]);
12
+ }
13
+ blockStatements(ctx) {
14
+ const blockStatement = this.mapVisit(ctx.blockStatement);
15
+ const separators = rejectSeparators(getBlankLinesSeparator(ctx.blockStatement), blockStatement);
16
+ return rejectAndJoinSeps(separators, blockStatement);
17
+ }
18
+ blockStatement(ctx) {
19
+ return this.visitSingle(ctx);
20
+ }
21
+ localVariableDeclarationStatement(ctx) {
22
+ const localVariableDeclaration = this.visit(ctx.localVariableDeclaration);
23
+ return rejectAndConcat([localVariableDeclaration, ctx.Semicolon[0]]);
24
+ }
25
+ localVariableDeclaration(ctx) {
26
+ const modifiers = sortModifiers(ctx.variableModifier);
27
+ const firstAnnotations = this.mapVisit(modifiers[0]);
28
+ const finalModifiers = this.mapVisit(modifiers[1]);
29
+ const localVariableType = this.visit(ctx.localVariableType);
30
+ const variableDeclaratorList = this.visit(ctx.variableDeclaratorList);
31
+ return rejectAndJoin(hardline, [
32
+ rejectAndJoin(hardline, firstAnnotations),
33
+ rejectAndJoin(" ", [
34
+ rejectAndJoin(" ", finalModifiers),
35
+ localVariableType,
36
+ variableDeclaratorList
37
+ ])
38
+ ]);
39
+ }
40
+ localVariableType(ctx) {
41
+ if (ctx.unannType) {
42
+ return this.visitSingle(ctx);
43
+ }
44
+ return printTokenWithComments(this.getSingle(ctx));
45
+ }
46
+ statement(ctx, params) {
47
+ // handling Labeled statements comments
48
+ if (ctx.labeledStatement !== undefined) {
49
+ const newLabelStatement = Object.assign({}, ctx.labeledStatement[0]);
50
+ const newColon = Object.assign({}, ctx.labeledStatement[0].children.Colon[0]);
51
+ const newStatement = Object.assign({}, ctx.labeledStatement[0].children.statement[0]);
52
+ const labeledStatementLeadingComments = [];
53
+ if (newColon.trailingComments !== undefined) {
54
+ labeledStatementLeadingComments.push(...newColon.trailingComments);
55
+ delete newColon.trailingComments;
56
+ }
57
+ if (newStatement.leadingComments !== undefined) {
58
+ labeledStatementLeadingComments.push(...newStatement.leadingComments);
59
+ delete newStatement.leadingComments;
60
+ }
61
+ if (labeledStatementLeadingComments.length !== 0) {
62
+ newLabelStatement.leadingComments = labeledStatementLeadingComments;
63
+ }
64
+ newLabelStatement.children.Colon[0] = newColon;
65
+ newLabelStatement.children.statement[0] = newStatement;
66
+ return this.visit([newLabelStatement]);
67
+ }
68
+ return this.visitSingle(ctx, params);
69
+ }
70
+ statementWithoutTrailingSubstatement(ctx, params) {
71
+ return this.visitSingle(ctx, params);
72
+ }
73
+ emptyStatement(ctx, params) {
74
+ return displaySemicolon(ctx.Semicolon[0], params);
75
+ }
76
+ labeledStatement(ctx) {
77
+ const identifier = ctx.Identifier[0];
78
+ const statement = this.visit(ctx.statement);
79
+ return rejectAndJoin(ctx.Colon[0], [identifier, statement]);
80
+ }
81
+ expressionStatement(ctx) {
82
+ const statementExpression = this.visit(ctx.statementExpression);
83
+ return rejectAndConcat([statementExpression, ctx.Semicolon[0]]);
84
+ }
85
+ statementExpression(ctx) {
86
+ return this.visitSingle(ctx);
87
+ }
88
+ ifStatement(ctx) {
89
+ var _a;
90
+ const expression = this.visit(ctx.expression);
91
+ const ifStatement = this.visit(ctx.statement[0], {
92
+ allowEmptyStatement: true
93
+ });
94
+ const ifSeparator = isStatementEmptyStatement(ifStatement) ? "" : " ";
95
+ let elsePart = "";
96
+ if (ctx.Else !== undefined) {
97
+ const elseStatement = this.visit(ctx.statement[1], {
98
+ allowEmptyStatement: true
99
+ });
100
+ const elseSeparator = isStatementEmptyStatement(elseStatement) ? "" : " ";
101
+ const elseOnSameLine = hasTrailingLineComments(ctx.statement[0]) ||
102
+ hasLeadingLineComments(ctx.Else[0]) ||
103
+ !((_a = ctx.statement[0].children.statementWithoutTrailingSubstatement) === null || _a === void 0 ? void 0 : _a[0].children.block)
104
+ ? hardline
105
+ : " ";
106
+ elsePart = rejectAndJoin(elseSeparator, [
107
+ concat([elseOnSameLine, ctx.Else[0]]),
108
+ elseStatement
109
+ ]);
110
+ }
111
+ return rejectAndConcat([
112
+ rejectAndJoin(" ", [
113
+ ctx.If[0],
114
+ concat([
115
+ putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
116
+ ifSeparator
117
+ ])
118
+ ]),
119
+ ifStatement,
120
+ elsePart
121
+ ]);
122
+ }
123
+ assertStatement(ctx) {
124
+ const expressions = this.mapVisit(ctx.expression);
125
+ const colon = ctx.Colon ? ctx.Colon[0] : ":";
126
+ return rejectAndConcat([
127
+ concat([ctx.Assert[0], " "]),
128
+ rejectAndJoin(concat([" ", colon, " "]), expressions),
129
+ ctx.Semicolon[0]
130
+ ]);
131
+ }
132
+ switchStatement(ctx) {
133
+ const expression = this.visit(ctx.expression);
134
+ const switchBlock = this.visit(ctx.switchBlock);
135
+ return rejectAndJoin(" ", [
136
+ ctx.Switch[0],
137
+ putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
138
+ switchBlock
139
+ ]);
140
+ }
141
+ switchBlock(ctx) {
142
+ const switchCases = ctx.switchBlockStatementGroup !== undefined
143
+ ? this.mapVisit(ctx.switchBlockStatementGroup)
144
+ : this.mapVisit(ctx.switchRule);
145
+ return putIntoBraces(rejectAndJoin(hardline, switchCases), hardline, ctx.LCurly[0], ctx.RCurly[0]);
146
+ }
147
+ switchBlockStatementGroup(ctx) {
148
+ var _a, _b, _c;
149
+ const switchLabel = this.visit(ctx.switchLabel);
150
+ const blockStatements = this.visit(ctx.blockStatements);
151
+ const statements = (_a = ctx.blockStatements) === null || _a === void 0 ? void 0 : _a[0].children.blockStatement;
152
+ const hasSingleStatementBlock = (statements === null || statements === void 0 ? void 0 : statements.length) === 1 &&
153
+ ((_c = (_b = statements[0].children.statement) === null || _b === void 0 ? void 0 : _b[0].children.statementWithoutTrailingSubstatement) === null || _c === void 0 ? void 0 : _c[0].children.block) !== undefined;
154
+ return concat([
155
+ switchLabel,
156
+ ctx.Colon[0],
157
+ hasSingleStatementBlock
158
+ ? concat([" ", blockStatements])
159
+ : blockStatements && indent([hardline, blockStatements])
160
+ ]);
161
+ }
162
+ switchLabel(ctx) {
163
+ var _a, _b, _c;
164
+ const Case = (_a = ctx.Case) === null || _a === void 0 ? void 0 : _a[0];
165
+ const commas = (_b = ctx.Comma) === null || _b === void 0 ? void 0 : _b.map(elt => concat([elt, line]));
166
+ if (ctx.caseConstant || ctx.Null) {
167
+ const caseConstants = ctx.Null
168
+ ? [ctx.Null[0], (_c = ctx.Default) === null || _c === void 0 ? void 0 : _c[0]]
169
+ : this.mapVisit(ctx.caseConstant);
170
+ return group(indent(join(" ", [Case, rejectAndJoinSeps(commas, caseConstants)])));
171
+ }
172
+ else if (ctx.casePattern) {
173
+ const casePatterns = this.mapVisit(ctx.casePattern);
174
+ const guard = this.visit(ctx.guard);
175
+ const multiplePatterns = ctx.casePattern.length > 1;
176
+ const separator = multiplePatterns ? line : " ";
177
+ const contents = join(separator, [
178
+ Case,
179
+ rejectAndJoinSeps(commas, casePatterns)
180
+ ]);
181
+ return group(rejectAndJoin(separator, [
182
+ multiplePatterns ? indent(contents) : contents,
183
+ guard
184
+ ]));
185
+ }
186
+ return printTokenWithComments(ctx.Default[0]);
187
+ }
188
+ switchRule(ctx) {
189
+ const switchLabel = this.visit(ctx.switchLabel);
190
+ let caseInstruction;
191
+ if (ctx.throwStatement !== undefined) {
192
+ caseInstruction = this.visit(ctx.throwStatement);
193
+ }
194
+ else if (ctx.block !== undefined) {
195
+ caseInstruction = this.visit(ctx.block);
196
+ }
197
+ else {
198
+ caseInstruction = concat([this.visit(ctx.expression), ctx.Semicolon[0]]);
199
+ }
200
+ return concat([switchLabel, " ", ctx.Arrow[0], " ", caseInstruction]);
201
+ }
202
+ caseConstant(ctx) {
203
+ return this.visitSingle(ctx);
204
+ }
205
+ casePattern(ctx) {
206
+ return this.visitSingle(ctx);
207
+ }
208
+ whileStatement(ctx) {
209
+ const expression = this.visit(ctx.expression);
210
+ const statement = this.visit(ctx.statement[0], {
211
+ allowEmptyStatement: true
212
+ });
213
+ const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
214
+ return rejectAndJoin(" ", [
215
+ ctx.While[0],
216
+ rejectAndJoin(statementSeparator, [
217
+ putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
218
+ statement
219
+ ])
220
+ ]);
221
+ }
222
+ doStatement(ctx) {
223
+ const statement = this.visit(ctx.statement[0], {
224
+ allowEmptyStatement: true
225
+ });
226
+ const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
227
+ const expression = this.visit(ctx.expression);
228
+ return rejectAndJoin(" ", [
229
+ rejectAndJoin(statementSeparator, [ctx.Do[0], statement]),
230
+ ctx.While[0],
231
+ rejectAndConcat([
232
+ putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
233
+ ctx.Semicolon[0]
234
+ ])
235
+ ]);
236
+ }
237
+ forStatement(ctx) {
238
+ return this.visitSingle(ctx);
239
+ }
240
+ basicForStatement(ctx) {
241
+ const forInit = this.visit(ctx.forInit);
242
+ const expression = this.visit(ctx.expression);
243
+ const forUpdate = this.visit(ctx.forUpdate);
244
+ const statement = this.visit(ctx.statement[0], {
245
+ allowEmptyStatement: true
246
+ });
247
+ const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
248
+ return rejectAndConcat([
249
+ rejectAndJoin(" ", [
250
+ ctx.For[0],
251
+ putIntoBraces(rejectAndConcat([
252
+ forInit,
253
+ rejectAndJoin(line, [ctx.Semicolon[0], expression]),
254
+ rejectAndJoin(line, [ctx.Semicolon[1], forUpdate])
255
+ ]), softline, ctx.LBrace[0], ctx.RBrace[0])
256
+ ]),
257
+ statementSeparator,
258
+ statement
259
+ ]);
260
+ }
261
+ forInit(ctx) {
262
+ return this.visitSingle(ctx);
263
+ }
264
+ forUpdate(ctx) {
265
+ return this.visitSingle(ctx);
266
+ }
267
+ statementExpressionList(ctx) {
268
+ const statementExpressions = this.mapVisit(ctx.statementExpression);
269
+ const commas = ctx.Comma
270
+ ? ctx.Comma.map(elt => {
271
+ return concat([printTokenWithComments(elt), " "]);
272
+ })
273
+ : [];
274
+ return rejectAndJoinSeps(commas, statementExpressions);
275
+ }
276
+ enhancedForStatement(ctx) {
277
+ const localVariableDeclaration = this.visit(ctx.localVariableDeclaration);
278
+ const expression = this.visit(ctx.expression);
279
+ const statement = this.visit(ctx.statement[0], {
280
+ allowEmptyStatement: true
281
+ });
282
+ const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
283
+ return rejectAndConcat([
284
+ rejectAndJoin(" ", [ctx.For[0], ctx.LBrace[0]]),
285
+ localVariableDeclaration,
286
+ concat([" ", ctx.Colon[0], " "]),
287
+ expression,
288
+ concat([ctx.RBrace[0], statementSeparator]),
289
+ statement
290
+ ]);
291
+ }
292
+ breakStatement(ctx) {
293
+ if (ctx.Identifier) {
294
+ const identifier = ctx.Identifier[0];
295
+ return rejectAndConcat([
296
+ concat([ctx.Break[0], " "]),
297
+ identifier,
298
+ ctx.Semicolon[0]
299
+ ]);
300
+ }
301
+ return concat([ctx.Break[0], ctx.Semicolon[0]]);
302
+ }
303
+ continueStatement(ctx) {
304
+ if (ctx.Identifier) {
305
+ const identifier = ctx.Identifier[0];
306
+ return rejectAndConcat([
307
+ concat([ctx.Continue[0], " "]),
308
+ identifier,
309
+ ctx.Semicolon[0]
310
+ ]);
311
+ }
312
+ return rejectAndConcat([ctx.Continue[0], ctx.Semicolon[0]]);
313
+ }
314
+ returnStatement(ctx) {
315
+ if (ctx.expression) {
316
+ const expression = this.visit(ctx.expression, {
317
+ addParenthesisToWrapStatement: true
318
+ });
319
+ return rejectAndConcat([
320
+ concat([ctx.Return[0], " "]),
321
+ expression,
322
+ ctx.Semicolon[0]
323
+ ]);
324
+ }
325
+ return rejectAndConcat([ctx.Return[0], ctx.Semicolon[0]]);
326
+ }
327
+ throwStatement(ctx) {
328
+ const expression = this.visit(ctx.expression);
329
+ return rejectAndConcat([
330
+ concat([ctx.Throw[0], " "]),
331
+ expression,
332
+ ctx.Semicolon[0]
333
+ ]);
334
+ }
335
+ synchronizedStatement(ctx) {
336
+ const expression = this.visit(ctx.expression);
337
+ const block = this.visit(ctx.block);
338
+ return rejectAndConcat([
339
+ join(" ", [
340
+ ctx.Synchronized[0],
341
+ concat([
342
+ putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
343
+ " "
344
+ ])
345
+ ]),
346
+ block
347
+ ]);
348
+ }
349
+ tryStatement(ctx) {
350
+ if (ctx.tryWithResourcesStatement) {
351
+ return this.visit(ctx.tryWithResourcesStatement);
352
+ }
353
+ const block = this.visit(ctx.block);
354
+ const catches = this.visit(ctx.catches);
355
+ const finallyBlock = this.visit(ctx.finally);
356
+ return rejectAndJoin(" ", [ctx.Try[0], block, catches, finallyBlock]);
357
+ }
358
+ catches(ctx) {
359
+ const catchClauses = this.mapVisit(ctx.catchClause);
360
+ return rejectAndJoin(" ", catchClauses);
361
+ }
362
+ catchClause(ctx) {
363
+ const catchFormalParameter = this.visit(ctx.catchFormalParameter);
364
+ const block = this.visit(ctx.block);
365
+ return rejectAndConcat([
366
+ group(rejectAndConcat([
367
+ rejectAndJoin(" ", [ctx.Catch[0], ctx.LBrace[0]]),
368
+ indent(rejectAndConcat([softline, catchFormalParameter])),
369
+ softline,
370
+ concat([ctx.RBrace[0], " "])
371
+ ])),
372
+ block
373
+ ]);
374
+ }
375
+ catchFormalParameter(ctx) {
376
+ const variableModifiers = this.mapVisit(ctx.variableModifier);
377
+ const catchType = this.visit(ctx.catchType);
378
+ const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
379
+ return rejectAndJoin(" ", [
380
+ rejectAndJoin(" ", variableModifiers),
381
+ catchType,
382
+ variableDeclaratorId
383
+ ]);
384
+ }
385
+ catchType(ctx) {
386
+ const unannClassType = this.visit(ctx.unannClassType);
387
+ const classTypes = this.mapVisit(ctx.classType);
388
+ const ors = ctx.Or ? ctx.Or.map(elt => concat([line, elt, " "])) : [];
389
+ return group(rejectAndJoinSeps(ors, [unannClassType, ...classTypes]));
390
+ }
391
+ finally(ctx) {
392
+ const block = this.visit(ctx.block);
393
+ return rejectAndJoin(" ", [ctx.Finally[0], block]);
394
+ }
395
+ tryWithResourcesStatement(ctx) {
396
+ const resourceSpecification = this.visit(ctx.resourceSpecification);
397
+ const block = this.visit(ctx.block);
398
+ const catches = this.visit(ctx.catches);
399
+ const finallyBlock = this.visit(ctx.finally);
400
+ return rejectAndJoin(" ", [
401
+ ctx.Try[0],
402
+ resourceSpecification,
403
+ block,
404
+ catches,
405
+ finallyBlock
406
+ ]);
407
+ }
408
+ resourceSpecification(ctx) {
409
+ const resourceList = this.visit(ctx.resourceList);
410
+ const optionalSemicolon = ctx.Semicolon ? ctx.Semicolon[0] : "";
411
+ return putIntoBraces(rejectAndConcat([resourceList, optionalSemicolon]), softline, ctx.LBrace[0], ctx.RBrace[0]);
412
+ }
413
+ resourceList(ctx) {
414
+ const resources = this.mapVisit(ctx.resource);
415
+ const semicolons = ctx.Semicolon
416
+ ? ctx.Semicolon.map(elt => {
417
+ return concat([elt, line]);
418
+ })
419
+ : [""];
420
+ return rejectAndJoinSeps(semicolons, resources);
421
+ }
422
+ resource(ctx) {
423
+ return this.visitSingle(ctx);
424
+ }
425
+ yieldStatement(ctx) {
426
+ const expression = this.visit(ctx.expression);
427
+ return join(" ", [ctx.Yield[0], concat([expression, ctx.Semicolon[0]])]);
428
+ }
429
+ variableAccess(ctx) {
430
+ return this.visitSingle(ctx);
431
+ }
432
+ }