prettier-plugin-java 1.3.0 → 1.6.0

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/expressions-utils.js +29 -0
  23. package/dist/utils/index.js +10 -0
  24. package/dist/utils/printArgumentListWithBraces.js +21 -0
  25. package/dist/utils/printSingleLambdaInvocation.js +20 -0
  26. package/package.json +32 -10
  27. package/src/cst-printer.js +0 -145
  28. package/src/index.js +0 -79
  29. package/src/options.js +0 -256
  30. package/src/parser.js +0 -10
  31. package/src/printer.js +0 -31
  32. package/src/printers/arrays.js +0 -38
  33. package/src/printers/blocks-and-statements.js +0 -588
  34. package/src/printers/classes.js +0 -940
  35. package/src/printers/comments/comments-utils.js +0 -38
  36. package/src/printers/comments/format-comments.js +0 -223
  37. package/src/printers/comments/handle-comments.js +0 -50
  38. package/src/printers/expressions.js +0 -703
  39. package/src/printers/interfaces.js +0 -324
  40. package/src/printers/lexical-structure.js +0 -27
  41. package/src/printers/names.js +0 -42
  42. package/src/printers/packages-and-modules.js +0 -231
  43. package/src/printers/prettier-builder.js +0 -60
  44. package/src/printers/printer-utils.js +0 -715
  45. package/src/printers/types-values-and-variables.js +0 -202
  46. package/yarn-error.log +0 -8052
@@ -1,588 +0,0 @@
1
- "use strict";
2
-
3
- const { line, softline, hardline } = require("prettier").doc.builders;
4
- const { group, indent, dedent, concat, join } = require("./prettier-builder");
5
- const { printTokenWithComments } = require("./comments/format-comments");
6
- const {
7
- hasLeadingLineComments,
8
- hasTrailingLineComments
9
- } = require("./comments/comments-utils");
10
- const {
11
- displaySemicolon,
12
- rejectAndConcat,
13
- rejectAndJoin,
14
- rejectAndJoinSeps,
15
- getBlankLinesSeparator,
16
- rejectSeparators,
17
- putIntoBraces,
18
- isStatementEmptyStatement,
19
- sortModifiers
20
- } = require("./printer-utils");
21
-
22
- class BlocksAndStatementPrettierVisitor {
23
- block(ctx) {
24
- const blockStatements = this.visit(ctx.blockStatements);
25
-
26
- return putIntoBraces(
27
- blockStatements,
28
- hardline,
29
- ctx.LCurly[0],
30
- ctx.RCurly[0]
31
- );
32
- }
33
-
34
- blockStatements(ctx) {
35
- const blockStatement = this.mapVisit(ctx.blockStatement);
36
-
37
- const separators = rejectSeparators(
38
- getBlankLinesSeparator(ctx.blockStatement),
39
- blockStatement
40
- );
41
-
42
- return rejectAndJoinSeps(separators, blockStatement);
43
- }
44
-
45
- blockStatement(ctx) {
46
- return this.visitSingle(ctx);
47
- }
48
-
49
- localVariableDeclarationStatement(ctx) {
50
- const localVariableDeclaration = this.visit(ctx.localVariableDeclaration);
51
- return rejectAndConcat([localVariableDeclaration, ctx.Semicolon[0]]);
52
- }
53
-
54
- localVariableDeclaration(ctx) {
55
- const modifiers = sortModifiers(ctx.variableModifier);
56
- const firstAnnotations = this.mapVisit(modifiers[0]);
57
- const finalModifiers = this.mapVisit(modifiers[1]);
58
-
59
- const localVariableType = this.visit(ctx.localVariableType);
60
- const variableDeclaratorList = this.visit(ctx.variableDeclaratorList);
61
- return rejectAndJoin(hardline, [
62
- rejectAndJoin(hardline, firstAnnotations),
63
- rejectAndJoin(" ", [
64
- rejectAndJoin(" ", finalModifiers),
65
- localVariableType,
66
- variableDeclaratorList
67
- ])
68
- ]);
69
- }
70
-
71
- localVariableType(ctx) {
72
- if (ctx.unannType) {
73
- return this.visitSingle(ctx);
74
- }
75
-
76
- return printTokenWithComments(this.getSingle(ctx));
77
- }
78
-
79
- statement(ctx, params) {
80
- // handling Labeled statements comments
81
- if (ctx.labeledStatement !== undefined) {
82
- const newLabelStatement = { ...ctx.labeledStatement[0] };
83
- const newColon = { ...ctx.labeledStatement[0].children.Colon[0] };
84
- const newStatement = {
85
- ...ctx.labeledStatement[0].children.statement[0]
86
- };
87
-
88
- const labeledStatementLeadingComments = [];
89
-
90
- if (newColon.trailingComments !== undefined) {
91
- labeledStatementLeadingComments.push(...newColon.trailingComments);
92
- delete newColon.trailingComments;
93
- }
94
-
95
- if (newStatement.leadingComments !== undefined) {
96
- labeledStatementLeadingComments.push(...newStatement.leadingComments);
97
- delete newStatement.leadingComments;
98
- }
99
-
100
- if (labeledStatementLeadingComments.length !== 0) {
101
- newLabelStatement.leadingComments = labeledStatementLeadingComments;
102
- }
103
- newLabelStatement.children.Colon[0] = newColon;
104
- newLabelStatement.children.statement[0] = newStatement;
105
-
106
- return this.visit([newLabelStatement]);
107
- }
108
-
109
- return this.visitSingle(ctx, params);
110
- }
111
-
112
- statementWithoutTrailingSubstatement(ctx, params) {
113
- return this.visitSingle(ctx, params);
114
- }
115
-
116
- emptyStatement(ctx, params) {
117
- return displaySemicolon(ctx.Semicolon[0], params);
118
- }
119
-
120
- labeledStatement(ctx) {
121
- const identifier = ctx.Identifier[0];
122
- const statement = this.visit(ctx.statement);
123
-
124
- return rejectAndJoin(ctx.Colon[0], [identifier, statement]);
125
- }
126
-
127
- expressionStatement(ctx) {
128
- const statementExpression = this.visit(ctx.statementExpression);
129
- return rejectAndConcat([statementExpression, ctx.Semicolon[0]]);
130
- }
131
-
132
- statementExpression(ctx) {
133
- return this.visitSingle(ctx);
134
- }
135
-
136
- ifStatement(ctx) {
137
- const expression = this.visit(ctx.expression);
138
-
139
- const ifStatement = this.visit(ctx.statement[0], {
140
- allowEmptyStatement: true
141
- });
142
- const ifSeparator = isStatementEmptyStatement(ifStatement) ? "" : " ";
143
-
144
- let elsePart = "";
145
- if (ctx.Else !== undefined) {
146
- const elseStatement = this.visit(ctx.statement[1], {
147
- allowEmptyStatement: true
148
- });
149
- const elseSeparator = isStatementEmptyStatement(elseStatement) ? "" : " ";
150
-
151
- const elseOnSameLine =
152
- hasTrailingLineComments(ctx.statement[0]) ||
153
- hasLeadingLineComments(ctx.Else[0])
154
- ? hardline
155
- : " ";
156
-
157
- elsePart = rejectAndJoin(elseSeparator, [
158
- concat([elseOnSameLine, ctx.Else[0]]),
159
- elseStatement
160
- ]);
161
- }
162
-
163
- return rejectAndConcat([
164
- rejectAndJoin(" ", [
165
- ctx.If[0],
166
- concat([
167
- putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
168
- ifSeparator
169
- ])
170
- ]),
171
- ifStatement,
172
- elsePart
173
- ]);
174
- }
175
-
176
- assertStatement(ctx) {
177
- const expressions = this.mapVisit(ctx.expression);
178
- const colon = ctx.Colon ? ctx.Colon[0] : ":";
179
- return rejectAndConcat([
180
- concat([ctx.Assert[0], " "]),
181
- rejectAndJoin(concat([" ", colon, " "]), expressions),
182
- ctx.Semicolon[0]
183
- ]);
184
- }
185
-
186
- switchStatement(ctx) {
187
- const expression = this.visit(ctx.expression);
188
- const switchBlock = this.visit(ctx.switchBlock);
189
-
190
- return rejectAndJoin(" ", [
191
- ctx.Switch[0],
192
- putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
193
- switchBlock
194
- ]);
195
- }
196
-
197
- switchBlock(ctx) {
198
- const switchCases =
199
- ctx.switchBlockStatementGroup !== undefined
200
- ? this.mapVisit(ctx.switchBlockStatementGroup)
201
- : this.mapVisit(ctx.switchRule);
202
-
203
- return putIntoBraces(
204
- rejectAndJoin(hardline, switchCases),
205
- hardline,
206
- ctx.LCurly[0],
207
- ctx.RCurly[0]
208
- );
209
- }
210
-
211
- switchBlockStatementGroup(ctx) {
212
- const switchLabels = this.mapVisit(ctx.switchLabel);
213
-
214
- const labels = [];
215
- for (let i = 0; i < switchLabels.length; i++) {
216
- labels.push(concat([switchLabels[i], ctx.Colon[i]]));
217
- }
218
-
219
- const blockStatements = this.visit(ctx.blockStatements);
220
-
221
- return indent(
222
- rejectAndJoin(hardline, [
223
- dedent(rejectAndJoin(hardline, labels)),
224
- blockStatements
225
- ])
226
- );
227
- }
228
-
229
- switchLabel(ctx) {
230
- if (ctx.Case) {
231
- const caseConstants = this.mapVisit(ctx.caseConstant);
232
-
233
- const commas = ctx.Comma
234
- ? ctx.Comma.map(elt => {
235
- return concat([elt, line]);
236
- })
237
- : [];
238
-
239
- return group(
240
- indent(
241
- rejectAndConcat([
242
- concat([ctx.Case[0], " "]),
243
- rejectAndJoinSeps(commas, caseConstants)
244
- ])
245
- )
246
- );
247
- }
248
-
249
- return concat([ctx.Default[0]]);
250
- }
251
-
252
- switchRule(ctx) {
253
- const switchLabel = this.visit(ctx.switchLabel);
254
-
255
- let caseInstruction;
256
- if (ctx.throwStatement !== undefined) {
257
- caseInstruction = this.visit(ctx.throwStatement);
258
- } else if (ctx.block !== undefined) {
259
- caseInstruction = this.visit(ctx.block);
260
- } else {
261
- caseInstruction = concat([this.visit(ctx.expression), ctx.Semicolon[0]]);
262
- }
263
-
264
- return join(" ", [switchLabel, ctx.Arrow[0], caseInstruction]);
265
- }
266
-
267
- caseConstant(ctx) {
268
- return this.visitSingle(ctx);
269
- }
270
-
271
- whileStatement(ctx) {
272
- const expression = this.visit(ctx.expression);
273
- const statement = this.visit(ctx.statement[0], {
274
- allowEmptyStatement: true
275
- });
276
- const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
277
-
278
- return rejectAndJoin(" ", [
279
- ctx.While[0],
280
- rejectAndJoin(statementSeparator, [
281
- putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
282
- statement
283
- ])
284
- ]);
285
- }
286
-
287
- doStatement(ctx) {
288
- const statement = this.visit(ctx.statement[0], {
289
- allowEmptyStatement: true
290
- });
291
- const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
292
-
293
- const expression = this.visit(ctx.expression);
294
-
295
- return rejectAndJoin(" ", [
296
- rejectAndJoin(statementSeparator, [ctx.Do[0], statement]),
297
- ctx.While[0],
298
- rejectAndConcat([
299
- putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
300
- ctx.Semicolon[0]
301
- ])
302
- ]);
303
- }
304
-
305
- forStatement(ctx) {
306
- return this.visitSingle(ctx);
307
- }
308
-
309
- basicForStatement(ctx) {
310
- const forInit = this.visit(ctx.forInit);
311
- const expression = this.visit(ctx.expression);
312
- const forUpdate = this.visit(ctx.forUpdate);
313
- const statement = this.visit(ctx.statement[0], {
314
- allowEmptyStatement: true
315
- });
316
- const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
317
-
318
- return rejectAndConcat([
319
- rejectAndJoin(" ", [
320
- ctx.For[0],
321
- putIntoBraces(
322
- rejectAndConcat([
323
- forInit,
324
- rejectAndJoin(line, [ctx.Semicolon[0], expression]),
325
- rejectAndJoin(line, [ctx.Semicolon[1], forUpdate])
326
- ]),
327
- softline,
328
- ctx.LBrace[0],
329
- ctx.RBrace[0]
330
- )
331
- ]),
332
- statementSeparator,
333
- statement
334
- ]);
335
- }
336
-
337
- forInit(ctx) {
338
- return this.visitSingle(ctx);
339
- }
340
-
341
- forUpdate(ctx) {
342
- return this.visitSingle(ctx);
343
- }
344
-
345
- statementExpressionList(ctx) {
346
- const statementExpressions = this.mapVisit(ctx.statementExpression);
347
- const commas = ctx.Comma
348
- ? ctx.Comma.map(elt => {
349
- return concat([printTokenWithComments(elt), " "]);
350
- })
351
- : [];
352
- return rejectAndJoinSeps(commas, statementExpressions);
353
- }
354
-
355
- enhancedForStatement(ctx) {
356
- const variableModifiers = this.mapVisit(ctx.variableModifier);
357
- const localVariableType = this.visit(ctx.localVariableType);
358
- const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
359
- const expression = this.visit(ctx.expression);
360
- const statement = this.visit(ctx.statement[0], {
361
- allowEmptyStatement: true
362
- });
363
- const statementSeparator = isStatementEmptyStatement(statement) ? "" : " ";
364
-
365
- return rejectAndConcat([
366
- rejectAndJoin(" ", [ctx.For[0], ctx.LBrace[0]]),
367
- rejectAndJoin(" ", [
368
- rejectAndJoin(" ", variableModifiers),
369
- localVariableType,
370
- variableDeclaratorId
371
- ]),
372
- concat([" ", ctx.Colon[0], " "]),
373
- expression,
374
- concat([ctx.RBrace[0], statementSeparator]),
375
- statement
376
- ]);
377
- }
378
-
379
- breakStatement(ctx) {
380
- if (ctx.Identifier) {
381
- const identifier = ctx.Identifier[0];
382
- return rejectAndConcat([
383
- concat([ctx.Break[0], " "]),
384
- identifier,
385
- ctx.Semicolon[0]
386
- ]);
387
- }
388
-
389
- return concat([ctx.Break[0], ctx.Semicolon[0]]);
390
- }
391
-
392
- continueStatement(ctx) {
393
- if (ctx.Identifier) {
394
- const identifier = ctx.Identifier[0];
395
-
396
- return rejectAndConcat([
397
- concat([ctx.Continue[0], " "]),
398
- identifier,
399
- ctx.Semicolon[0]
400
- ]);
401
- }
402
-
403
- return rejectAndConcat([ctx.Continue[0], ctx.Semicolon[0]]);
404
- }
405
-
406
- returnStatement(ctx) {
407
- if (ctx.expression) {
408
- const expression = this.visit(ctx.expression, {
409
- addParenthesisToWrapStatement: true
410
- });
411
-
412
- return rejectAndConcat([
413
- concat([ctx.Return[0], " "]),
414
- expression,
415
- ctx.Semicolon[0]
416
- ]);
417
- }
418
-
419
- return rejectAndConcat([ctx.Return[0], ctx.Semicolon[0]]);
420
- }
421
-
422
- throwStatement(ctx) {
423
- const expression = this.visit(ctx.expression);
424
-
425
- return rejectAndConcat([
426
- concat([ctx.Throw[0], " "]),
427
- expression,
428
- ctx.Semicolon[0]
429
- ]);
430
- }
431
-
432
- synchronizedStatement(ctx) {
433
- const expression = this.visit(ctx.expression);
434
- const block = this.visit(ctx.block);
435
-
436
- return rejectAndConcat([
437
- join(" ", [
438
- ctx.Synchronized[0],
439
- concat([
440
- putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]),
441
- " "
442
- ])
443
- ]),
444
- block
445
- ]);
446
- }
447
-
448
- tryStatement(ctx) {
449
- if (ctx.tryWithResourcesStatement) {
450
- return this.visit(ctx.tryWithResourcesStatement);
451
- }
452
-
453
- const block = this.visit(ctx.block);
454
- const catches = this.visit(ctx.catches);
455
- const finallyBlock = this.visit(ctx.finally);
456
-
457
- return rejectAndJoin(" ", [ctx.Try[0], block, catches, finallyBlock]);
458
- }
459
-
460
- catches(ctx) {
461
- const catchClauses = this.mapVisit(ctx.catchClause);
462
- return rejectAndJoin(" ", catchClauses);
463
- }
464
-
465
- catchClause(ctx) {
466
- const catchFormalParameter = this.visit(ctx.catchFormalParameter);
467
- const block = this.visit(ctx.block);
468
-
469
- return rejectAndConcat([
470
- group(
471
- rejectAndConcat([
472
- rejectAndJoin(" ", [ctx.Catch[0], ctx.LBrace[0]]),
473
- indent(rejectAndConcat([softline, catchFormalParameter])),
474
- softline,
475
- concat([ctx.RBrace[0], " "])
476
- ])
477
- ),
478
- block
479
- ]);
480
- }
481
-
482
- catchFormalParameter(ctx) {
483
- const variableModifiers = this.mapVisit(ctx.variableModifier);
484
- const catchType = this.visit(ctx.catchType);
485
- const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
486
-
487
- return rejectAndJoin(" ", [
488
- rejectAndJoin(" ", variableModifiers),
489
- catchType,
490
- variableDeclaratorId
491
- ]);
492
- }
493
-
494
- catchType(ctx) {
495
- const unannClassType = this.visit(ctx.unannClassType);
496
- const classTypes = this.mapVisit(ctx.classType);
497
- const ors = ctx.Or ? ctx.Or.map(elt => concat([line, elt, " "])) : [];
498
-
499
- return group(rejectAndJoinSeps(ors, [unannClassType, ...classTypes]));
500
- }
501
-
502
- finally(ctx) {
503
- const block = this.visit(ctx.block);
504
-
505
- return rejectAndJoin(" ", [ctx.Finally[0], block]);
506
- }
507
-
508
- tryWithResourcesStatement(ctx) {
509
- const resourceSpecification = this.visit(ctx.resourceSpecification);
510
- const block = this.visit(ctx.block);
511
- const catches = this.visit(ctx.catches);
512
- const finallyBlock = this.visit(ctx.finally);
513
-
514
- return rejectAndJoin(" ", [
515
- ctx.Try[0],
516
- resourceSpecification,
517
- block,
518
- catches,
519
- finallyBlock
520
- ]);
521
- }
522
-
523
- resourceSpecification(ctx) {
524
- const resourceList = this.visit(ctx.resourceList);
525
- const optionalSemicolon = ctx.Semicolon ? ctx.Semicolon[0] : "";
526
-
527
- return putIntoBraces(
528
- rejectAndConcat([resourceList, optionalSemicolon]),
529
- softline,
530
- ctx.LBrace[0],
531
- ctx.RBrace[0]
532
- );
533
- }
534
-
535
- resourceList(ctx) {
536
- const resources = this.mapVisit(ctx.resource);
537
- const semicolons = ctx.Semicolon
538
- ? ctx.Semicolon.map(elt => {
539
- return concat([elt, line]);
540
- })
541
- : [""];
542
- return rejectAndJoinSeps(semicolons, resources);
543
- }
544
-
545
- resource(ctx) {
546
- return this.visitSingle(ctx);
547
- }
548
-
549
- resourceInit(ctx) {
550
- const variableModifiers = this.mapVisit(ctx.variableModifier);
551
- const localVariableType = this.visit(ctx.localVariableType);
552
- const identifier = ctx.Identifier[0];
553
- const expression = this.visit(ctx.expression);
554
-
555
- return rejectAndJoin(" ", [
556
- rejectAndJoin(" ", variableModifiers),
557
- localVariableType,
558
- identifier,
559
- ctx.Equals[0],
560
- expression
561
- ]);
562
- }
563
-
564
- yieldStatement(ctx) {
565
- const expression = this.visit(ctx.expression);
566
- return join(" ", [ctx.Yield[0], concat([expression, ctx.Semicolon[0]])]);
567
- }
568
-
569
- variableAccess(ctx) {
570
- return this.visitSingle(ctx);
571
- }
572
-
573
- isBasicForStatement() {
574
- return "isBasicForStatement";
575
- }
576
-
577
- isLocalVariableDeclaration() {
578
- return "isLocalVariableDeclaration";
579
- }
580
-
581
- isClassicSwitchLabel() {
582
- return "isClassicSwitchLabel";
583
- }
584
- }
585
-
586
- module.exports = {
587
- BlocksAndStatementPrettierVisitor
588
- };