prettier-plugin-java 2.6.8 → 2.7.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 (45) hide show
  1. package/dist/comments.d.ts +17 -0
  2. package/dist/comments.js +199 -0
  3. package/dist/index.d.ts +543 -0
  4. package/dist/index.js +26 -63
  5. package/dist/options.d.ts +23 -0
  6. package/dist/options.js +247 -239
  7. package/dist/parser.d.ts +9 -0
  8. package/dist/parser.js +24 -4
  9. package/dist/printer.d.ts +18 -0
  10. package/dist/printer.js +39 -5
  11. package/dist/printers/arrays.d.ts +9 -0
  12. package/dist/printers/arrays.js +8 -24
  13. package/dist/printers/blocks-and-statements.d.ts +117 -0
  14. package/dist/printers/blocks-and-statements.js +316 -412
  15. package/dist/printers/classes.d.ts +157 -0
  16. package/dist/printers/classes.js +422 -685
  17. package/dist/printers/expressions.d.ts +134 -0
  18. package/dist/printers/expressions.js +549 -555
  19. package/dist/printers/helpers.d.ts +71 -0
  20. package/dist/printers/helpers.js +233 -0
  21. package/dist/printers/index.d.ts +2 -0
  22. package/dist/printers/index.js +13 -0
  23. package/dist/printers/interfaces.d.ts +62 -0
  24. package/dist/printers/interfaces.js +146 -211
  25. package/dist/printers/lexical-structure.d.ts +14 -0
  26. package/dist/printers/lexical-structure.js +26 -28
  27. package/dist/printers/names.d.ts +12 -0
  28. package/dist/printers/names.js +11 -29
  29. package/dist/printers/packages-and-modules.d.ts +46 -0
  30. package/dist/printers/packages-and-modules.js +157 -159
  31. package/dist/printers/types-values-and-variables.d.ts +46 -0
  32. package/dist/printers/types-values-and-variables.js +86 -149
  33. package/package.json +5 -8
  34. package/dist/base-cst-printer.js +0 -55
  35. package/dist/cst-printer.js +0 -29
  36. package/dist/printers/comments/comments-utils.js +0 -21
  37. package/dist/printers/comments/format-comments.js +0 -171
  38. package/dist/printers/comments/handle-comments.js +0 -102
  39. package/dist/printers/prettier-builder.js +0 -45
  40. package/dist/printers/printer-utils.js +0 -598
  41. package/dist/types/utils.js +0 -20
  42. package/dist/utils/index.js +0 -2
  43. package/dist/utils/isEmptyDoc.js +0 -4
  44. package/dist/utils/printArgumentListWithBraces.js +0 -37
  45. package/index.d.ts +0 -4
@@ -1,432 +1,336 @@
1
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
- ])
2
+ import { call, definedKeys, indentInParentheses, isBinaryExpression, isEmptyStatement, lineEndWithComments, lineStartWithComments, map, onlyDefinedKey, printBlock, printDanglingComments, printSingle, printWithModifiers } from "./helpers.js";
3
+ const { group, hardline, ifBreak, indent, join, line, softline } = builders;
4
+ export default {
5
+ block(path, print) {
6
+ const statements = path.node.children.blockStatements
7
+ ? call(path, print, "blockStatements")
8
+ : [];
9
+ return printBlock(path, statements.length ? [statements] : []);
10
+ },
11
+ blockStatements(path, print) {
12
+ return join(hardline, map(path, statementPath => {
13
+ const { node, previous } = statementPath;
14
+ const statement = print(statementPath);
15
+ return previous &&
16
+ lineStartWithComments(node) > lineEndWithComments(previous) + 1
17
+ ? [hardline, statement]
18
+ : statement;
19
+ }, "blockStatement").filter(doc => doc !== ""));
20
+ },
21
+ blockStatement: printSingle,
22
+ localVariableDeclarationStatement(path, print) {
23
+ return [call(path, print, "localVariableDeclaration"), ";"];
24
+ },
25
+ localVariableDeclaration(path, print) {
26
+ const declaration = join(" ", [
27
+ call(path, print, "localVariableType"),
28
+ call(path, print, "variableDeclaratorList")
38
29
  ]);
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;
30
+ return printWithModifiers(path, print, "variableModifier", declaration);
31
+ },
32
+ localVariableType: printSingle,
33
+ statement: printSingle,
34
+ statementWithoutTrailingSubstatement: printSingle,
35
+ emptyStatement() {
36
+ return "";
37
+ },
38
+ labeledStatement(path, print) {
39
+ return [
40
+ call(path, print, "Identifier"),
41
+ ": ",
42
+ call(path, print, "statement")
43
+ ];
44
+ },
45
+ expressionStatement(path, print) {
46
+ return [call(path, print, "statementExpression"), ";"];
47
+ },
48
+ statementExpression: printSingle,
49
+ ifStatement(path, print) {
50
+ var _a;
51
+ const { children } = path.node;
52
+ const hasEmptyStatement = isEmptyStatement(children.statement[0]);
53
+ const statement = [
54
+ "if ",
55
+ indentInParentheses(call(path, print, "expression")),
56
+ hasEmptyStatement ? ";" : [" ", call(path, print, "statement", 0)]
57
+ ];
58
+ if (children.Else) {
59
+ const danglingComments = printDanglingComments(path);
60
+ if (danglingComments.length) {
61
+ statement.push(hardline, ...danglingComments, hardline);
60
62
  }
61
- if (labeledStatementLeadingComments.length !== 0) {
62
- newLabelStatement.leadingComments = labeledStatementLeadingComments;
63
+ else {
64
+ const elseHasBlock = ((_a = children.statement[0].children
65
+ .statementWithoutTrailingSubstatement) === null || _a === void 0 ? void 0 : _a[0].children.block) !==
66
+ undefined;
67
+ statement.push(elseHasBlock ? " " : hardline);
63
68
  }
64
- newLabelStatement.children.Colon[0] = newColon;
65
- newLabelStatement.children.statement[0] = newStatement;
66
- return this.visit([newLabelStatement]);
69
+ const elseHasEmptyStatement = isEmptyStatement(children.statement[1]);
70
+ statement.push("else", elseHasEmptyStatement ? ";" : [" ", call(path, print, "statement", 1)]);
67
71
  }
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 concat([identifier, ctx.Colon[0], " ", 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
72
+ return statement;
73
+ },
74
+ assertStatement(path, print) {
75
+ return ["assert ", ...join([" : "], map(path, print, "expression")), ";"];
76
+ },
77
+ switchStatement(path, print) {
78
+ return join(" ", [
79
+ "switch",
80
+ indentInParentheses(call(path, print, "expression")),
81
+ call(path, print, "switchBlock")
121
82
  ]);
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]
83
+ },
84
+ switchBlock(path, print) {
85
+ const { children } = path.node;
86
+ const caseKeys = definedKeys(children, [
87
+ "switchBlockStatementGroup",
88
+ "switchRule"
130
89
  ]);
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([
90
+ const cases = caseKeys.length === 1 ? map(path, print, caseKeys[0]) : [];
91
+ return printBlock(path, cases);
92
+ },
93
+ switchBlockStatementGroup(path, print) {
94
+ var _a, _b;
95
+ const { children } = path.node;
96
+ const switchLabel = call(path, print, "switchLabel");
97
+ if (!children.blockStatements) {
98
+ return [switchLabel, ":"];
99
+ }
100
+ const blockStatements = call(path, print, "blockStatements");
101
+ const statements = children.blockStatements[0].children.blockStatement;
102
+ const onlyStatementIsBlock = statements.length === 1 &&
103
+ ((_b = (_a = statements[0].children.statement) === null || _a === void 0 ? void 0 : _a[0].children.statementWithoutTrailingSubstatement) === null || _b === void 0 ? void 0 : _b[0].children.block) !== undefined;
104
+ return [
155
105
  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)])));
106
+ ":",
107
+ onlyStatementIsBlock
108
+ ? [" ", blockStatements]
109
+ : indent([hardline, blockStatements])
110
+ ];
111
+ },
112
+ switchLabel(path, print) {
113
+ var _a, _b;
114
+ const { children } = path.node;
115
+ if (!((_b = (_a = children.caseConstant) !== null && _a !== void 0 ? _a : children.casePattern) !== null && _b !== void 0 ? _b : children.Null)) {
116
+ return "default";
117
+ }
118
+ const values = [];
119
+ if (children.Null) {
120
+ values.push("null");
121
+ if (children.Default) {
122
+ values.push("default");
123
+ }
171
124
  }
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)
125
+ else {
126
+ const valuesKey = onlyDefinedKey(children, [
127
+ "caseConstant",
128
+ "casePattern"
180
129
  ]);
181
- return group(rejectAndJoin(separator, [
182
- multiplePatterns ? indent(contents) : contents,
183
- guard
184
- ]));
130
+ values.push(...map(path, print, valuesKey));
185
131
  }
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);
132
+ const hasMultipleValues = values.length > 1;
133
+ const label = hasMultipleValues
134
+ ? ["case", indent([line, ...join([",", line], values)])]
135
+ : ["case ", values[0]];
136
+ return children.guard
137
+ ? [
138
+ group([...label, hasMultipleValues ? line : " "]),
139
+ call(path, print, "guard")
140
+ ]
141
+ : group(label);
142
+ },
143
+ switchRule(path, print) {
144
+ const { children } = path.node;
145
+ const bodyKey = onlyDefinedKey(children, [
146
+ "block",
147
+ "expression",
148
+ "throwStatement"
149
+ ]);
150
+ const parts = [
151
+ call(path, print, "switchLabel"),
152
+ " -> ",
153
+ call(path, print, bodyKey)
154
+ ];
155
+ if (children.Semicolon) {
156
+ parts.push(";");
157
+ }
158
+ return parts;
159
+ },
160
+ caseConstant: printSingle,
161
+ casePattern: printSingle,
162
+ whileStatement(path, print) {
163
+ const statement = call(path, print, "statement");
164
+ const hasEmptyStatement = isEmptyStatement(path.node.children.statement[0]);
165
+ return [
166
+ "while ",
167
+ indentInParentheses(call(path, print, "expression")),
168
+ ...[hasEmptyStatement ? ";" : " ", statement]
169
+ ];
170
+ },
171
+ doStatement(path, print) {
172
+ const hasEmptyStatement = isEmptyStatement(path.node.children.statement[0]);
173
+ return [
174
+ "do",
175
+ hasEmptyStatement ? ";" : [" ", call(path, print, "statement")],
176
+ " while ",
177
+ indentInParentheses(call(path, print, "expression")),
178
+ ";"
179
+ ];
180
+ },
181
+ forStatement: printSingle,
182
+ basicForStatement(path, print) {
183
+ const { children } = path.node;
184
+ const danglingComments = printDanglingComments(path);
185
+ if (danglingComments.length) {
186
+ danglingComments.push(hardline);
193
187
  }
194
- else if (ctx.block !== undefined) {
195
- caseInstruction = this.visit(ctx.block);
188
+ const expressions = ["forInit", "expression", "forUpdate"].map(expressionKey => expressionKey in children ? call(path, print, expressionKey) : "");
189
+ const hasEmptyStatement = isEmptyStatement(children.statement[0]);
190
+ return [
191
+ ...danglingComments,
192
+ "for ",
193
+ expressions.some(expression => expression !== "")
194
+ ? indentInParentheses(join([";", line], expressions))
195
+ : "(;;)",
196
+ hasEmptyStatement ? ";" : [" ", call(path, print, "statement")]
197
+ ];
198
+ },
199
+ forInit: printSingle,
200
+ forUpdate: printSingle,
201
+ statementExpressionList(path, print) {
202
+ return group(map(path, print, "statementExpression").map((expression, index) => index === 0 ? expression : [",", indent([line, expression])]));
203
+ },
204
+ enhancedForStatement(path, print) {
205
+ var _a;
206
+ const statementNode = path.node.children.statement[0];
207
+ const forStatement = [
208
+ printDanglingComments(path),
209
+ "for ",
210
+ "(",
211
+ call(path, print, "localVariableDeclaration"),
212
+ " : ",
213
+ call(path, print, "expression"),
214
+ ")"
215
+ ];
216
+ if (isEmptyStatement(statementNode)) {
217
+ forStatement.push(";");
196
218
  }
197
219
  else {
198
- caseInstruction = concat([this.visit(ctx.expression), ctx.Semicolon[0]]);
220
+ const hasStatementBlock = ((_a = statementNode.children.statementWithoutTrailingSubstatement) === null || _a === void 0 ? void 0 : _a[0].children.block) !== undefined;
221
+ const statement = call(path, print, "statement");
222
+ forStatement.push(hasStatementBlock ? [" ", statement] : indent([line, statement]));
199
223
  }
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
- ]);
224
+ return group(forStatement);
225
+ },
226
+ breakStatement(path, print) {
227
+ return path.node.children.Identifier
228
+ ? ["break ", call(path, print, "Identifier"), ";"]
229
+ : "break;";
230
+ },
231
+ continueStatement(path, print) {
232
+ return path.node.children.Identifier
233
+ ? ["continue ", call(path, print, "Identifier"), ";"]
234
+ : "continue;";
235
+ },
236
+ returnStatement(path, print) {
237
+ const { children } = path.node;
238
+ const statement = ["return"];
239
+ if (children.expression) {
240
+ statement.push(" ");
241
+ const expression = call(path, print, "expression");
242
+ if (isBinaryExpression(children.expression[0])) {
243
+ statement.push(group([
244
+ ifBreak("("),
245
+ indent([softline, expression]),
246
+ softline,
247
+ ifBreak(")")
248
+ ]));
249
+ }
250
+ else {
251
+ statement.push(expression);
252
+ }
300
253
  }
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
- ]);
254
+ statement.push(";");
255
+ return statement;
256
+ },
257
+ throwStatement(path, print) {
258
+ return ["throw ", call(path, print, "expression"), ";"];
259
+ },
260
+ synchronizedStatement(path, print) {
261
+ return [
262
+ "synchronized ",
263
+ indentInParentheses(call(path, print, "expression")),
264
+ " ",
265
+ call(path, print, "block")
266
+ ];
267
+ },
268
+ tryStatement(path, print) {
269
+ const { children } = path.node;
270
+ if (children.tryWithResourcesStatement) {
271
+ return call(path, print, "tryWithResourcesStatement");
311
272
  }
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
- ]);
273
+ const blocks = ["try", call(path, print, "block")];
274
+ if (children.catches) {
275
+ blocks.push(call(path, print, "catches"));
324
276
  }
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);
277
+ if (children.finally) {
278
+ blocks.push(call(path, print, "finally"));
352
279
  }
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
280
+ return join(" ", blocks);
281
+ },
282
+ catches(path, print) {
283
+ return join(" ", map(path, print, "catchClause"));
284
+ },
285
+ catchClause(path, print) {
286
+ return [
287
+ "catch ",
288
+ indentInParentheses(call(path, print, "catchFormalParameter")),
289
+ " ",
290
+ call(path, print, "block")
291
+ ];
292
+ },
293
+ catchFormalParameter(path, print) {
294
+ return join(" ", [
295
+ ...map(path, print, "variableModifier"),
296
+ call(path, print, "catchType"),
297
+ call(path, print, "variableDeclaratorId")
383
298
  ]);
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
- }
299
+ },
300
+ catchType(path, print) {
301
+ return join([line, "| "], [call(path, print, "unannClassType"), ...map(path, print, "classType")]);
302
+ },
303
+ finally(path, print) {
304
+ return ["finally ", call(path, print, "block")];
305
+ },
306
+ tryWithResourcesStatement(path, print) {
307
+ const { children } = path.node;
308
+ const blocks = [
309
+ "try",
310
+ call(path, print, "resourceSpecification"),
311
+ call(path, print, "block")
312
+ ];
313
+ if (children.catches) {
314
+ blocks.push(call(path, print, "catches"));
315
+ }
316
+ if (children.finally) {
317
+ blocks.push(call(path, print, "finally"));
318
+ }
319
+ return join(" ", blocks);
320
+ },
321
+ resourceSpecification(path, print, options) {
322
+ const resources = [call(path, print, "resourceList")];
323
+ if (options.trailingComma !== "none") {
324
+ resources.push(ifBreak(";"));
325
+ }
326
+ return indentInParentheses(resources);
327
+ },
328
+ resourceList(path, print) {
329
+ return join([";", line], map(path, print, "resource"));
330
+ },
331
+ resource: printSingle,
332
+ yieldStatement(path, print) {
333
+ return ["yield ", call(path, print, "expression"), ";"];
334
+ },
335
+ variableAccess: printSingle
336
+ };