prettier-plugin-java 2.2.0 → 2.3.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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # prettier-plugin-java
4
4
 
5
- ![Prettier Banner](https://raw.githubusercontent.com/prettier/prettier-logo/master/images/prettier-banner-light.png)
5
+ ![Prettier Java Banner](https://raw.githubusercontent.com/jhipster/prettier-java/main/logo/prettier-java-banner-light.svg)
6
6
 
7
7
  Prettier is an opinionated code formatter which forces a certain coding style. It makes the code consistent through an entire project.
8
8
 
@@ -156,4 +156,3 @@ public void myfunction() {
156
156
  .big();
157
157
  }
158
158
  ```
159
-
@@ -228,12 +228,14 @@ var ClassesPrettierVisitor = /** @class */ (function (_super) {
228
228
  }
229
229
  if (ctx.variableInitializer[0].children.expression[0].children
230
230
  .ternaryExpression !== undefined) {
231
- var firstPrimary = ctx.variableInitializer[0].children.expression[0].children
231
+ var unaryExpressions = ctx.variableInitializer[0].children.expression[0].children
232
232
  .ternaryExpression[0].children.binaryExpression[0].children
233
- .unaryExpression[0].children.primary[0];
233
+ .unaryExpression;
234
+ var firstPrimary = unaryExpressions[0].children.primary[0];
234
235
  // Cast Expression
235
236
  if (firstPrimary.children.primaryPrefix[0].children.castExpression !==
236
- undefined) {
237
+ undefined &&
238
+ unaryExpressions.length === 1) {
237
239
  var groupId = Symbol("assignment");
238
240
  return (0, prettier_builder_1.group)([
239
241
  (0, prettier_builder_1.group)(variableDeclaratorId),
@@ -3,36 +3,77 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.handleCommentsBinaryExpression = void 0;
4
4
  var comments_utils_1 = require("./comments-utils");
5
5
  function handleCommentsBinaryExpression(ctx) {
6
+ moveOperatorLeadingCommentsToNextExpression(ctx);
7
+ moveExpressionTrailingCommentsToNextOperator(ctx);
8
+ }
9
+ exports.handleCommentsBinaryExpression = handleCommentsBinaryExpression;
10
+ function moveOperatorLeadingCommentsToNextExpression(ctx) {
11
+ var _a;
6
12
  var unaryExpressionIndex = 1;
7
- if (ctx.BinaryOperator !== undefined) {
8
- ctx.BinaryOperator.forEach(function (binaryOperator) {
13
+ (_a = ctx.BinaryOperator) === null || _a === void 0 ? void 0 : _a.forEach(function (binaryOperator) {
14
+ var _a;
15
+ if ((0, comments_utils_1.hasLeadingComments)(binaryOperator)) {
16
+ while (ctx.unaryExpression[unaryExpressionIndex].location.startOffset <
17
+ binaryOperator.endOffset) {
18
+ unaryExpressionIndex++;
19
+ }
20
+ // Adapt the position of the operator and its leading comments
21
+ var shiftUp = binaryOperator.leadingComments[0].startLine -
22
+ 1 -
23
+ binaryOperator.startLine;
24
+ if (binaryOperator.startLine !==
25
+ ctx.unaryExpression[unaryExpressionIndex].location.startLine) {
26
+ binaryOperator.leadingComments.forEach(function (comment) {
27
+ comment.startLine += 1;
28
+ comment.endLine += 1;
29
+ });
30
+ }
31
+ binaryOperator.startLine += shiftUp;
32
+ binaryOperator.endLine += shiftUp;
33
+ // Move binaryOperator's leading comments to the following
34
+ // unaryExpression
35
+ ctx.unaryExpression[unaryExpressionIndex].leadingComments =
36
+ ctx.unaryExpression[unaryExpressionIndex].leadingComments || [];
37
+ (_a = ctx.unaryExpression[unaryExpressionIndex].leadingComments).unshift.apply(_a, binaryOperator.leadingComments);
38
+ delete binaryOperator.leadingComments;
39
+ }
40
+ });
41
+ }
42
+ function moveExpressionTrailingCommentsToNextOperator(ctx) {
43
+ var binaryOperators = ctx.BinaryOperator;
44
+ var binaryOperatorIndex = 1;
45
+ if (binaryOperators === null || binaryOperators === void 0 ? void 0 : binaryOperators.length) {
46
+ ctx.unaryExpression.forEach(function (unaryExpression) {
9
47
  var _a;
10
- if ((0, comments_utils_1.hasLeadingComments)(binaryOperator)) {
11
- while (ctx.unaryExpression[unaryExpressionIndex].location.startOffset <
12
- binaryOperator.endOffset) {
13
- unaryExpressionIndex++;
48
+ var _b;
49
+ if ((0, comments_utils_1.hasTrailingComments)(unaryExpression)) {
50
+ while (binaryOperatorIndex < binaryOperators.length &&
51
+ unaryExpression.location.endOffset &&
52
+ binaryOperators[binaryOperatorIndex].startOffset <
53
+ unaryExpression.location.endOffset) {
54
+ binaryOperatorIndex++;
14
55
  }
15
- // Adapt the position of the operator and its leading comments
16
- var shiftUp = binaryOperator.leadingComments[0].startLine -
56
+ var binaryOperator = binaryOperators[binaryOperatorIndex];
57
+ // Adapt the position of the expression and its trailing comments
58
+ var shiftUp = unaryExpression.trailingComments[0].startLine -
17
59
  1 -
18
- binaryOperator.startLine;
19
- if (binaryOperator.startLine !==
20
- ctx.unaryExpression[unaryExpressionIndex].location.startLine) {
21
- binaryOperator.leadingComments.forEach(function (comment) {
60
+ unaryExpression.location.startLine;
61
+ if (unaryExpression.location.startLine !== binaryOperator.startLine) {
62
+ unaryExpression.trailingComments.forEach(function (comment) {
22
63
  comment.startLine += 1;
23
64
  comment.endLine += 1;
24
65
  });
25
66
  }
26
- binaryOperator.startLine += shiftUp;
27
- binaryOperator.endLine += shiftUp;
28
- // Assign the leading comments & trailing comments of the binaryOperator
29
- // to the following unaryExpression as leading comments
30
- ctx.unaryExpression[unaryExpressionIndex].leadingComments =
31
- ctx.unaryExpression[unaryExpressionIndex].leadingComments || [];
32
- (_a = ctx.unaryExpression[unaryExpressionIndex].leadingComments).unshift.apply(_a, binaryOperator.leadingComments);
33
- delete binaryOperator.leadingComments;
67
+ unaryExpression.location.startLine += shiftUp;
68
+ if (unaryExpression.location.endLine !== undefined) {
69
+ unaryExpression.location.endLine += shiftUp;
70
+ }
71
+ // Move unaryExpression's trailing comments to the following
72
+ // binaryOperator
73
+ binaryOperator.trailingComments = (_b = binaryOperator.trailingComments) !== null && _b !== void 0 ? _b : [];
74
+ (_a = binaryOperator.trailingComments).unshift.apply(_a, unaryExpression.trailingComments);
75
+ delete unaryExpression.trailingComments;
34
76
  }
35
77
  });
36
78
  }
37
79
  }
38
- exports.handleCommentsBinaryExpression = handleCommentsBinaryExpression;
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
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
+ };
17
28
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
29
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
30
  if (ar || !(i in from)) {
@@ -157,68 +168,36 @@ var ExpressionsPrettierVisitor = /** @class */ (function (_super) {
157
168
  };
158
169
  ExpressionsPrettierVisitor.prototype.binaryExpression = function (ctx, params) {
159
170
  (0, handle_comments_1.handleCommentsBinaryExpression)(ctx);
160
- var instanceofReferences = this.mapVisit((0, printer_utils_1.sortNodes)([ctx.pattern, ctx.referenceType]));
161
- var expression = this.mapVisit(ctx.expression);
162
- var unaryExpression = this.mapVisit(ctx.unaryExpression);
163
- var _a = (0, printer_utils_1.separateTokensIntoGroups)(ctx), groupsOfOperator = _a.groupsOfOperator, sortedBinaryOperators = _a.sortedBinaryOperators;
164
- var segmentsSplitByBinaryOperator = [];
165
- var currentSegment = [];
166
- if (groupsOfOperator.length === 1 && groupsOfOperator[0].length === 0) {
167
- return unaryExpression.shift();
168
- }
169
- groupsOfOperator.forEach(function (subgroup) {
170
- currentSegment = [unaryExpression.shift()];
171
- for (var i = 0; i < subgroup.length; i++) {
172
- var token = subgroup[i];
173
- var shiftOperator = (0, printer_utils_1.isShiftOperator)(subgroup, i);
174
- if (token.tokenType.name === "Instanceof") {
175
- currentSegment.push((0, printer_utils_1.rejectAndJoin)(" ", [
176
- ctx.Instanceof[0],
177
- instanceofReferences.shift()
178
- ]));
179
- }
180
- else if ((0, printer_utils_1.matchCategory)(token, "'AssignmentOperator'")) {
181
- currentSegment.push((0, prettier_builder_1.indent)((0, printer_utils_1.rejectAndJoin)(line, [token, expression.shift()])));
182
- }
183
- else if (shiftOperator === "leftShift" ||
184
- shiftOperator === "rightShift") {
185
- currentSegment.push((0, printer_utils_1.rejectAndJoin)(" ", [
186
- (0, printer_utils_1.rejectAndConcat)([token, subgroup[i + 1]]),
187
- unaryExpression.shift()
188
- ]));
189
- i++;
190
- }
191
- else if (shiftOperator === "doubleRightShift") {
192
- currentSegment.push((0, printer_utils_1.rejectAndJoin)(" ", [
193
- (0, printer_utils_1.rejectAndConcat)([token, subgroup[i + 1], subgroup[i + 2]]),
194
- unaryExpression.shift()
195
- ]));
196
- i += 2;
197
- }
198
- else if ((0, printer_utils_1.matchCategory)(token, "'BinaryOperator'")) {
199
- currentSegment.push((0, printer_utils_1.rejectAndJoin)(line, [token, unaryExpression.shift()]));
200
- }
201
- }
202
- segmentsSplitByBinaryOperator.push((0, prettier_builder_1.group)((0, printer_utils_1.rejectAndJoin)(" ", currentSegment)));
203
- });
204
- if (params !== undefined && params.addParenthesisToWrapStatement) {
205
- return (0, prettier_builder_1.group)((0, prettier_builder_1.concat)([
206
- ifBreak("(", ""),
207
- (0, prettier_builder_1.indent)((0, prettier_builder_1.concat)([
208
- softline,
209
- (0, prettier_builder_1.group)((0, printer_utils_1.rejectAndJoinSeps)(sortedBinaryOperators.map(function (elt) { return (0, prettier_builder_1.concat)([" ", elt, line]); }), segmentsSplitByBinaryOperator))
210
- ])),
171
+ var sortedNodes = (0, printer_utils_1.sortNodes)([
172
+ ctx.pattern,
173
+ ctx.referenceType,
174
+ ctx.expression,
175
+ ctx.unaryExpression
176
+ ]);
177
+ var nodes = this.mapVisit(sortedNodes, sortedNodes.length === 1 ? params : undefined);
178
+ var tokens = (0, printer_utils_1.sortTokens)([
179
+ ctx.Instanceof,
180
+ ctx.AssignmentOperator,
181
+ ctx.Less,
182
+ ctx.Greater,
183
+ ctx.BinaryOperator
184
+ ]);
185
+ var hasTokens = tokens.length > 0;
186
+ var content = (0, printer_utils_1.binary)(nodes, tokens, true);
187
+ return hasTokens && (params === null || params === void 0 ? void 0 : params.addParenthesisToWrapStatement)
188
+ ? (0, prettier_builder_1.group)((0, prettier_builder_1.concat)([
189
+ ifBreak("("),
190
+ (0, prettier_builder_1.indent)((0, prettier_builder_1.concat)([softline, content])),
211
191
  softline,
212
192
  ifBreak(")")
213
- ]));
214
- }
215
- return (0, prettier_builder_1.group)((0, printer_utils_1.rejectAndJoinSeps)(sortedBinaryOperators.map(function (elt) { return (0, prettier_builder_1.concat)([" ", elt, line]); }), segmentsSplitByBinaryOperator));
193
+ ]))
194
+ : content;
216
195
  };
217
- ExpressionsPrettierVisitor.prototype.unaryExpression = function (ctx) {
196
+ ExpressionsPrettierVisitor.prototype.unaryExpression = function (ctx, params) {
218
197
  var unaryPrefixOperator = ctx.UnaryPrefixOperator
219
198
  ? ctx.UnaryPrefixOperator
220
199
  : [];
221
- var primary = this.visit(ctx.primary);
200
+ var primary = this.visit(ctx.primary, params);
222
201
  var unarySuffixOperator = ctx.UnarySuffixOperator
223
202
  ? ctx.UnarySuffixOperator
224
203
  : [];
@@ -242,11 +221,9 @@ var ExpressionsPrettierVisitor = /** @class */ (function (_super) {
242
221
  unarySuffixOperator
243
222
  ]);
244
223
  };
245
- ExpressionsPrettierVisitor.prototype.primary = function (ctx) {
224
+ ExpressionsPrettierVisitor.prototype.primary = function (ctx, params) {
246
225
  var countMethodInvocation = (0, printer_utils_1.isUniqueMethodInvocation)(ctx.primarySuffix);
247
- var primaryPrefix = this.visit(ctx.primaryPrefix, {
248
- shouldBreakBeforeFirstMethodInvocation: countMethodInvocation > 1
249
- });
226
+ var primaryPrefix = this.visit(ctx.primaryPrefix, __assign(__assign({}, params), { shouldBreakBeforeFirstMethodInvocation: countMethodInvocation > 1 }));
250
227
  var suffixes = [];
251
228
  if (ctx.primarySuffix !== undefined) {
252
229
  // edge case: https://github.com/jhipster/prettier-java/issues/381
@@ -386,9 +363,10 @@ var ExpressionsPrettierVisitor = /** @class */ (function (_super) {
386
363
  var typeArguments = this.visit(ctx.typeArguments);
387
364
  return (0, printer_utils_1.rejectAndConcat)([keyWord, typeArguments]);
388
365
  };
389
- ExpressionsPrettierVisitor.prototype.parenthesisExpression = function (ctx) {
366
+ ExpressionsPrettierVisitor.prototype.parenthesisExpression = function (ctx, params) {
390
367
  var expression = this.visit(ctx.expression);
391
- return (0, printer_utils_1.putIntoBraces)(expression, softline, ctx.LBrace[0], ctx.RBrace[0]);
368
+ var separator = (params === null || params === void 0 ? void 0 : params.addParenthesisToWrapStatement) ? softline : "";
369
+ return (0, printer_utils_1.putIntoBraces)(expression, separator, ctx.LBrace[0], ctx.RBrace[0]);
392
370
  };
393
371
  ExpressionsPrettierVisitor.prototype.castExpression = function (ctx) {
394
372
  return this.visitSingle(ctx);
@@ -14,17 +14,37 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
+ if (ar || !(i in from)) {
20
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21
+ ar[i] = from[i];
22
+ }
23
+ }
24
+ return to.concat(ar || Array.prototype.slice.call(from));
25
+ };
17
26
  Object.defineProperty(exports, "__esModule", { value: true });
18
27
  exports.LexicalStructurePrettierVisitor = void 0;
19
28
  var format_comments_1 = require("./comments/format-comments");
29
+ var prettier_builder_1 = require("./prettier-builder");
20
30
  var base_cst_printer_1 = require("../base-cst-printer");
31
+ var doc_1 = require("prettier/doc");
32
+ var hardline = doc_1.builders.hardline;
21
33
  var LexicalStructurePrettierVisitor = /** @class */ (function (_super) {
22
34
  __extends(LexicalStructurePrettierVisitor, _super);
23
35
  function LexicalStructurePrettierVisitor() {
24
36
  return _super !== null && _super.apply(this, arguments) || this;
25
37
  }
26
38
  LexicalStructurePrettierVisitor.prototype.literal = function (ctx) {
27
- if (ctx.CharLiteral || ctx.TextBlock || ctx.StringLiteral || ctx.Null) {
39
+ if (ctx.TextBlock) {
40
+ var lines = ctx.TextBlock[0].image.split("\n");
41
+ var open_1 = lines.shift();
42
+ var baseIndent_1 = Math.min.apply(Math, lines.map(function (line) { return line.search(/\S/); }).filter(function (indent) { return indent >= 0; }));
43
+ return (0, prettier_builder_1.join)(hardline, __spreadArray([
44
+ open_1
45
+ ], lines.map(function (line) { return line.slice(baseIndent_1); }), true));
46
+ }
47
+ if (ctx.CharLiteral || ctx.StringLiteral || ctx.Null) {
28
48
  return (0, format_comments_1.printTokenWithComments)(this.getSingle(ctx));
29
49
  }
30
50
  return this.visitSingle(ctx);
@@ -23,7 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.printArrayList = exports.isUniqueMethodInvocation = exports.sortImports = exports.isStatementEmptyStatement = exports.isShiftOperator = exports.separateTokensIntoGroups = exports.putIntoBraces = exports.getInterfaceBodyDeclarationsSeparator = exports.getClassBodyDeclarationsSeparator = exports.getBlankLinesSeparator = exports.isExplicitLambdaParameter = exports.displaySemicolon = exports.findDeepElementInPartsArray = exports.sortModifiers = exports.sortClassTypeChildren = exports.matchCategory = exports.sortNodes = exports.sortAnnotationIdentifier = exports.rejectAndConcat = exports.rejectAndJoin = exports.rejectSeparators = exports.reject = exports.rejectAndJoinSeps = exports.buildFqn = void 0;
26
+ exports.printArrayList = exports.isUniqueMethodInvocation = exports.sortImports = exports.isStatementEmptyStatement = exports.binary = exports.putIntoBraces = exports.getInterfaceBodyDeclarationsSeparator = exports.getClassBodyDeclarationsSeparator = exports.getBlankLinesSeparator = exports.isExplicitLambdaParameter = exports.displaySemicolon = exports.findDeepElementInPartsArray = exports.sortModifiers = exports.sortClassTypeChildren = exports.matchCategory = exports.sortNodes = exports.sortTokens = exports.sortAnnotationIdentifier = exports.rejectAndConcat = exports.rejectAndJoin = exports.rejectSeparators = exports.reject = exports.rejectAndJoinSeps = exports.buildFqn = void 0;
27
27
  var findIndex_1 = __importDefault(require("lodash/findIndex"));
28
28
  var findLastIndex_1 = __importDefault(require("lodash/findLastIndex"));
29
29
  var forEach_1 = __importDefault(require("lodash/forEach"));
@@ -131,6 +131,7 @@ function sortTokens(values) {
131
131
  return a.startOffset - b.startOffset;
132
132
  });
133
133
  }
134
+ exports.sortTokens = sortTokens;
134
135
  function sortNodes(values) {
135
136
  var nodes = [];
136
137
  (0, forEach_1.default)(values, function (argument) {
@@ -185,16 +186,17 @@ function sortModifiers(modifiers) {
185
186
  var hasOtherModifier = false;
186
187
  /**
187
188
  * iterate in reverse order because we special-case
188
- * method annotations which come after all other
189
+ * type annotations which come after all other
189
190
  * modifiers
190
191
  */
191
192
  (0, forEachRight_1.default)(modifiers, function (modifier) {
192
193
  var isAnnotation = modifier.children.annotation !== undefined;
193
- var isMethodAnnotation = isAnnotation &&
194
+ var isTypeAnnotation = isAnnotation &&
194
195
  (modifier.name === "methodModifier" ||
195
- modifier.name === "interfaceMethodModifier");
196
+ modifier.name === "interfaceMethodModifier" ||
197
+ modifier.name === "fieldModifier");
196
198
  if (isAnnotation) {
197
- if (isMethodAnnotation && !hasOtherModifier) {
199
+ if (isTypeAnnotation && !hasOtherModifier) {
198
200
  lastAnnotations.unshift(modifier);
199
201
  }
200
202
  else {
@@ -335,7 +337,8 @@ function needLineClassBodyDeclaration(declaration) {
335
337
  if (classMemberDeclaration.children.fieldDeclaration !== undefined) {
336
338
  var fieldDeclaration = classMemberDeclaration.children.fieldDeclaration[0];
337
339
  if (fieldDeclaration.children.fieldModifier !== undefined &&
338
- hasAnnotation(fieldDeclaration.children.fieldModifier)) {
340
+ hasAnnotation(fieldDeclaration.children.fieldModifier) &&
341
+ hasNonTrailingAnnotation(fieldDeclaration.children.fieldModifier)) {
339
342
  return true;
340
343
  }
341
344
  return false;
@@ -349,7 +352,8 @@ function needLineInterfaceMemberDeclaration(declaration) {
349
352
  if (declaration.children.constantDeclaration !== undefined) {
350
353
  var constantDeclaration = declaration.children.constantDeclaration[0];
351
354
  if (constantDeclaration.children.constantModifier !== undefined &&
352
- hasAnnotation(constantDeclaration.children.constantModifier)) {
355
+ hasAnnotation(constantDeclaration.children.constantModifier) &&
356
+ hasNonTrailingAnnotation(constantDeclaration.children.constantModifier)) {
353
357
  return true;
354
358
  }
355
359
  return false;
@@ -381,15 +385,15 @@ function hasAnnotation(modifiers) {
381
385
  return modifiers.some(function (modifier) { return modifier.children.annotation !== undefined; });
382
386
  }
383
387
  /**
384
- * Return true if there is a method modifier that does not come after all other modifiers
388
+ * Return true if there is a modifier that does not come after all other modifiers
385
389
  * It is useful to know if sortModifiers will add an annotation before other modifiers
386
390
  *
387
- * @param methodModifiers
391
+ * @param modifiers
388
392
  * @returns {boolean}
389
393
  */
390
- function hasNonTrailingAnnotation(methodModifiers) {
391
- var firstAnnotationIndex = (0, findIndex_1.default)(methodModifiers, function (modifier) { return modifier.children.annotation !== undefined; });
392
- var lastNonAnnotationIndex = (0, findLastIndex_1.default)(methodModifiers, function (modifier) { return modifier.children.annotation === undefined; });
394
+ function hasNonTrailingAnnotation(modifiers) {
395
+ var firstAnnotationIndex = (0, findIndex_1.default)(modifiers, function (modifier) { return modifier.children.annotation !== undefined; });
396
+ var lastNonAnnotationIndex = (0, findLastIndex_1.default)(modifiers, function (modifier) { return modifier.children.annotation === undefined; });
393
397
  return (firstAnnotationIndex < lastNonAnnotationIndex ||
394
398
  lastNonAnnotationIndex === -1);
395
399
  }
@@ -443,62 +447,118 @@ function putIntoBraces(argument, separator, LBrace, RBrace) {
443
447
  ]));
444
448
  }
445
449
  exports.putIntoBraces = putIntoBraces;
446
- var andOrBinaryOperators = ["&&", "||", "&", "|", "^"];
447
- function separateTokensIntoGroups(ctx) {
448
- /**
449
- * separate tokens into groups by andOrBinaryOperators ("&&", "||", "&", "|", "^")
450
- * in order to break those operators in priority.
451
- */
452
- var tokens = sortTokens([
453
- ctx.Instanceof,
454
- ctx.AssignmentOperator,
455
- ctx.Less,
456
- ctx.Greater,
457
- ctx.BinaryOperator
458
- ]);
459
- var groupsOfOperator = [];
460
- var sortedBinaryOperators = [];
461
- var tmpGroup = [];
462
- tokens.forEach(function (token) {
463
- if (matchCategory(token, "'BinaryOperator'") &&
464
- (0, includes_1.default)(andOrBinaryOperators, token.image)) {
465
- sortedBinaryOperators.push(token);
466
- groupsOfOperator.push(tmpGroup);
467
- tmpGroup = [];
450
+ function binary(nodes, tokens, isRoot) {
451
+ if (isRoot === void 0) { isRoot = false; }
452
+ var levelOperator;
453
+ var levelPrecedence;
454
+ var level = [];
455
+ while (tokens.length) {
456
+ var nextOperator = getOperator(tokens);
457
+ var nextPrecedence = getOperatorPrecedence(nextOperator);
458
+ if (levelPrecedence === undefined || nextPrecedence === levelPrecedence) {
459
+ var tokenLength = ["<<", ">>", ">>>"].includes(nextOperator)
460
+ ? nextOperator.length
461
+ : 1;
462
+ var operator = (0, prettier_builder_1.concat)(tokens.splice(0, tokenLength));
463
+ if (levelOperator !== undefined &&
464
+ needsParentheses(levelOperator, nextOperator)) {
465
+ level.push(nodes.shift());
466
+ level = [
467
+ (0, prettier_builder_1.concat)(["(", (0, prettier_builder_1.group)(indent((0, prettier_builder_1.join)(line, level))), ") ", operator])
468
+ ];
469
+ }
470
+ else {
471
+ level.push((0, prettier_builder_1.join)(" ", [nodes.shift(), operator]));
472
+ }
473
+ levelOperator = nextOperator;
474
+ levelPrecedence = nextPrecedence;
475
+ }
476
+ else if (nextPrecedence < levelPrecedence) {
477
+ level.push(nodes.shift());
478
+ if (isRoot) {
479
+ var content_1 = (0, prettier_builder_1.group)(indent((0, prettier_builder_1.join)(line, level)));
480
+ nodes.unshift(levelOperator !== undefined &&
481
+ needsParentheses(levelOperator, nextOperator)
482
+ ? (0, prettier_builder_1.concat)(["(", content_1, ")"])
483
+ : content_1);
484
+ level = [];
485
+ levelOperator = undefined;
486
+ levelPrecedence = undefined;
487
+ }
488
+ else {
489
+ return (0, prettier_builder_1.group)((0, prettier_builder_1.join)(line, level));
490
+ }
468
491
  }
469
492
  else {
470
- tmpGroup.push(token);
493
+ var content_2 = indent(binary(nodes, tokens));
494
+ nodes.unshift(levelOperator !== undefined &&
495
+ needsParentheses(nextOperator, levelOperator)
496
+ ? (0, prettier_builder_1.concat)(["(", content_2, ")"])
497
+ : content_2);
471
498
  }
472
- });
473
- groupsOfOperator.push(tmpGroup);
474
- return {
475
- groupsOfOperator: groupsOfOperator,
476
- sortedBinaryOperators: sortedBinaryOperators
477
- };
499
+ }
500
+ level.push(nodes.shift());
501
+ var content = (0, prettier_builder_1.group)((0, prettier_builder_1.join)(line, level));
502
+ return levelOperator === "=" ? indent(content) : content;
478
503
  }
479
- exports.separateTokensIntoGroups = separateTokensIntoGroups;
480
- function isShiftOperator(tokens, index) {
481
- if (tokens.length <= index + 1) {
482
- return "none";
483
- }
484
- if (tokens[index].image === "<" &&
485
- tokens[index + 1].image === "<" &&
486
- tokens[index].startOffset === tokens[index + 1].startOffset - 1) {
487
- return "leftShift";
488
- }
489
- if (tokens[index].image === ">" &&
490
- tokens[index + 1].image === ">" &&
491
- tokens[index].startOffset === tokens[index + 1].startOffset - 1) {
492
- if (tokens.length > index + 2 &&
493
- tokens[index + 2].image === ">" &&
494
- tokens[index + 1].startOffset === tokens[index + 2].startOffset - 1) {
495
- return "doubleRightShift";
504
+ exports.binary = binary;
505
+ function getOperator(tokens) {
506
+ if (!tokens.length) {
507
+ return "";
508
+ }
509
+ var _a = tokens[0], image = _a.image, startOffset = _a.startOffset;
510
+ if (!["<", ">"].includes(image)) {
511
+ return image;
512
+ }
513
+ var repeatedTokenCount = 1;
514
+ for (var i = 1; i < Math.min(3, tokens.length); i++) {
515
+ var token = tokens[i];
516
+ if (token.image !== image || token.startOffset !== startOffset + i) {
517
+ break;
496
518
  }
497
- return "rightShift";
519
+ repeatedTokenCount++;
520
+ }
521
+ if (repeatedTokenCount === 1) {
522
+ return image;
523
+ }
524
+ if (image === "<") {
525
+ return "<<";
498
526
  }
499
- return "none";
527
+ else if (repeatedTokenCount == 2) {
528
+ return ">>";
529
+ }
530
+ else {
531
+ return ">>>";
532
+ }
533
+ }
534
+ var PRECEDENCES_BY_OPERATOR = new Map([
535
+ ["||"],
536
+ ["&&"],
537
+ ["|"],
538
+ ["^"],
539
+ ["&"],
540
+ ["==", "!="],
541
+ ["<", ">", "<=", ">=", "instanceof"],
542
+ ["<<", ">>", ">>>"],
543
+ ["+", "-"],
544
+ ["*", "/", "%"]
545
+ ].flatMap(function (operators, index) { return operators.map(function (operator) { return [operator, index]; }); }));
546
+ function getOperatorPrecedence(operator) {
547
+ var _a;
548
+ return (_a = PRECEDENCES_BY_OPERATOR.get(operator)) !== null && _a !== void 0 ? _a : -1;
549
+ }
550
+ function needsParentheses(operator, parentOperator) {
551
+ return ((operator === "&&" && parentOperator === "||") ||
552
+ (["|", "^", "&", "<<", ">>", ">>>"].includes(parentOperator) &&
553
+ getOperatorPrecedence(operator) >
554
+ getOperatorPrecedence(parentOperator)) ||
555
+ [operator, parentOperator].every(function (o) { return ["==", "!="].includes(o); }) ||
556
+ [operator, parentOperator].every(function (o) { return ["<<", ">>", ">>>"].includes(o); }) ||
557
+ (operator === "*" && parentOperator === "/") ||
558
+ (operator === "/" && parentOperator === "*") ||
559
+ (operator === "%" && ["+", "-", "*", "/"].includes(parentOperator)) ||
560
+ (["*", "/"].includes(operator) && parentOperator === "%"));
500
561
  }
501
- exports.isShiftOperator = isShiftOperator;
502
562
  function isStatementEmptyStatement(statement) {
503
563
  return (statement === ";" || (Array.isArray(statement) && statement[0] === ";"));
504
564
  }
@@ -29,11 +29,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.TypesValuesAndVariablesPrettierVisitor = void 0;
31
31
  var forEach_1 = __importDefault(require("lodash/forEach"));
32
+ var doc_1 = require("prettier/doc");
32
33
  var prettier_builder_1 = require("./prettier-builder");
33
34
  var format_comments_1 = require("./comments/format-comments");
34
35
  var printer_utils_1 = require("./printer-utils");
35
36
  var base_cst_printer_1 = require("../base-cst-printer");
36
37
  var utils_1 = require("../types/utils");
38
+ var line = doc_1.builders.line, softline = doc_1.builders.softline;
37
39
  var TypesValuesAndVariablesPrettierVisitor = /** @class */ (function (_super) {
38
40
  __extends(TypesValuesAndVariablesPrettierVisitor, _super);
39
41
  function TypesValuesAndVariablesPrettierVisitor() {
@@ -146,10 +148,12 @@ var TypesValuesAndVariablesPrettierVisitor = /** @class */ (function (_super) {
146
148
  TypesValuesAndVariablesPrettierVisitor.prototype.typeBound = function (ctx) {
147
149
  var classOrInterfaceType = this.visit(ctx.classOrInterfaceType);
148
150
  var additionalBound = this.mapVisit(ctx.additionalBound);
149
- return (0, printer_utils_1.rejectAndJoin)(" ", [
150
- ctx.Extends[0],
151
- classOrInterfaceType,
152
- (0, prettier_builder_1.join)(" ", additionalBound)
151
+ return (0, prettier_builder_1.concat)([
152
+ (0, printer_utils_1.rejectAndJoin)(" ", [ctx.Extends[0], classOrInterfaceType]),
153
+ (0, prettier_builder_1.indent)((0, prettier_builder_1.group)((0, prettier_builder_1.concat)([
154
+ additionalBound.length ? line : "",
155
+ (0, printer_utils_1.rejectAndJoin)(line, additionalBound)
156
+ ])))
153
157
  ]);
154
158
  };
155
159
  TypesValuesAndVariablesPrettierVisitor.prototype.additionalBound = function (ctx) {
@@ -158,11 +162,11 @@ var TypesValuesAndVariablesPrettierVisitor = /** @class */ (function (_super) {
158
162
  };
159
163
  TypesValuesAndVariablesPrettierVisitor.prototype.typeArguments = function (ctx) {
160
164
  var typeArgumentList = this.visit(ctx.typeArgumentList);
161
- return (0, printer_utils_1.rejectAndConcat)([ctx.Less[0], typeArgumentList, ctx.Greater[0]]);
165
+ return (0, printer_utils_1.putIntoBraces)(typeArgumentList, softline, ctx.Less[0], ctx.Greater[0]);
162
166
  };
163
167
  TypesValuesAndVariablesPrettierVisitor.prototype.typeArgumentList = function (ctx) {
164
168
  var typeArguments = this.mapVisit(ctx.typeArgument);
165
- var commas = ctx.Comma ? ctx.Comma.map(function (elt) { return (0, prettier_builder_1.concat)([elt, " "]); }) : [];
169
+ var commas = ctx.Comma ? ctx.Comma.map(function (elt) { return (0, prettier_builder_1.concat)([elt, line]); }) : [];
166
170
  return (0, printer_utils_1.rejectAndJoinSeps)(commas, typeArguments);
167
171
  };
168
172
  TypesValuesAndVariablesPrettierVisitor.prototype.typeArgument = function (ctx) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Prettier Java Plugin",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -9,9 +9,9 @@
9
9
  "repository": "https://github.com/jhipster/prettier-java",
10
10
  "license": "Apache-2.0",
11
11
  "dependencies": {
12
- "java-parser": "2.0.4",
12
+ "java-parser": "2.0.5",
13
13
  "lodash": "4.17.21",
14
- "prettier": "3.0.0"
14
+ "prettier": "3.0.3"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "yarn run test:unit && yarn run test:e2e-core",
@@ -41,5 +41,5 @@
41
41
  "ts-node": "10.9.1",
42
42
  "typescript": "4.9.3"
43
43
  },
44
- "gitHead": "f93d55a4b4764c8ff289602f91716e686f112b50"
44
+ "gitHead": "a7f9a5f795a87ccfd7910118b0856b449dedb957"
45
45
  }