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,435 +0,0 @@
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.pattern) {
173
- const patterns = this.mapVisit(ctx.pattern);
174
- const guard = this.visit(ctx.guard);
175
- const multiplePatterns = ctx.pattern.length > 1;
176
- const separator = multiplePatterns ? line : " ";
177
- const contents = join(separator, [
178
- Case,
179
- rejectAndJoinSeps(commas, patterns)
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
- whileStatement(ctx) {
206
- const expression = this.visit(ctx.expression);
207
- const statement = this.visit(ctx.statement[0], {
208
- allowEmptyStatement: true
209
- });
210
- const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
211
- return rejectAndJoin(" ", [
212
- ctx.While[0],
213
- rejectAndJoin(statementSeparator, [
214
- putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
215
- statement
216
- ])
217
- ]);
218
- }
219
- doStatement(ctx) {
220
- const statement = this.visit(ctx.statement[0], {
221
- allowEmptyStatement: true
222
- });
223
- const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
224
- const expression = this.visit(ctx.expression);
225
- return rejectAndJoin(" ", [
226
- rejectAndJoin(statementSeparator, [ctx.Do[0], statement]),
227
- ctx.While[0],
228
- rejectAndConcat([
229
- putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
230
- ctx.Semicolon[0]
231
- ])
232
- ]);
233
- }
234
- forStatement(ctx) {
235
- return this.visitSingle(ctx);
236
- }
237
- basicForStatement(ctx) {
238
- const forInit = this.visit(ctx.forInit);
239
- const expression = this.visit(ctx.expression);
240
- const forUpdate = this.visit(ctx.forUpdate);
241
- const statement = this.visit(ctx.statement[0], {
242
- allowEmptyStatement: true
243
- });
244
- const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
245
- return rejectAndConcat([
246
- rejectAndJoin(" ", [
247
- ctx.For[0],
248
- putIntoBraces(rejectAndConcat([
249
- forInit,
250
- rejectAndJoin(line, [ctx.Semicolon[0], expression]),
251
- rejectAndJoin(line, [ctx.Semicolon[1], forUpdate])
252
- ]), softline, ctx.LBrace[0], ctx.RBrace[0])
253
- ]),
254
- statementSeparator,
255
- statement
256
- ]);
257
- }
258
- forInit(ctx) {
259
- return this.visitSingle(ctx);
260
- }
261
- forUpdate(ctx) {
262
- return this.visitSingle(ctx);
263
- }
264
- statementExpressionList(ctx) {
265
- const statementExpressions = this.mapVisit(ctx.statementExpression);
266
- const commas = ctx.Comma
267
- ? ctx.Comma.map(elt => {
268
- return concat([printTokenWithComments(elt), " "]);
269
- })
270
- : [];
271
- return rejectAndJoinSeps(commas, statementExpressions);
272
- }
273
- enhancedForStatement(ctx) {
274
- const variableModifiers = this.mapVisit(ctx.variableModifier);
275
- const localVariableType = this.visit(ctx.localVariableType);
276
- const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
277
- const expression = this.visit(ctx.expression);
278
- const statement = this.visit(ctx.statement[0], {
279
- allowEmptyStatement: true
280
- });
281
- const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
282
- return rejectAndConcat([
283
- rejectAndJoin(" ", [ctx.For[0], ctx.LBrace[0]]),
284
- rejectAndJoin(" ", [
285
- rejectAndJoin(" ", variableModifiers),
286
- localVariableType,
287
- variableDeclaratorId
288
- ]),
289
- concat([" ", ctx.Colon[0], " "]),
290
- expression,
291
- concat([ctx.RBrace[0], statementSeparator]),
292
- statement
293
- ]);
294
- }
295
- breakStatement(ctx) {
296
- if (ctx.Identifier) {
297
- const identifier = ctx.Identifier[0];
298
- return rejectAndConcat([
299
- concat([ctx.Break[0], " "]),
300
- identifier,
301
- ctx.Semicolon[0]
302
- ]);
303
- }
304
- return concat([ctx.Break[0], ctx.Semicolon[0]]);
305
- }
306
- continueStatement(ctx) {
307
- if (ctx.Identifier) {
308
- const identifier = ctx.Identifier[0];
309
- return rejectAndConcat([
310
- concat([ctx.Continue[0], " "]),
311
- identifier,
312
- ctx.Semicolon[0]
313
- ]);
314
- }
315
- return rejectAndConcat([ctx.Continue[0], ctx.Semicolon[0]]);
316
- }
317
- returnStatement(ctx) {
318
- if (ctx.expression) {
319
- const expression = this.visit(ctx.expression, {
320
- addParenthesisToWrapStatement: true
321
- });
322
- return rejectAndConcat([
323
- concat([ctx.Return[0], " "]),
324
- expression,
325
- ctx.Semicolon[0]
326
- ]);
327
- }
328
- return rejectAndConcat([ctx.Return[0], ctx.Semicolon[0]]);
329
- }
330
- throwStatement(ctx) {
331
- const expression = this.visit(ctx.expression);
332
- return rejectAndConcat([
333
- concat([ctx.Throw[0], " "]),
334
- expression,
335
- ctx.Semicolon[0]
336
- ]);
337
- }
338
- synchronizedStatement(ctx) {
339
- const expression = this.visit(ctx.expression);
340
- const block = this.visit(ctx.block);
341
- return rejectAndConcat([
342
- join(" ", [
343
- ctx.Synchronized[0],
344
- concat([
345
- putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
346
- " "
347
- ])
348
- ]),
349
- block
350
- ]);
351
- }
352
- tryStatement(ctx) {
353
- if (ctx.tryWithResourcesStatement) {
354
- return this.visit(ctx.tryWithResourcesStatement);
355
- }
356
- const block = this.visit(ctx.block);
357
- const catches = this.visit(ctx.catches);
358
- const finallyBlock = this.visit(ctx.finally);
359
- return rejectAndJoin(" ", [ctx.Try[0], block, catches, finallyBlock]);
360
- }
361
- catches(ctx) {
362
- const catchClauses = this.mapVisit(ctx.catchClause);
363
- return rejectAndJoin(" ", catchClauses);
364
- }
365
- catchClause(ctx) {
366
- const catchFormalParameter = this.visit(ctx.catchFormalParameter);
367
- const block = this.visit(ctx.block);
368
- return rejectAndConcat([
369
- group(rejectAndConcat([
370
- rejectAndJoin(" ", [ctx.Catch[0], ctx.LBrace[0]]),
371
- indent(rejectAndConcat([softline, catchFormalParameter])),
372
- softline,
373
- concat([ctx.RBrace[0], " "])
374
- ])),
375
- block
376
- ]);
377
- }
378
- catchFormalParameter(ctx) {
379
- const variableModifiers = this.mapVisit(ctx.variableModifier);
380
- const catchType = this.visit(ctx.catchType);
381
- const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
382
- return rejectAndJoin(" ", [
383
- rejectAndJoin(" ", variableModifiers),
384
- catchType,
385
- variableDeclaratorId
386
- ]);
387
- }
388
- catchType(ctx) {
389
- const unannClassType = this.visit(ctx.unannClassType);
390
- const classTypes = this.mapVisit(ctx.classType);
391
- const ors = ctx.Or ? ctx.Or.map(elt => concat([line, elt, " "])) : [];
392
- return group(rejectAndJoinSeps(ors, [unannClassType, ...classTypes]));
393
- }
394
- finally(ctx) {
395
- const block = this.visit(ctx.block);
396
- return rejectAndJoin(" ", [ctx.Finally[0], block]);
397
- }
398
- tryWithResourcesStatement(ctx) {
399
- const resourceSpecification = this.visit(ctx.resourceSpecification);
400
- const block = this.visit(ctx.block);
401
- const catches = this.visit(ctx.catches);
402
- const finallyBlock = this.visit(ctx.finally);
403
- return rejectAndJoin(" ", [
404
- ctx.Try[0],
405
- resourceSpecification,
406
- block,
407
- catches,
408
- finallyBlock
409
- ]);
410
- }
411
- resourceSpecification(ctx) {
412
- const resourceList = this.visit(ctx.resourceList);
413
- const optionalSemicolon = ctx.Semicolon ? ctx.Semicolon[0] : "";
414
- return putIntoBraces(rejectAndConcat([resourceList, optionalSemicolon]), softline, ctx.LBrace[0], ctx.RBrace[0]);
415
- }
416
- resourceList(ctx) {
417
- const resources = this.mapVisit(ctx.resource);
418
- const semicolons = ctx.Semicolon
419
- ? ctx.Semicolon.map(elt => {
420
- return concat([elt, line]);
421
- })
422
- : [""];
423
- return rejectAndJoinSeps(semicolons, resources);
424
- }
425
- resource(ctx) {
426
- return this.visitSingle(ctx);
427
- }
428
- yieldStatement(ctx) {
429
- const expression = this.visit(ctx.expression);
430
- return join(" ", [ctx.Yield[0], concat([expression, ctx.Semicolon[0]])]);
431
- }
432
- variableAccess(ctx) {
433
- return this.visitSingle(ctx);
434
- }
435
- }