prettier-plugin-java 2.5.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.
- package/LICENSE +1 -1
- package/package.json +15 -13
- package/dist/base-cst-printer.js +0 -77
- package/dist/cst-printer.js +0 -37
- package/dist/index.js +0 -67
- package/dist/options.js +0 -258
- package/dist/parser.js +0 -6
- package/dist/printer.js +0 -8
- package/dist/printers/arrays.js +0 -49
- package/dist/printers/blocks-and-statements.js +0 -480
- package/dist/printers/classes.js +0 -745
- package/dist/printers/comments/comments-utils.js +0 -29
- package/dist/printers/comments/format-comments.js +0 -187
- package/dist/printers/comments/handle-comments.js +0 -79
- package/dist/printers/expressions.js +0 -557
- package/dist/printers/interfaces.js +0 -251
- package/dist/printers/lexical-structure.js +0 -63
- package/dist/printers/names.js +0 -53
- package/dist/printers/packages-and-modules.js +0 -198
- package/dist/printers/prettier-builder.js +0 -55
- package/dist/printers/printer-utils.js +0 -640
- package/dist/printers/types-values-and-variables.js +0 -191
- package/dist/types/utils.js +0 -29
- package/dist/utils/expressions-utils.js +0 -29
- package/dist/utils/index.js +0 -12
- package/dist/utils/isEmptyDoc.js +0 -6
- package/dist/utils/printArgumentListWithBraces.js +0 -21
- package/dist/utils/printSingleLambdaInvocation.js +0 -20
|
@@ -1,640 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
|
-
if (ar || !(i in from)) {
|
|
16
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
17
|
-
ar[i] = from[i];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
-
};
|
|
22
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
-
var findIndex_1 = __importDefault(require("lodash/findIndex"));
|
|
28
|
-
var findLastIndex_1 = __importDefault(require("lodash/findLastIndex"));
|
|
29
|
-
var forEach_1 = __importDefault(require("lodash/forEach"));
|
|
30
|
-
var forEachRight_1 = __importDefault(require("lodash/forEachRight"));
|
|
31
|
-
var includes_1 = __importDefault(require("lodash/includes"));
|
|
32
|
-
var doc_1 = require("prettier/doc");
|
|
33
|
-
var utils_1 = require("../types/utils");
|
|
34
|
-
var utils_2 = require("../utils");
|
|
35
|
-
var comments_utils_1 = require("./comments/comments-utils");
|
|
36
|
-
var format_comments_1 = require("./comments/format-comments");
|
|
37
|
-
var prettier_builder_1 = require("./prettier-builder");
|
|
38
|
-
var indent = doc_1.builders.indent, hardline = doc_1.builders.hardline, line = doc_1.builders.line;
|
|
39
|
-
var orderedModifiers = [
|
|
40
|
-
"Public",
|
|
41
|
-
"Protected",
|
|
42
|
-
"Private",
|
|
43
|
-
"Abstract",
|
|
44
|
-
"Default",
|
|
45
|
-
"Static",
|
|
46
|
-
"Final",
|
|
47
|
-
"Transient",
|
|
48
|
-
"Volatile",
|
|
49
|
-
"Synchronized",
|
|
50
|
-
"Native",
|
|
51
|
-
"Sealed",
|
|
52
|
-
"NonSealed",
|
|
53
|
-
"Strictfp"
|
|
54
|
-
];
|
|
55
|
-
function buildFqn(tokens, dots) {
|
|
56
|
-
return rejectAndJoinSeps(dots ? dots : [], tokens);
|
|
57
|
-
}
|
|
58
|
-
exports.buildFqn = buildFqn;
|
|
59
|
-
function rejectAndJoinSeps(sepTokens, elems, sep) {
|
|
60
|
-
if (!Array.isArray(sepTokens)) {
|
|
61
|
-
return rejectAndJoin(sepTokens, elems);
|
|
62
|
-
}
|
|
63
|
-
var actualElements = reject(elems);
|
|
64
|
-
var res = [];
|
|
65
|
-
for (var i = 0; i < sepTokens.length; i++) {
|
|
66
|
-
res.push(actualElements[i], sepTokens[i]);
|
|
67
|
-
if (sep) {
|
|
68
|
-
res.push(sep);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
res.push.apply(res, actualElements.slice(sepTokens.length));
|
|
72
|
-
return (0, prettier_builder_1.concat)(res);
|
|
73
|
-
}
|
|
74
|
-
exports.rejectAndJoinSeps = rejectAndJoinSeps;
|
|
75
|
-
function reject(elems) {
|
|
76
|
-
return elems.filter(function (item) {
|
|
77
|
-
if (typeof item === "string") {
|
|
78
|
-
return item !== "";
|
|
79
|
-
}
|
|
80
|
-
// eslint-ignore next - We want the conversion to boolean!
|
|
81
|
-
// @ts-ignore
|
|
82
|
-
return item != false && item !== undefined;
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
exports.reject = reject;
|
|
86
|
-
function rejectSeparators(separators, elems) {
|
|
87
|
-
var realElements = reject(elems);
|
|
88
|
-
var realSeparators = [];
|
|
89
|
-
for (var i = 0; i < realElements.length - 1; i++) {
|
|
90
|
-
if (realElements[i] !== "") {
|
|
91
|
-
realSeparators.push(separators[i]);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return realSeparators;
|
|
95
|
-
}
|
|
96
|
-
exports.rejectSeparators = rejectSeparators;
|
|
97
|
-
function rejectAndJoin(sep, elems) {
|
|
98
|
-
var actualElements = reject(elems);
|
|
99
|
-
return (0, prettier_builder_1.join)(sep, actualElements);
|
|
100
|
-
}
|
|
101
|
-
exports.rejectAndJoin = rejectAndJoin;
|
|
102
|
-
function rejectAndConcat(elems) {
|
|
103
|
-
var actualElements = reject(elems);
|
|
104
|
-
return (0, prettier_builder_1.concat)(actualElements);
|
|
105
|
-
}
|
|
106
|
-
exports.rejectAndConcat = rejectAndConcat;
|
|
107
|
-
function sortAnnotationIdentifier(annotations, identifiers) {
|
|
108
|
-
var tokens = __spreadArray([], identifiers, true);
|
|
109
|
-
if (annotations && annotations.length > 0) {
|
|
110
|
-
tokens = __spreadArray(__spreadArray([], tokens, true), annotations, true);
|
|
111
|
-
}
|
|
112
|
-
return tokens.sort(function (a, b) {
|
|
113
|
-
var startOffset1 = (0, utils_1.isCstNode)(a)
|
|
114
|
-
? a.children.At[0].startOffset
|
|
115
|
-
: a.startOffset;
|
|
116
|
-
var startOffset2 = (0, utils_1.isCstNode)(b)
|
|
117
|
-
? b.children.At[0].startOffset
|
|
118
|
-
: b.startOffset;
|
|
119
|
-
return startOffset1 - startOffset2;
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
exports.sortAnnotationIdentifier = sortAnnotationIdentifier;
|
|
123
|
-
function sortTokens(values) {
|
|
124
|
-
var tokens = [];
|
|
125
|
-
(0, forEach_1.default)(values, function (argument) {
|
|
126
|
-
if (argument) {
|
|
127
|
-
tokens = tokens.concat(argument);
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
return tokens.sort(function (a, b) {
|
|
131
|
-
return a.startOffset - b.startOffset;
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
exports.sortTokens = sortTokens;
|
|
135
|
-
function sortNodes(values) {
|
|
136
|
-
var nodes = [];
|
|
137
|
-
(0, forEach_1.default)(values, function (argument) {
|
|
138
|
-
if (argument) {
|
|
139
|
-
nodes = nodes.concat(argument);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
return nodes.sort(function (a, b) {
|
|
143
|
-
var aOffset = a.location.startOffset;
|
|
144
|
-
var bOffset = b.location.startOffset;
|
|
145
|
-
return aOffset - bOffset;
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
exports.sortNodes = sortNodes;
|
|
149
|
-
function matchCategory(token, categoryName) {
|
|
150
|
-
var labels = (token.tokenType.CATEGORIES || []).map(function (category) {
|
|
151
|
-
return category.LABEL;
|
|
152
|
-
});
|
|
153
|
-
return labels.indexOf(categoryName) !== -1;
|
|
154
|
-
}
|
|
155
|
-
exports.matchCategory = matchCategory;
|
|
156
|
-
function sortClassTypeChildren(annotations, typeArguments, identifiers, dots) {
|
|
157
|
-
var tokens = __spreadArray([], identifiers, true);
|
|
158
|
-
if (annotations && annotations.length > 0) {
|
|
159
|
-
tokens = __spreadArray(__spreadArray([], tokens, true), annotations, true);
|
|
160
|
-
}
|
|
161
|
-
if (typeArguments && typeArguments.length > 0) {
|
|
162
|
-
tokens = __spreadArray(__spreadArray([], tokens, true), typeArguments, true);
|
|
163
|
-
}
|
|
164
|
-
if (dots && dots.length > 0) {
|
|
165
|
-
tokens = __spreadArray(__spreadArray([], tokens, true), dots, true);
|
|
166
|
-
}
|
|
167
|
-
return tokens.sort(function (a, b) {
|
|
168
|
-
var startOffsetA = (0, utils_1.isCstNode)(a)
|
|
169
|
-
? a.children.At
|
|
170
|
-
? a.children.At[0].startOffset
|
|
171
|
-
: a.children.Less[0].startOffset
|
|
172
|
-
: a.startOffset;
|
|
173
|
-
var startOffsetB = (0, utils_1.isCstNode)(b)
|
|
174
|
-
? b.children.At
|
|
175
|
-
? b.children.At[0].startOffset
|
|
176
|
-
: b.children.Less[0].startOffset
|
|
177
|
-
: b.startOffset;
|
|
178
|
-
return startOffsetA - startOffsetB;
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
exports.sortClassTypeChildren = sortClassTypeChildren;
|
|
182
|
-
function sortModifiers(modifiers) {
|
|
183
|
-
var firstAnnotations = [];
|
|
184
|
-
var otherModifiers = [];
|
|
185
|
-
var lastAnnotations = [];
|
|
186
|
-
var hasOtherModifier = false;
|
|
187
|
-
/**
|
|
188
|
-
* iterate in reverse order because we special-case
|
|
189
|
-
* type annotations which come after all other
|
|
190
|
-
* modifiers
|
|
191
|
-
*/
|
|
192
|
-
(0, forEachRight_1.default)(modifiers, function (modifier) {
|
|
193
|
-
var isAnnotation = modifier.children.annotation !== undefined;
|
|
194
|
-
var isTypeAnnotation = isAnnotation &&
|
|
195
|
-
(modifier.name === "methodModifier" ||
|
|
196
|
-
modifier.name === "interfaceMethodModifier" ||
|
|
197
|
-
modifier.name === "fieldModifier");
|
|
198
|
-
if (isAnnotation) {
|
|
199
|
-
if (isTypeAnnotation && !hasOtherModifier) {
|
|
200
|
-
lastAnnotations.unshift(modifier);
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
firstAnnotations.unshift(modifier);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
otherModifiers.unshift(modifier);
|
|
208
|
-
hasOtherModifier = true;
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
/**
|
|
212
|
-
* if there are only annotations, move everything from
|
|
213
|
-
* lastAnnotations to firstAnnotations
|
|
214
|
-
*/
|
|
215
|
-
if (!hasOtherModifier) {
|
|
216
|
-
firstAnnotations = firstAnnotations.concat(lastAnnotations);
|
|
217
|
-
lastAnnotations = [];
|
|
218
|
-
}
|
|
219
|
-
otherModifiers.sort(function (a, b) {
|
|
220
|
-
var modifierIndexA = orderedModifiers.indexOf(Object.keys(a.children)[0]);
|
|
221
|
-
var modifierIndexB = orderedModifiers.indexOf(Object.keys(b.children)[0]);
|
|
222
|
-
return modifierIndexA - modifierIndexB;
|
|
223
|
-
});
|
|
224
|
-
return [firstAnnotations, otherModifiers.concat(lastAnnotations)];
|
|
225
|
-
}
|
|
226
|
-
exports.sortModifiers = sortModifiers;
|
|
227
|
-
function findDeepElementInPartsArray(item, elt) {
|
|
228
|
-
if (Array.isArray(item)) {
|
|
229
|
-
if ((0, includes_1.default)(item, elt)) {
|
|
230
|
-
return true;
|
|
231
|
-
}
|
|
232
|
-
for (var i = 0; i < item.length; i++) {
|
|
233
|
-
if (findDeepElementInPartsArray(item[i], elt)) {
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
else {
|
|
239
|
-
for (var key in item) {
|
|
240
|
-
if (typeof item[key] === "object" &&
|
|
241
|
-
findDeepElementInPartsArray(item[key], elt)) {
|
|
242
|
-
return true;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return false;
|
|
247
|
-
}
|
|
248
|
-
exports.findDeepElementInPartsArray = findDeepElementInPartsArray;
|
|
249
|
-
function displaySemicolon(token, params) {
|
|
250
|
-
if (params !== undefined && params.allowEmptyStatement) {
|
|
251
|
-
return (0, format_comments_1.printTokenWithComments)(token);
|
|
252
|
-
}
|
|
253
|
-
if (!(0, comments_utils_1.hasComments)(token)) {
|
|
254
|
-
return "";
|
|
255
|
-
}
|
|
256
|
-
token.image = "";
|
|
257
|
-
return (0, format_comments_1.printTokenWithComments)(token);
|
|
258
|
-
}
|
|
259
|
-
exports.displaySemicolon = displaySemicolon;
|
|
260
|
-
function isExplicitLambdaParameter(ctx) {
|
|
261
|
-
return (ctx &&
|
|
262
|
-
ctx.lambdaParameterList &&
|
|
263
|
-
ctx.lambdaParameterList[0] &&
|
|
264
|
-
ctx.lambdaParameterList[0].children &&
|
|
265
|
-
ctx.lambdaParameterList[0].children.explicitLambdaParameterList);
|
|
266
|
-
}
|
|
267
|
-
exports.isExplicitLambdaParameter = isExplicitLambdaParameter;
|
|
268
|
-
function getBlankLinesSeparator(ctx, separator) {
|
|
269
|
-
if (separator === void 0) { separator = hardline; }
|
|
270
|
-
if (ctx === undefined) {
|
|
271
|
-
return [];
|
|
272
|
-
}
|
|
273
|
-
var separators = [];
|
|
274
|
-
for (var i = 0; i < ctx.length - 1; i++) {
|
|
275
|
-
var node = ctx[i];
|
|
276
|
-
var previousRuleEndLineWithComment = (0, comments_utils_1.hasTrailingComments)(node)
|
|
277
|
-
? node.trailingComments[node.trailingComments.length - 1].endLine
|
|
278
|
-
: node.location.endLine;
|
|
279
|
-
var nextNode = ctx[i + 1];
|
|
280
|
-
var nextRuleStartLineWithComment = (0, comments_utils_1.hasLeadingComments)(nextNode)
|
|
281
|
-
? nextNode.leadingComments[0].startLine
|
|
282
|
-
: nextNode.location.startLine;
|
|
283
|
-
if (nextRuleStartLineWithComment - previousRuleEndLineWithComment > 1) {
|
|
284
|
-
separators.push([hardline, hardline]);
|
|
285
|
-
}
|
|
286
|
-
else {
|
|
287
|
-
separators.push(separator);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return separators;
|
|
291
|
-
}
|
|
292
|
-
exports.getBlankLinesSeparator = getBlankLinesSeparator;
|
|
293
|
-
var isTwoHardLine = function (userBlankLinesSeparator) {
|
|
294
|
-
if (!Array.isArray(userBlankLinesSeparator)) {
|
|
295
|
-
return false;
|
|
296
|
-
}
|
|
297
|
-
return (userBlankLinesSeparator.length === 2 &&
|
|
298
|
-
userBlankLinesSeparator[0] === hardline &&
|
|
299
|
-
userBlankLinesSeparator[1] === hardline);
|
|
300
|
-
};
|
|
301
|
-
function getDeclarationsSeparator(declarations, needLineDeclaration, isSemicolon) {
|
|
302
|
-
var declarationsWithoutEmptyStatements = declarations.filter(function (declaration) { return !isSemicolon(declaration); });
|
|
303
|
-
var userBlankLinesSeparators = getBlankLinesSeparator(declarationsWithoutEmptyStatements);
|
|
304
|
-
var additionalBlankLines = declarationsWithoutEmptyStatements.map(needLineDeclaration);
|
|
305
|
-
var separators = [];
|
|
306
|
-
var indexNextNotEmptyDeclaration = 0;
|
|
307
|
-
for (var i = 0; i < declarations.length - 1; i++) {
|
|
308
|
-
// if the empty statement has comments
|
|
309
|
-
// we want to print them on their own line
|
|
310
|
-
if (isSemicolon(declarations[i])) {
|
|
311
|
-
if ((0, comments_utils_1.hasComments)(declarations[i])) {
|
|
312
|
-
separators.push(hardline);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
else if (indexNextNotEmptyDeclaration <
|
|
316
|
-
declarationsWithoutEmptyStatements.length - 1) {
|
|
317
|
-
var isNextSeparatorTwoHardLine = isTwoHardLine(userBlankLinesSeparators[indexNextNotEmptyDeclaration]);
|
|
318
|
-
var additionalSep = !isNextSeparatorTwoHardLine &&
|
|
319
|
-
(additionalBlankLines[indexNextNotEmptyDeclaration + 1] ||
|
|
320
|
-
additionalBlankLines[indexNextNotEmptyDeclaration])
|
|
321
|
-
? hardline
|
|
322
|
-
: "";
|
|
323
|
-
separators.push((0, prettier_builder_1.concat)([
|
|
324
|
-
userBlankLinesSeparators[indexNextNotEmptyDeclaration],
|
|
325
|
-
additionalSep
|
|
326
|
-
]));
|
|
327
|
-
indexNextNotEmptyDeclaration += 1;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
return separators;
|
|
331
|
-
}
|
|
332
|
-
function needLineClassBodyDeclaration(declaration) {
|
|
333
|
-
if (declaration.children.classMemberDeclaration === undefined) {
|
|
334
|
-
return true;
|
|
335
|
-
}
|
|
336
|
-
var classMemberDeclaration = declaration.children.classMemberDeclaration[0];
|
|
337
|
-
if (classMemberDeclaration.children.fieldDeclaration !== undefined) {
|
|
338
|
-
var fieldDeclaration = classMemberDeclaration.children.fieldDeclaration[0];
|
|
339
|
-
if (fieldDeclaration.children.fieldModifier !== undefined &&
|
|
340
|
-
hasAnnotation(fieldDeclaration.children.fieldModifier) &&
|
|
341
|
-
hasNonTrailingAnnotation(fieldDeclaration.children.fieldModifier)) {
|
|
342
|
-
return true;
|
|
343
|
-
}
|
|
344
|
-
return false;
|
|
345
|
-
}
|
|
346
|
-
else if (classMemberDeclaration.children.Semicolon !== undefined) {
|
|
347
|
-
return false;
|
|
348
|
-
}
|
|
349
|
-
return true;
|
|
350
|
-
}
|
|
351
|
-
function needLineInterfaceMemberDeclaration(declaration) {
|
|
352
|
-
if (declaration.children.constantDeclaration !== undefined) {
|
|
353
|
-
var constantDeclaration = declaration.children.constantDeclaration[0];
|
|
354
|
-
if (constantDeclaration.children.constantModifier !== undefined &&
|
|
355
|
-
hasAnnotation(constantDeclaration.children.constantModifier) &&
|
|
356
|
-
hasNonTrailingAnnotation(constantDeclaration.children.constantModifier)) {
|
|
357
|
-
return true;
|
|
358
|
-
}
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
|
-
else if (declaration.children.interfaceMethodDeclaration !== undefined) {
|
|
362
|
-
var interfaceMethodDeclaration = declaration.children.interfaceMethodDeclaration[0];
|
|
363
|
-
if (interfaceMethodDeclaration.children.interfaceMethodModifier !==
|
|
364
|
-
undefined &&
|
|
365
|
-
hasNonTrailingAnnotation(interfaceMethodDeclaration.children.interfaceMethodModifier)) {
|
|
366
|
-
return true;
|
|
367
|
-
}
|
|
368
|
-
return false;
|
|
369
|
-
}
|
|
370
|
-
return true;
|
|
371
|
-
}
|
|
372
|
-
function isClassBodyDeclarationASemicolon(classBodyDeclaration) {
|
|
373
|
-
if (classBodyDeclaration.children.classMemberDeclaration) {
|
|
374
|
-
if (classBodyDeclaration.children.classMemberDeclaration[0].children
|
|
375
|
-
.Semicolon !== undefined) {
|
|
376
|
-
return true;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
return false;
|
|
380
|
-
}
|
|
381
|
-
function isInterfaceMemberASemicolon(interfaceMemberDeclaration) {
|
|
382
|
-
return interfaceMemberDeclaration.children.Semicolon !== undefined;
|
|
383
|
-
}
|
|
384
|
-
function hasAnnotation(modifiers) {
|
|
385
|
-
return modifiers.some(function (modifier) { return modifier.children.annotation !== undefined; });
|
|
386
|
-
}
|
|
387
|
-
/**
|
|
388
|
-
* Return true if there is a modifier that does not come after all other modifiers
|
|
389
|
-
* It is useful to know if sortModifiers will add an annotation before other modifiers
|
|
390
|
-
*
|
|
391
|
-
* @param modifiers
|
|
392
|
-
* @returns {boolean}
|
|
393
|
-
*/
|
|
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; });
|
|
397
|
-
return (firstAnnotationIndex < lastNonAnnotationIndex ||
|
|
398
|
-
lastNonAnnotationIndex === -1);
|
|
399
|
-
}
|
|
400
|
-
function getClassBodyDeclarationsSeparator(classBodyDeclarationContext) {
|
|
401
|
-
return getDeclarationsSeparator(classBodyDeclarationContext, needLineClassBodyDeclaration, isClassBodyDeclarationASemicolon);
|
|
402
|
-
}
|
|
403
|
-
exports.getClassBodyDeclarationsSeparator = getClassBodyDeclarationsSeparator;
|
|
404
|
-
function getInterfaceBodyDeclarationsSeparator(interfaceMemberDeclarationContext) {
|
|
405
|
-
return getDeclarationsSeparator(interfaceMemberDeclarationContext, needLineInterfaceMemberDeclaration, isInterfaceMemberASemicolon);
|
|
406
|
-
}
|
|
407
|
-
exports.getInterfaceBodyDeclarationsSeparator = getInterfaceBodyDeclarationsSeparator;
|
|
408
|
-
function getAndRemoveLeadingComment(doc) {
|
|
409
|
-
var isTokenWithLeadingComment = typeof doc !== "string" && "leadingComments" in doc;
|
|
410
|
-
if (!isTokenWithLeadingComment) {
|
|
411
|
-
return [];
|
|
412
|
-
}
|
|
413
|
-
var leadingComments = (0, format_comments_1.getTokenLeadingComments)(doc);
|
|
414
|
-
delete doc.leadingComments;
|
|
415
|
-
return leadingComments;
|
|
416
|
-
}
|
|
417
|
-
function putIntoBraces(argument, separator, LBrace, RBrace) {
|
|
418
|
-
var rightBraceLeadingComments = getAndRemoveLeadingComment(RBrace);
|
|
419
|
-
var lastBreakLine =
|
|
420
|
-
// check if last element of the array is a line
|
|
421
|
-
rightBraceLeadingComments.length !== 0 &&
|
|
422
|
-
rightBraceLeadingComments[rightBraceLeadingComments.length - 1] === hardline
|
|
423
|
-
? rightBraceLeadingComments.pop()
|
|
424
|
-
: separator;
|
|
425
|
-
var contentInsideBraces;
|
|
426
|
-
if ((0, utils_2.isEmptyDoc)(argument)) {
|
|
427
|
-
if (rightBraceLeadingComments.length === 0) {
|
|
428
|
-
return (0, prettier_builder_1.concat)([LBrace, RBrace]);
|
|
429
|
-
}
|
|
430
|
-
contentInsideBraces = __spreadArray([separator], rightBraceLeadingComments, true);
|
|
431
|
-
}
|
|
432
|
-
else if (rightBraceLeadingComments.length !== 0) {
|
|
433
|
-
contentInsideBraces = __spreadArray([
|
|
434
|
-
separator,
|
|
435
|
-
argument,
|
|
436
|
-
separator
|
|
437
|
-
], rightBraceLeadingComments, true);
|
|
438
|
-
}
|
|
439
|
-
else {
|
|
440
|
-
contentInsideBraces = [separator, argument];
|
|
441
|
-
}
|
|
442
|
-
return (0, prettier_builder_1.group)(rejectAndConcat([
|
|
443
|
-
LBrace,
|
|
444
|
-
indent((0, prettier_builder_1.concat)(contentInsideBraces)),
|
|
445
|
-
lastBreakLine,
|
|
446
|
-
RBrace
|
|
447
|
-
]));
|
|
448
|
-
}
|
|
449
|
-
exports.putIntoBraces = putIntoBraces;
|
|
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
|
-
}
|
|
491
|
-
}
|
|
492
|
-
else {
|
|
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);
|
|
498
|
-
}
|
|
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;
|
|
503
|
-
}
|
|
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;
|
|
518
|
-
}
|
|
519
|
-
repeatedTokenCount++;
|
|
520
|
-
}
|
|
521
|
-
if (repeatedTokenCount === 1) {
|
|
522
|
-
return image;
|
|
523
|
-
}
|
|
524
|
-
if (image === "<") {
|
|
525
|
-
return "<<";
|
|
526
|
-
}
|
|
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 === "%"));
|
|
561
|
-
}
|
|
562
|
-
function isStatementEmptyStatement(statement) {
|
|
563
|
-
return (statement === ";" || (Array.isArray(statement) && statement[0] === ";"));
|
|
564
|
-
}
|
|
565
|
-
exports.isStatementEmptyStatement = isStatementEmptyStatement;
|
|
566
|
-
function sortImports(imports) {
|
|
567
|
-
var staticImports = [];
|
|
568
|
-
var nonStaticImports = [];
|
|
569
|
-
if (imports !== undefined) {
|
|
570
|
-
for (var i = 0; i < imports.length; i++) {
|
|
571
|
-
if (imports[i].children.Static !== undefined) {
|
|
572
|
-
staticImports.push(imports[i]);
|
|
573
|
-
}
|
|
574
|
-
else if (imports[i].children.emptyStatement === undefined) {
|
|
575
|
-
nonStaticImports.push(imports[i]);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
// TODO: Could be optimized as we could expect that the array is already almost sorted
|
|
579
|
-
var comparator = function (first, second) {
|
|
580
|
-
return compareFqn(first.children.packageOrTypeName[0], second.children.packageOrTypeName[0]);
|
|
581
|
-
};
|
|
582
|
-
staticImports.sort(comparator);
|
|
583
|
-
nonStaticImports.sort(comparator);
|
|
584
|
-
}
|
|
585
|
-
return {
|
|
586
|
-
staticImports: staticImports,
|
|
587
|
-
nonStaticImports: nonStaticImports
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
exports.sortImports = sortImports;
|
|
591
|
-
function compareFqn(packageOrTypeNameFirst, packageOrTypeNameSecond) {
|
|
592
|
-
var identifiersFirst = packageOrTypeNameFirst.children.Identifier;
|
|
593
|
-
var identifiersSecond = packageOrTypeNameSecond.children.Identifier;
|
|
594
|
-
var minParts = Math.min(identifiersFirst.length, identifiersSecond.length);
|
|
595
|
-
for (var i = 0; i < minParts; i++) {
|
|
596
|
-
if (identifiersFirst[i].image < identifiersSecond[i].image) {
|
|
597
|
-
return -1;
|
|
598
|
-
}
|
|
599
|
-
else if (identifiersFirst[i].image > identifiersSecond[i].image) {
|
|
600
|
-
return 1;
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
if (identifiersFirst.length < identifiersSecond.length) {
|
|
604
|
-
return -1;
|
|
605
|
-
}
|
|
606
|
-
else if (identifiersFirst.length > identifiersSecond.length) {
|
|
607
|
-
return 1;
|
|
608
|
-
}
|
|
609
|
-
return 0;
|
|
610
|
-
}
|
|
611
|
-
function isUniqueMethodInvocation(primarySuffixes) {
|
|
612
|
-
if (primarySuffixes === undefined) {
|
|
613
|
-
return 0;
|
|
614
|
-
}
|
|
615
|
-
var count = 0;
|
|
616
|
-
primarySuffixes.forEach(function (primarySuffix) {
|
|
617
|
-
if (primarySuffix.children.methodInvocationSuffix !== undefined) {
|
|
618
|
-
count++;
|
|
619
|
-
if (count > 1) {
|
|
620
|
-
return 2;
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
});
|
|
624
|
-
return count;
|
|
625
|
-
}
|
|
626
|
-
exports.isUniqueMethodInvocation = isUniqueMethodInvocation;
|
|
627
|
-
function printArrayList(_a) {
|
|
628
|
-
var list = _a.list, extraComma = _a.extraComma, LCurly = _a.LCurly, RCurly = _a.RCurly, trailingComma = _a.trailingComma;
|
|
629
|
-
var optionalComma;
|
|
630
|
-
if (trailingComma !== "none" && list !== "") {
|
|
631
|
-
optionalComma = extraComma
|
|
632
|
-
? (0, prettier_builder_1.ifBreak)(extraComma[0], __assign(__assign({}, extraComma[0]), { image: "" }))
|
|
633
|
-
: (0, prettier_builder_1.ifBreak)(",", "");
|
|
634
|
-
}
|
|
635
|
-
else {
|
|
636
|
-
optionalComma = extraComma ? __assign(__assign({}, extraComma[0]), { image: "" }) : "";
|
|
637
|
-
}
|
|
638
|
-
return putIntoBraces(rejectAndConcat([list, optionalComma]), line, LCurly, RCurly);
|
|
639
|
-
}
|
|
640
|
-
exports.printArrayList = printArrayList;
|