prettier-plugin-java 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/base-cst-printer.js +22 -44
- package/dist/cst-printer.js +19 -27
- package/dist/index.js +19 -20
- package/dist/options.js +13 -17
- package/dist/parser.js +2 -4
- package/dist/printer.js +3 -5
- package/dist/printers/arrays.js +17 -41
- package/dist/printers/blocks-and-statements.js +281 -326
- package/dist/printers/classes.js +449 -496
- package/dist/printers/comments/comments-utils.js +5 -13
- package/dist/printers/comments/format-comments.js +34 -50
- package/dist/printers/comments/handle-comments.js +17 -23
- package/dist/printers/expressions.js +377 -391
- package/dist/printers/interfaces.js +148 -181
- package/dist/printers/lexical-structure.js +26 -58
- package/dist/printers/names.js +29 -53
- package/dist/printers/packages-and-modules.js +116 -143
- package/dist/printers/prettier-builder.js +21 -31
- package/dist/printers/printer-utils.js +173 -221
- package/dist/printers/types-values-and-variables.js +111 -149
- package/dist/types/utils.js +6 -15
- package/dist/utils/expressions-utils.js +7 -11
- package/dist/utils/index.js +2 -12
- package/dist/utils/isEmptyDoc.js +2 -4
- package/dist/utils/printArgumentListWithBraces.js +29 -19
- package/dist/utils/printSingleLambdaInvocation.js +15 -17
- package/package.json +10 -13
|
@@ -1,42 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
|
|
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 = [
|
|
1
|
+
import findIndex from "lodash/findIndex.js";
|
|
2
|
+
import findLastIndex from "lodash/findLastIndex.js";
|
|
3
|
+
import forEach from "lodash/forEach.js";
|
|
4
|
+
import forEachRight from "lodash/forEachRight.js";
|
|
5
|
+
import includes from "lodash/includes.js";
|
|
6
|
+
import { builders } from "prettier/doc";
|
|
7
|
+
import { isCstNode } from "../types/utils.js";
|
|
8
|
+
import { isEmptyDoc } from "../utils/index.js";
|
|
9
|
+
import { hasComments, hasLeadingComments, hasTrailingComments } from "./comments/comments-utils.js";
|
|
10
|
+
import { getTokenLeadingComments, printTokenWithComments } from "./comments/format-comments.js";
|
|
11
|
+
import { concat, group, ifBreak, indentIfBreak, join } from "./prettier-builder.js";
|
|
12
|
+
const { indent, hardline, line } = builders;
|
|
13
|
+
const orderedModifiers = [
|
|
40
14
|
"Public",
|
|
41
15
|
"Protected",
|
|
42
16
|
"Private",
|
|
@@ -52,28 +26,26 @@ var orderedModifiers = [
|
|
|
52
26
|
"NonSealed",
|
|
53
27
|
"Strictfp"
|
|
54
28
|
];
|
|
55
|
-
function buildFqn(tokens, dots) {
|
|
29
|
+
export function buildFqn(tokens, dots) {
|
|
56
30
|
return rejectAndJoinSeps(dots ? dots : [], tokens);
|
|
57
31
|
}
|
|
58
|
-
|
|
59
|
-
function rejectAndJoinSeps(sepTokens, elems, sep) {
|
|
32
|
+
export function rejectAndJoinSeps(sepTokens, elems, sep) {
|
|
60
33
|
if (!Array.isArray(sepTokens)) {
|
|
61
34
|
return rejectAndJoin(sepTokens, elems);
|
|
62
35
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
for (
|
|
36
|
+
const actualElements = reject(elems);
|
|
37
|
+
const res = [];
|
|
38
|
+
for (let i = 0; i < sepTokens.length; i++) {
|
|
66
39
|
res.push(actualElements[i], sepTokens[i]);
|
|
67
40
|
if (sep) {
|
|
68
41
|
res.push(sep);
|
|
69
42
|
}
|
|
70
43
|
}
|
|
71
|
-
res.push
|
|
72
|
-
return
|
|
44
|
+
res.push(...actualElements.slice(sepTokens.length));
|
|
45
|
+
return concat(res);
|
|
73
46
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return elems.filter(function (item) {
|
|
47
|
+
export function reject(elems) {
|
|
48
|
+
return elems.filter(item => {
|
|
77
49
|
if (typeof item === "string") {
|
|
78
50
|
return item !== "";
|
|
79
51
|
}
|
|
@@ -82,95 +54,87 @@ function reject(elems) {
|
|
|
82
54
|
return item != false && item !== undefined;
|
|
83
55
|
});
|
|
84
56
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
for (var i = 0; i < realElements.length - 1; i++) {
|
|
57
|
+
export function rejectSeparators(separators, elems) {
|
|
58
|
+
const realElements = reject(elems);
|
|
59
|
+
const realSeparators = [];
|
|
60
|
+
for (let i = 0; i < realElements.length - 1; i++) {
|
|
90
61
|
if (realElements[i] !== "") {
|
|
91
62
|
realSeparators.push(separators[i]);
|
|
92
63
|
}
|
|
93
64
|
}
|
|
94
65
|
return realSeparators;
|
|
95
66
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return (0, prettier_builder_1.join)(sep, actualElements);
|
|
67
|
+
export function rejectAndJoin(sep, elems) {
|
|
68
|
+
const actualElements = reject(elems);
|
|
69
|
+
return join(sep, actualElements);
|
|
100
70
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return (0, prettier_builder_1.concat)(actualElements);
|
|
71
|
+
export function rejectAndConcat(elems) {
|
|
72
|
+
const actualElements = reject(elems);
|
|
73
|
+
return concat(actualElements);
|
|
105
74
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
var tokens = __spreadArray([], identifiers, true);
|
|
75
|
+
export function sortAnnotationIdentifier(annotations, identifiers) {
|
|
76
|
+
let tokens = [...identifiers];
|
|
109
77
|
if (annotations && annotations.length > 0) {
|
|
110
|
-
tokens =
|
|
78
|
+
tokens = [...tokens, ...annotations];
|
|
111
79
|
}
|
|
112
|
-
return tokens.sort(
|
|
113
|
-
|
|
80
|
+
return tokens.sort((a, b) => {
|
|
81
|
+
const startOffset1 = isCstNode(a)
|
|
114
82
|
? a.children.At[0].startOffset
|
|
115
83
|
: a.startOffset;
|
|
116
|
-
|
|
84
|
+
const startOffset2 = isCstNode(b)
|
|
117
85
|
? b.children.At[0].startOffset
|
|
118
86
|
: b.startOffset;
|
|
119
87
|
return startOffset1 - startOffset2;
|
|
120
88
|
});
|
|
121
89
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
(0, forEach_1.default)(values, function (argument) {
|
|
90
|
+
export function sortTokens(values) {
|
|
91
|
+
let tokens = [];
|
|
92
|
+
forEach(values, argument => {
|
|
126
93
|
if (argument) {
|
|
127
94
|
tokens = tokens.concat(argument);
|
|
128
95
|
}
|
|
129
96
|
});
|
|
130
|
-
return tokens.sort(
|
|
97
|
+
return tokens.sort((a, b) => {
|
|
131
98
|
return a.startOffset - b.startOffset;
|
|
132
99
|
});
|
|
133
100
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
(0, forEach_1.default)(values, function (argument) {
|
|
101
|
+
export function sortNodes(values) {
|
|
102
|
+
let nodes = [];
|
|
103
|
+
forEach(values, argument => {
|
|
138
104
|
if (argument) {
|
|
139
105
|
nodes = nodes.concat(argument);
|
|
140
106
|
}
|
|
141
107
|
});
|
|
142
|
-
return nodes.sort(
|
|
143
|
-
|
|
144
|
-
|
|
108
|
+
return nodes.sort((a, b) => {
|
|
109
|
+
const aOffset = a.location.startOffset;
|
|
110
|
+
const bOffset = b.location.startOffset;
|
|
145
111
|
return aOffset - bOffset;
|
|
146
112
|
});
|
|
147
113
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
var labels = (token.tokenType.CATEGORIES || []).map(function (category) {
|
|
114
|
+
export function matchCategory(token, categoryName) {
|
|
115
|
+
const labels = (token.tokenType.CATEGORIES || []).map(category => {
|
|
151
116
|
return category.LABEL;
|
|
152
117
|
});
|
|
153
118
|
return labels.indexOf(categoryName) !== -1;
|
|
154
119
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
var tokens = __spreadArray([], identifiers, true);
|
|
120
|
+
export function sortClassTypeChildren(annotations, typeArguments, identifiers, dots) {
|
|
121
|
+
let tokens = [...identifiers];
|
|
158
122
|
if (annotations && annotations.length > 0) {
|
|
159
|
-
tokens =
|
|
123
|
+
tokens = [...tokens, ...annotations];
|
|
160
124
|
}
|
|
161
125
|
if (typeArguments && typeArguments.length > 0) {
|
|
162
|
-
tokens =
|
|
126
|
+
tokens = [...tokens, ...typeArguments];
|
|
163
127
|
}
|
|
164
128
|
if (dots && dots.length > 0) {
|
|
165
|
-
tokens =
|
|
129
|
+
tokens = [...tokens, ...dots];
|
|
166
130
|
}
|
|
167
|
-
return tokens.sort(
|
|
168
|
-
|
|
131
|
+
return tokens.sort((a, b) => {
|
|
132
|
+
const startOffsetA = isCstNode(a)
|
|
169
133
|
? a.children.At
|
|
170
134
|
? a.children.At[0].startOffset
|
|
171
135
|
: a.children.Less[0].startOffset
|
|
172
136
|
: a.startOffset;
|
|
173
|
-
|
|
137
|
+
const startOffsetB = isCstNode(b)
|
|
174
138
|
? b.children.At
|
|
175
139
|
? b.children.At[0].startOffset
|
|
176
140
|
: b.children.Less[0].startOffset
|
|
@@ -178,20 +142,19 @@ function sortClassTypeChildren(annotations, typeArguments, identifiers, dots) {
|
|
|
178
142
|
return startOffsetA - startOffsetB;
|
|
179
143
|
});
|
|
180
144
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
var hasOtherModifier = false;
|
|
145
|
+
export function sortModifiers(modifiers) {
|
|
146
|
+
let firstAnnotations = [];
|
|
147
|
+
const otherModifiers = [];
|
|
148
|
+
let lastAnnotations = [];
|
|
149
|
+
let hasOtherModifier = false;
|
|
187
150
|
/**
|
|
188
151
|
* iterate in reverse order because we special-case
|
|
189
152
|
* type annotations which come after all other
|
|
190
153
|
* modifiers
|
|
191
154
|
*/
|
|
192
|
-
(
|
|
193
|
-
|
|
194
|
-
|
|
155
|
+
forEachRight(modifiers, modifier => {
|
|
156
|
+
const isAnnotation = modifier.children.annotation !== undefined;
|
|
157
|
+
const isTypeAnnotation = isAnnotation &&
|
|
195
158
|
(modifier.name === "methodModifier" ||
|
|
196
159
|
modifier.name === "interfaceMethodModifier" ||
|
|
197
160
|
modifier.name === "fieldModifier");
|
|
@@ -216,27 +179,26 @@ function sortModifiers(modifiers) {
|
|
|
216
179
|
firstAnnotations = firstAnnotations.concat(lastAnnotations);
|
|
217
180
|
lastAnnotations = [];
|
|
218
181
|
}
|
|
219
|
-
otherModifiers.sort(
|
|
220
|
-
|
|
221
|
-
|
|
182
|
+
otherModifiers.sort((a, b) => {
|
|
183
|
+
const modifierIndexA = orderedModifiers.indexOf(Object.keys(a.children)[0]);
|
|
184
|
+
const modifierIndexB = orderedModifiers.indexOf(Object.keys(b.children)[0]);
|
|
222
185
|
return modifierIndexA - modifierIndexB;
|
|
223
186
|
});
|
|
224
187
|
return [firstAnnotations, otherModifiers.concat(lastAnnotations)];
|
|
225
188
|
}
|
|
226
|
-
|
|
227
|
-
function findDeepElementInPartsArray(item, elt) {
|
|
189
|
+
export function findDeepElementInPartsArray(item, elt) {
|
|
228
190
|
if (Array.isArray(item)) {
|
|
229
|
-
if ((
|
|
191
|
+
if (includes(item, elt)) {
|
|
230
192
|
return true;
|
|
231
193
|
}
|
|
232
|
-
for (
|
|
194
|
+
for (let i = 0; i < item.length; i++) {
|
|
233
195
|
if (findDeepElementInPartsArray(item[i], elt)) {
|
|
234
196
|
return true;
|
|
235
197
|
}
|
|
236
198
|
}
|
|
237
199
|
}
|
|
238
200
|
else {
|
|
239
|
-
for (
|
|
201
|
+
for (const key in item) {
|
|
240
202
|
if (typeof item[key] === "object" &&
|
|
241
203
|
findDeepElementInPartsArray(item[key], elt)) {
|
|
242
204
|
return true;
|
|
@@ -245,39 +207,35 @@ function findDeepElementInPartsArray(item, elt) {
|
|
|
245
207
|
}
|
|
246
208
|
return false;
|
|
247
209
|
}
|
|
248
|
-
|
|
249
|
-
function displaySemicolon(token, params) {
|
|
210
|
+
export function displaySemicolon(token, params) {
|
|
250
211
|
if (params !== undefined && params.allowEmptyStatement) {
|
|
251
|
-
return
|
|
212
|
+
return printTokenWithComments(token);
|
|
252
213
|
}
|
|
253
|
-
if (!
|
|
214
|
+
if (!hasComments(token)) {
|
|
254
215
|
return "";
|
|
255
216
|
}
|
|
256
217
|
token.image = "";
|
|
257
|
-
return
|
|
218
|
+
return printTokenWithComments(token);
|
|
258
219
|
}
|
|
259
|
-
|
|
260
|
-
function isExplicitLambdaParameter(ctx) {
|
|
220
|
+
export function isExplicitLambdaParameter(ctx) {
|
|
261
221
|
return (ctx &&
|
|
262
222
|
ctx.lambdaParameterList &&
|
|
263
223
|
ctx.lambdaParameterList[0] &&
|
|
264
224
|
ctx.lambdaParameterList[0].children &&
|
|
265
225
|
ctx.lambdaParameterList[0].children.explicitLambdaParameterList);
|
|
266
226
|
}
|
|
267
|
-
|
|
268
|
-
function getBlankLinesSeparator(ctx, separator) {
|
|
269
|
-
if (separator === void 0) { separator = hardline; }
|
|
227
|
+
export function getBlankLinesSeparator(ctx, separator = hardline) {
|
|
270
228
|
if (ctx === undefined) {
|
|
271
229
|
return [];
|
|
272
230
|
}
|
|
273
|
-
|
|
274
|
-
for (
|
|
275
|
-
|
|
276
|
-
|
|
231
|
+
const separators = [];
|
|
232
|
+
for (let i = 0; i < ctx.length - 1; i++) {
|
|
233
|
+
const node = ctx[i];
|
|
234
|
+
const previousRuleEndLineWithComment = hasTrailingComments(node)
|
|
277
235
|
? node.trailingComments[node.trailingComments.length - 1].endLine
|
|
278
236
|
: node.location.endLine;
|
|
279
|
-
|
|
280
|
-
|
|
237
|
+
const nextNode = ctx[i + 1];
|
|
238
|
+
const nextRuleStartLineWithComment = hasLeadingComments(nextNode)
|
|
281
239
|
? nextNode.leadingComments[0].startLine
|
|
282
240
|
: nextNode.location.startLine;
|
|
283
241
|
if (nextRuleStartLineWithComment - previousRuleEndLineWithComment > 1) {
|
|
@@ -289,8 +247,7 @@ function getBlankLinesSeparator(ctx, separator) {
|
|
|
289
247
|
}
|
|
290
248
|
return separators;
|
|
291
249
|
}
|
|
292
|
-
|
|
293
|
-
var isTwoHardLine = function (userBlankLinesSeparator) {
|
|
250
|
+
const isTwoHardLine = (userBlankLinesSeparator) => {
|
|
294
251
|
if (!Array.isArray(userBlankLinesSeparator)) {
|
|
295
252
|
return false;
|
|
296
253
|
}
|
|
@@ -299,28 +256,28 @@ var isTwoHardLine = function (userBlankLinesSeparator) {
|
|
|
299
256
|
userBlankLinesSeparator[1] === hardline);
|
|
300
257
|
};
|
|
301
258
|
function getDeclarationsSeparator(declarations, needLineDeclaration, isSemicolon) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
for (
|
|
259
|
+
const declarationsWithoutEmptyStatements = declarations.filter(declaration => !isSemicolon(declaration));
|
|
260
|
+
const userBlankLinesSeparators = getBlankLinesSeparator(declarationsWithoutEmptyStatements);
|
|
261
|
+
const additionalBlankLines = declarationsWithoutEmptyStatements.map(needLineDeclaration);
|
|
262
|
+
const separators = [];
|
|
263
|
+
let indexNextNotEmptyDeclaration = 0;
|
|
264
|
+
for (let i = 0; i < declarations.length - 1; i++) {
|
|
308
265
|
// if the empty statement has comments
|
|
309
266
|
// we want to print them on their own line
|
|
310
267
|
if (isSemicolon(declarations[i])) {
|
|
311
|
-
if (
|
|
268
|
+
if (hasComments(declarations[i])) {
|
|
312
269
|
separators.push(hardline);
|
|
313
270
|
}
|
|
314
271
|
}
|
|
315
272
|
else if (indexNextNotEmptyDeclaration <
|
|
316
273
|
declarationsWithoutEmptyStatements.length - 1) {
|
|
317
|
-
|
|
318
|
-
|
|
274
|
+
const isNextSeparatorTwoHardLine = isTwoHardLine(userBlankLinesSeparators[indexNextNotEmptyDeclaration]);
|
|
275
|
+
const additionalSep = !isNextSeparatorTwoHardLine &&
|
|
319
276
|
(additionalBlankLines[indexNextNotEmptyDeclaration + 1] ||
|
|
320
277
|
additionalBlankLines[indexNextNotEmptyDeclaration])
|
|
321
278
|
? hardline
|
|
322
279
|
: "";
|
|
323
|
-
separators.push(
|
|
280
|
+
separators.push(concat([
|
|
324
281
|
userBlankLinesSeparators[indexNextNotEmptyDeclaration],
|
|
325
282
|
additionalSep
|
|
326
283
|
]));
|
|
@@ -333,9 +290,9 @@ function needLineClassBodyDeclaration(declaration) {
|
|
|
333
290
|
if (declaration.children.classMemberDeclaration === undefined) {
|
|
334
291
|
return true;
|
|
335
292
|
}
|
|
336
|
-
|
|
293
|
+
const classMemberDeclaration = declaration.children.classMemberDeclaration[0];
|
|
337
294
|
if (classMemberDeclaration.children.fieldDeclaration !== undefined) {
|
|
338
|
-
|
|
295
|
+
const fieldDeclaration = classMemberDeclaration.children.fieldDeclaration[0];
|
|
339
296
|
if (fieldDeclaration.children.fieldModifier !== undefined &&
|
|
340
297
|
hasAnnotation(fieldDeclaration.children.fieldModifier) &&
|
|
341
298
|
hasNonTrailingAnnotation(fieldDeclaration.children.fieldModifier)) {
|
|
@@ -350,7 +307,7 @@ function needLineClassBodyDeclaration(declaration) {
|
|
|
350
307
|
}
|
|
351
308
|
function needLineInterfaceMemberDeclaration(declaration) {
|
|
352
309
|
if (declaration.children.constantDeclaration !== undefined) {
|
|
353
|
-
|
|
310
|
+
const constantDeclaration = declaration.children.constantDeclaration[0];
|
|
354
311
|
if (constantDeclaration.children.constantModifier !== undefined &&
|
|
355
312
|
hasAnnotation(constantDeclaration.children.constantModifier) &&
|
|
356
313
|
hasNonTrailingAnnotation(constantDeclaration.children.constantModifier)) {
|
|
@@ -359,7 +316,7 @@ function needLineInterfaceMemberDeclaration(declaration) {
|
|
|
359
316
|
return false;
|
|
360
317
|
}
|
|
361
318
|
else if (declaration.children.interfaceMethodDeclaration !== undefined) {
|
|
362
|
-
|
|
319
|
+
const interfaceMethodDeclaration = declaration.children.interfaceMethodDeclaration[0];
|
|
363
320
|
if (interfaceMethodDeclaration.children.interfaceMethodModifier !==
|
|
364
321
|
undefined &&
|
|
365
322
|
hasNonTrailingAnnotation(interfaceMethodDeclaration.children.interfaceMethodModifier)) {
|
|
@@ -382,7 +339,7 @@ function isInterfaceMemberASemicolon(interfaceMemberDeclaration) {
|
|
|
382
339
|
return interfaceMemberDeclaration.children.Semicolon !== undefined;
|
|
383
340
|
}
|
|
384
341
|
function hasAnnotation(modifiers) {
|
|
385
|
-
return modifiers.some(
|
|
342
|
+
return modifiers.some(modifier => modifier.children.annotation !== undefined);
|
|
386
343
|
}
|
|
387
344
|
/**
|
|
388
345
|
* Return true if there is a modifier that does not come after all other modifiers
|
|
@@ -392,83 +349,80 @@ function hasAnnotation(modifiers) {
|
|
|
392
349
|
* @returns {boolean}
|
|
393
350
|
*/
|
|
394
351
|
function hasNonTrailingAnnotation(modifiers) {
|
|
395
|
-
|
|
396
|
-
|
|
352
|
+
const firstAnnotationIndex = findIndex(modifiers, modifier => modifier.children.annotation !== undefined);
|
|
353
|
+
const lastNonAnnotationIndex = findLastIndex(modifiers, modifier => modifier.children.annotation === undefined);
|
|
397
354
|
return (firstAnnotationIndex < lastNonAnnotationIndex ||
|
|
398
355
|
lastNonAnnotationIndex === -1);
|
|
399
356
|
}
|
|
400
|
-
function getClassBodyDeclarationsSeparator(classBodyDeclarationContext) {
|
|
357
|
+
export function getClassBodyDeclarationsSeparator(classBodyDeclarationContext) {
|
|
401
358
|
return getDeclarationsSeparator(classBodyDeclarationContext, needLineClassBodyDeclaration, isClassBodyDeclarationASemicolon);
|
|
402
359
|
}
|
|
403
|
-
|
|
404
|
-
function getInterfaceBodyDeclarationsSeparator(interfaceMemberDeclarationContext) {
|
|
360
|
+
export function getInterfaceBodyDeclarationsSeparator(interfaceMemberDeclarationContext) {
|
|
405
361
|
return getDeclarationsSeparator(interfaceMemberDeclarationContext, needLineInterfaceMemberDeclaration, isInterfaceMemberASemicolon);
|
|
406
362
|
}
|
|
407
|
-
exports.getInterfaceBodyDeclarationsSeparator = getInterfaceBodyDeclarationsSeparator;
|
|
408
363
|
function getAndRemoveLeadingComment(doc) {
|
|
409
|
-
|
|
364
|
+
const isTokenWithLeadingComment = typeof doc !== "string" && "leadingComments" in doc;
|
|
410
365
|
if (!isTokenWithLeadingComment) {
|
|
411
366
|
return [];
|
|
412
367
|
}
|
|
413
|
-
|
|
368
|
+
const leadingComments = getTokenLeadingComments(doc);
|
|
414
369
|
delete doc.leadingComments;
|
|
415
370
|
return leadingComments;
|
|
416
371
|
}
|
|
417
|
-
function putIntoBraces(argument, separator, LBrace, RBrace) {
|
|
418
|
-
|
|
419
|
-
|
|
372
|
+
export function putIntoBraces(argument, separator, LBrace, RBrace) {
|
|
373
|
+
const rightBraceLeadingComments = getAndRemoveLeadingComment(RBrace);
|
|
374
|
+
const lastBreakLine =
|
|
420
375
|
// check if last element of the array is a line
|
|
421
376
|
rightBraceLeadingComments.length !== 0 &&
|
|
422
377
|
rightBraceLeadingComments[rightBraceLeadingComments.length - 1] === hardline
|
|
423
378
|
? rightBraceLeadingComments.pop()
|
|
424
379
|
: separator;
|
|
425
|
-
|
|
426
|
-
if (
|
|
380
|
+
let contentInsideBraces;
|
|
381
|
+
if (isEmptyDoc(argument)) {
|
|
427
382
|
if (rightBraceLeadingComments.length === 0) {
|
|
428
|
-
return
|
|
383
|
+
return concat([LBrace, RBrace]);
|
|
429
384
|
}
|
|
430
|
-
contentInsideBraces =
|
|
385
|
+
contentInsideBraces = [separator, ...rightBraceLeadingComments];
|
|
431
386
|
}
|
|
432
387
|
else if (rightBraceLeadingComments.length !== 0) {
|
|
433
|
-
contentInsideBraces =
|
|
388
|
+
contentInsideBraces = [
|
|
434
389
|
separator,
|
|
435
390
|
argument,
|
|
436
|
-
separator
|
|
437
|
-
|
|
391
|
+
separator,
|
|
392
|
+
...rightBraceLeadingComments
|
|
393
|
+
];
|
|
438
394
|
}
|
|
439
395
|
else {
|
|
440
396
|
contentInsideBraces = [separator, argument];
|
|
441
397
|
}
|
|
442
|
-
return
|
|
398
|
+
return group(rejectAndConcat([
|
|
443
399
|
LBrace,
|
|
444
|
-
indent(
|
|
400
|
+
indent(concat(contentInsideBraces)),
|
|
445
401
|
lastBreakLine,
|
|
446
402
|
RBrace
|
|
447
403
|
]));
|
|
448
404
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
var levelPrecedence;
|
|
454
|
-
var level = [];
|
|
405
|
+
export function binary(nodes, tokens, isRoot = false) {
|
|
406
|
+
let levelOperator;
|
|
407
|
+
let levelPrecedence;
|
|
408
|
+
let level = [];
|
|
455
409
|
while (tokens.length) {
|
|
456
|
-
|
|
457
|
-
|
|
410
|
+
const nextOperator = getOperator(tokens);
|
|
411
|
+
const nextPrecedence = getOperatorPrecedence(nextOperator);
|
|
458
412
|
if (levelPrecedence === undefined || nextPrecedence === levelPrecedence) {
|
|
459
|
-
|
|
413
|
+
const tokenLength = ["<<", ">>", ">>>"].includes(nextOperator)
|
|
460
414
|
? nextOperator.length
|
|
461
415
|
: 1;
|
|
462
|
-
|
|
416
|
+
const operator = concat(tokens.splice(0, tokenLength));
|
|
463
417
|
if (levelOperator !== undefined &&
|
|
464
418
|
needsParentheses(levelOperator, nextOperator)) {
|
|
465
419
|
level.push(nodes.shift());
|
|
466
420
|
level = [
|
|
467
|
-
|
|
421
|
+
concat(["(", group(indent(join(line, level))), ") ", operator])
|
|
468
422
|
];
|
|
469
423
|
}
|
|
470
424
|
else {
|
|
471
|
-
level.push(
|
|
425
|
+
level.push(join(" ", [nodes.shift(), operator]));
|
|
472
426
|
}
|
|
473
427
|
levelOperator = nextOperator;
|
|
474
428
|
levelPrecedence = nextPrecedence;
|
|
@@ -476,43 +430,48 @@ function binary(nodes, tokens, isRoot) {
|
|
|
476
430
|
else if (nextPrecedence < levelPrecedence) {
|
|
477
431
|
level.push(nodes.shift());
|
|
478
432
|
if (isRoot) {
|
|
479
|
-
|
|
433
|
+
const content = group(indent(join(line, level)));
|
|
480
434
|
nodes.unshift(levelOperator !== undefined &&
|
|
481
435
|
needsParentheses(levelOperator, nextOperator)
|
|
482
|
-
?
|
|
483
|
-
:
|
|
436
|
+
? concat(["(", content, ")"])
|
|
437
|
+
: content);
|
|
484
438
|
level = [];
|
|
485
439
|
levelOperator = undefined;
|
|
486
440
|
levelPrecedence = undefined;
|
|
487
441
|
}
|
|
488
442
|
else {
|
|
489
|
-
return
|
|
443
|
+
return group(join(line, level));
|
|
490
444
|
}
|
|
491
445
|
}
|
|
492
446
|
else {
|
|
493
|
-
|
|
447
|
+
const content = indent(binary(nodes, tokens));
|
|
494
448
|
nodes.unshift(levelOperator !== undefined &&
|
|
495
449
|
needsParentheses(nextOperator, levelOperator)
|
|
496
|
-
?
|
|
497
|
-
:
|
|
450
|
+
? concat(["(", content, ")"])
|
|
451
|
+
: content);
|
|
498
452
|
}
|
|
499
453
|
}
|
|
500
454
|
level.push(nodes.shift());
|
|
501
|
-
|
|
502
|
-
return levelOperator === "="
|
|
455
|
+
const lineGroupId = Symbol("line");
|
|
456
|
+
return group(levelOperator === "="
|
|
457
|
+
? [
|
|
458
|
+
level[0],
|
|
459
|
+
indent(group(line, { id: lineGroupId })),
|
|
460
|
+
indentIfBreak(level[1], { groupId: lineGroupId })
|
|
461
|
+
]
|
|
462
|
+
: join(line, level));
|
|
503
463
|
}
|
|
504
|
-
exports.binary = binary;
|
|
505
464
|
function getOperator(tokens) {
|
|
506
465
|
if (!tokens.length) {
|
|
507
466
|
return "";
|
|
508
467
|
}
|
|
509
|
-
|
|
468
|
+
const [{ image, startOffset }] = tokens;
|
|
510
469
|
if (!["<", ">"].includes(image)) {
|
|
511
470
|
return image;
|
|
512
471
|
}
|
|
513
|
-
|
|
514
|
-
for (
|
|
515
|
-
|
|
472
|
+
let repeatedTokenCount = 1;
|
|
473
|
+
for (let i = 1; i < Math.min(3, tokens.length); i++) {
|
|
474
|
+
const token = tokens[i];
|
|
516
475
|
if (token.image !== image || token.startOffset !== startOffset + i) {
|
|
517
476
|
break;
|
|
518
477
|
}
|
|
@@ -531,7 +490,7 @@ function getOperator(tokens) {
|
|
|
531
490
|
return ">>>";
|
|
532
491
|
}
|
|
533
492
|
}
|
|
534
|
-
|
|
493
|
+
const PRECEDENCES_BY_OPERATOR = new Map([
|
|
535
494
|
["||"],
|
|
536
495
|
["&&"],
|
|
537
496
|
["|"],
|
|
@@ -542,7 +501,7 @@ var PRECEDENCES_BY_OPERATOR = new Map([
|
|
|
542
501
|
["<<", ">>", ">>>"],
|
|
543
502
|
["+", "-"],
|
|
544
503
|
["*", "/", "%"]
|
|
545
|
-
].flatMap(
|
|
504
|
+
].flatMap((operators, index) => operators.map(operator => [operator, index])));
|
|
546
505
|
function getOperatorPrecedence(operator) {
|
|
547
506
|
var _a;
|
|
548
507
|
return (_a = PRECEDENCES_BY_OPERATOR.get(operator)) !== null && _a !== void 0 ? _a : -1;
|
|
@@ -552,22 +511,21 @@ function needsParentheses(operator, parentOperator) {
|
|
|
552
511
|
(["|", "^", "&", "<<", ">>", ">>>"].includes(parentOperator) &&
|
|
553
512
|
getOperatorPrecedence(operator) >
|
|
554
513
|
getOperatorPrecedence(parentOperator)) ||
|
|
555
|
-
[operator, parentOperator].every(
|
|
556
|
-
[operator, parentOperator].every(
|
|
514
|
+
[operator, parentOperator].every(o => ["==", "!="].includes(o)) ||
|
|
515
|
+
[operator, parentOperator].every(o => ["<<", ">>", ">>>"].includes(o)) ||
|
|
557
516
|
(operator === "*" && parentOperator === "/") ||
|
|
558
517
|
(operator === "/" && parentOperator === "*") ||
|
|
559
518
|
(operator === "%" && ["+", "-", "*", "/"].includes(parentOperator)) ||
|
|
560
519
|
(["*", "/"].includes(operator) && parentOperator === "%"));
|
|
561
520
|
}
|
|
562
|
-
function isStatementEmptyStatement(statement) {
|
|
521
|
+
export function isStatementEmptyStatement(statement) {
|
|
563
522
|
return (statement === ";" || (Array.isArray(statement) && statement[0] === ";"));
|
|
564
523
|
}
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
var nonStaticImports = [];
|
|
524
|
+
export function sortImports(imports) {
|
|
525
|
+
const staticImports = [];
|
|
526
|
+
const nonStaticImports = [];
|
|
569
527
|
if (imports !== undefined) {
|
|
570
|
-
for (
|
|
528
|
+
for (let i = 0; i < imports.length; i++) {
|
|
571
529
|
if (imports[i].children.Static !== undefined) {
|
|
572
530
|
staticImports.push(imports[i]);
|
|
573
531
|
}
|
|
@@ -576,23 +534,20 @@ function sortImports(imports) {
|
|
|
576
534
|
}
|
|
577
535
|
}
|
|
578
536
|
// TODO: Could be optimized as we could expect that the array is already almost sorted
|
|
579
|
-
|
|
580
|
-
return compareFqn(first.children.packageOrTypeName[0], second.children.packageOrTypeName[0]);
|
|
581
|
-
};
|
|
537
|
+
const comparator = (first, second) => compareFqn(first.children.packageOrTypeName[0], second.children.packageOrTypeName[0]);
|
|
582
538
|
staticImports.sort(comparator);
|
|
583
539
|
nonStaticImports.sort(comparator);
|
|
584
540
|
}
|
|
585
541
|
return {
|
|
586
|
-
staticImports
|
|
587
|
-
nonStaticImports
|
|
542
|
+
staticImports,
|
|
543
|
+
nonStaticImports
|
|
588
544
|
};
|
|
589
545
|
}
|
|
590
|
-
exports.sortImports = sortImports;
|
|
591
546
|
function compareFqn(packageOrTypeNameFirst, packageOrTypeNameSecond) {
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
for (
|
|
547
|
+
const identifiersFirst = packageOrTypeNameFirst.children.Identifier;
|
|
548
|
+
const identifiersSecond = packageOrTypeNameSecond.children.Identifier;
|
|
549
|
+
const minParts = Math.min(identifiersFirst.length, identifiersSecond.length);
|
|
550
|
+
for (let i = 0; i < minParts; i++) {
|
|
596
551
|
if (identifiersFirst[i].image < identifiersSecond[i].image) {
|
|
597
552
|
return -1;
|
|
598
553
|
}
|
|
@@ -608,12 +563,12 @@ function compareFqn(packageOrTypeNameFirst, packageOrTypeNameSecond) {
|
|
|
608
563
|
}
|
|
609
564
|
return 0;
|
|
610
565
|
}
|
|
611
|
-
function isUniqueMethodInvocation(primarySuffixes) {
|
|
566
|
+
export function isUniqueMethodInvocation(primarySuffixes) {
|
|
612
567
|
if (primarySuffixes === undefined) {
|
|
613
568
|
return 0;
|
|
614
569
|
}
|
|
615
|
-
|
|
616
|
-
primarySuffixes.forEach(
|
|
570
|
+
let count = 0;
|
|
571
|
+
primarySuffixes.forEach(primarySuffix => {
|
|
617
572
|
if (primarySuffix.children.methodInvocationSuffix !== undefined) {
|
|
618
573
|
count++;
|
|
619
574
|
if (count > 1) {
|
|
@@ -623,18 +578,15 @@ function isUniqueMethodInvocation(primarySuffixes) {
|
|
|
623
578
|
});
|
|
624
579
|
return count;
|
|
625
580
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
var list = _a.list, extraComma = _a.extraComma, LCurly = _a.LCurly, RCurly = _a.RCurly, trailingComma = _a.trailingComma;
|
|
629
|
-
var optionalComma;
|
|
581
|
+
export function printArrayList({ list, extraComma, LCurly, RCurly, trailingComma }) {
|
|
582
|
+
let optionalComma;
|
|
630
583
|
if (trailingComma !== "none" && list !== "") {
|
|
631
584
|
optionalComma = extraComma
|
|
632
|
-
?
|
|
633
|
-
:
|
|
585
|
+
? ifBreak(extraComma[0], Object.assign(Object.assign({}, extraComma[0]), { image: "" }))
|
|
586
|
+
: ifBreak(",", "");
|
|
634
587
|
}
|
|
635
588
|
else {
|
|
636
|
-
optionalComma = extraComma ?
|
|
589
|
+
optionalComma = extraComma ? Object.assign(Object.assign({}, extraComma[0]), { image: "" }) : "";
|
|
637
590
|
}
|
|
638
591
|
return putIntoBraces(rejectAndConcat([list, optionalComma]), line, LCurly, RCurly);
|
|
639
592
|
}
|
|
640
|
-
exports.printArrayList = printArrayList;
|