prettier-plugin-java 2.6.3 → 2.6.5

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.
@@ -0,0 +1,21 @@
1
+ export function hasLeadingComments(token) {
2
+ return token.leadingComments !== undefined;
3
+ }
4
+ export function hasTrailingComments(token) {
5
+ return token.trailingComments !== undefined;
6
+ }
7
+ export function hasLeadingLineComments(token) {
8
+ return (token.leadingComments !== undefined &&
9
+ token.leadingComments.length !== 0 &&
10
+ token.leadingComments[token.leadingComments.length - 1].tokenType.name ===
11
+ "LineComment");
12
+ }
13
+ export function hasTrailingLineComments(token) {
14
+ return (token.trailingComments !== undefined &&
15
+ token.trailingComments.length !== 0 &&
16
+ token.trailingComments[token.trailingComments.length - 1].tokenType.name ===
17
+ "LineComment");
18
+ }
19
+ export function hasComments(token) {
20
+ return hasLeadingComments(token) || hasTrailingComments(token);
21
+ }
@@ -0,0 +1,171 @@
1
+ import { builders } from "prettier/doc";
2
+ import { isCstElementOrUndefinedIToken } from "../../types/utils.js";
3
+ import { doc } from "prettier";
4
+ import isEmptyDoc from "../../utils/isEmptyDoc.js";
5
+ const { hardline, lineSuffix, breakParent, literalline } = builders;
6
+ /**
7
+ * Takes a token and return a doc with:
8
+ * - concatenated leading comments
9
+ * - the token image
10
+ * - concatenated trailing comments
11
+ *
12
+ * @param {IToken} token
13
+ * @return a doc with the token and its comments
14
+ */
15
+ export function printTokenWithComments(token) {
16
+ return printWithComments(token, token.image.includes("\n")
17
+ ? doc.utils.replaceEndOfLine(token.image)
18
+ : token.image, getTokenLeadingComments, getTokenTrailingComments);
19
+ }
20
+ /**
21
+ * Takes a node and return a doc with:
22
+ * - concatenated leading comments
23
+ * - the node doc value
24
+ * - concatenated trailing comments
25
+ *
26
+ * @param {CstNode} node
27
+ * @param {Doc} value - the converted node value
28
+ * @return a doc with the token and its comments
29
+ */
30
+ export function printNodeWithComments(node, value) {
31
+ return printWithComments(node, value, getNodeLeadingComments, getNodeTrailingComments);
32
+ }
33
+ function printWithComments(nodeOrToken, value, getLeadingComments, getTrailingComments) {
34
+ const leadingComments = getLeadingComments(nodeOrToken);
35
+ const trailingComments = getTrailingComments(nodeOrToken, value);
36
+ return leadingComments.length === 0 && trailingComments.length === 0
37
+ ? value
38
+ : [...leadingComments, value, ...trailingComments];
39
+ }
40
+ /**
41
+ * @param {IToken} token
42
+ * @return an array containing processed leading comments and separators
43
+ */
44
+ export function getTokenLeadingComments(token) {
45
+ return getLeadingComments(token, token);
46
+ }
47
+ /**
48
+ * @param {CstNode} node
49
+ * @return an array containing processed leading comments and separators
50
+ */
51
+ function getNodeLeadingComments(node) {
52
+ return getLeadingComments(node, node.location);
53
+ }
54
+ function getLeadingComments(nodeOrToken, location) {
55
+ const arr = [];
56
+ if (nodeOrToken.leadingComments !== undefined) {
57
+ let previousEndLine = nodeOrToken.leadingComments[0].endLine;
58
+ let step;
59
+ arr.push(formatComment(nodeOrToken.leadingComments[0]));
60
+ for (let i = 1; i < nodeOrToken.leadingComments.length; i++) {
61
+ step = nodeOrToken.leadingComments[i].startLine - previousEndLine;
62
+ if (step === 1 ||
63
+ nodeOrToken.leadingComments[i].startOffset > location.startOffset) {
64
+ arr.push(hardline);
65
+ }
66
+ else if (step > 1) {
67
+ arr.push(hardline, hardline);
68
+ }
69
+ arr.push(formatComment(nodeOrToken.leadingComments[i]));
70
+ previousEndLine = nodeOrToken.leadingComments[i].endLine;
71
+ }
72
+ step = location.startLine - previousEndLine;
73
+ if (step === 1 ||
74
+ nodeOrToken.leadingComments[nodeOrToken.leadingComments.length - 1]
75
+ .startOffset > location.startOffset) {
76
+ arr.push(hardline);
77
+ }
78
+ else if (step > 1) {
79
+ arr.push(hardline, hardline);
80
+ }
81
+ }
82
+ return arr;
83
+ }
84
+ /**
85
+ * @param {IToken} token
86
+ * @return an array containing processed trailing comments and separators
87
+ */
88
+ function getTokenTrailingComments(token) {
89
+ return getTrailingComments(token, token.image, token);
90
+ }
91
+ /**
92
+ * @param {CstNode} node
93
+ * @param {string} value
94
+ * @return an array containing processed trailing comments and separators
95
+ */
96
+ function getNodeTrailingComments(node, value) {
97
+ return getTrailingComments(node, value, node.location);
98
+ }
99
+ function getTrailingComments(nodeOrToken, value, location) {
100
+ const arr = [];
101
+ let previousEndLine = location.endLine;
102
+ if (nodeOrToken.trailingComments !== undefined) {
103
+ nodeOrToken.trailingComments.forEach((comment, idx) => {
104
+ let separator = "";
105
+ if (comment.startLine !== previousEndLine) {
106
+ arr.push(hardline);
107
+ }
108
+ else if (!isEmptyDoc(value) && idx === 0) {
109
+ separator = " ";
110
+ }
111
+ if (comment.tokenType.name === "LineComment") {
112
+ arr.push(lineSuffix([separator, formatComment(comment), breakParent]));
113
+ }
114
+ else {
115
+ arr.push(formatComment(comment));
116
+ }
117
+ previousEndLine = comment.endLine;
118
+ });
119
+ }
120
+ return arr;
121
+ }
122
+ function isJavaDoc(comment, lines) {
123
+ let isJavaDoc = true;
124
+ if (comment.tokenType.name === "TraditionalComment" && lines.length > 1) {
125
+ for (let i = 1; i < lines.length; i++) {
126
+ if (lines[i].trim().charAt(0) !== "*") {
127
+ isJavaDoc = false;
128
+ break;
129
+ }
130
+ }
131
+ }
132
+ else {
133
+ isJavaDoc = false;
134
+ }
135
+ return isJavaDoc;
136
+ }
137
+ function formatJavaDoc(lines) {
138
+ const res = [lines[0].trim()];
139
+ for (let i = 1; i < lines.length; i++) {
140
+ res.push(hardline);
141
+ res.push(" " + lines[i].trim());
142
+ }
143
+ return res;
144
+ }
145
+ function formatComment(comment) {
146
+ const res = [];
147
+ const lines = comment.image.split("\n");
148
+ if (isJavaDoc(comment, lines)) {
149
+ return formatJavaDoc(lines);
150
+ }
151
+ lines.forEach(line => {
152
+ res.push(line);
153
+ res.push(literalline);
154
+ });
155
+ res.pop();
156
+ return res;
157
+ }
158
+ export function processComments(docs) {
159
+ if (!Array.isArray(docs)) {
160
+ if (isCstElementOrUndefinedIToken(docs)) {
161
+ return printTokenWithComments(docs);
162
+ }
163
+ return docs;
164
+ }
165
+ return docs.map(elt => {
166
+ if (isCstElementOrUndefinedIToken(elt)) {
167
+ return printTokenWithComments(elt);
168
+ }
169
+ return elt;
170
+ });
171
+ }
@@ -0,0 +1,102 @@
1
+ import { hasLeadingComments, hasTrailingComments } from "./comments-utils.js";
2
+ export function handleCommentsBinaryExpression(ctx) {
3
+ moveOperatorLeadingCommentsToNextExpression(ctx);
4
+ moveExpressionTrailingCommentsToNextOperator(ctx);
5
+ }
6
+ export function handleCommentsParameters(lBrace, parameters, rBrace) {
7
+ var _a, _b, _c;
8
+ const lBraceTrailingComments = lBrace.trailingComments;
9
+ const firstParameter = parameters.at(0);
10
+ if (lBraceTrailingComments && firstParameter) {
11
+ delete lBrace.trailingComments;
12
+ firstParameter.leadingComments = [
13
+ ...lBraceTrailingComments,
14
+ ...((_a = firstParameter.leadingComments) !== null && _a !== void 0 ? _a : [])
15
+ ];
16
+ }
17
+ const lastParameter = parameters.at(-1);
18
+ const rBraceLeadingComments = rBrace.leadingComments;
19
+ if (rBraceLeadingComments) {
20
+ delete rBrace.leadingComments;
21
+ if (lastParameter) {
22
+ lastParameter.trailingComments = [
23
+ ...((_b = lastParameter.trailingComments) !== null && _b !== void 0 ? _b : []),
24
+ ...rBraceLeadingComments
25
+ ];
26
+ }
27
+ else {
28
+ lBrace.trailingComments = [
29
+ ...((_c = lBrace.trailingComments) !== null && _c !== void 0 ? _c : []),
30
+ ...rBraceLeadingComments
31
+ ];
32
+ }
33
+ }
34
+ }
35
+ function moveOperatorLeadingCommentsToNextExpression(ctx) {
36
+ var _a;
37
+ let unaryExpressionIndex = 1;
38
+ (_a = ctx.BinaryOperator) === null || _a === void 0 ? void 0 : _a.forEach(binaryOperator => {
39
+ if (hasLeadingComments(binaryOperator)) {
40
+ while (ctx.unaryExpression[unaryExpressionIndex].location.startOffset <
41
+ binaryOperator.endOffset) {
42
+ unaryExpressionIndex++;
43
+ }
44
+ // Adapt the position of the operator and its leading comments
45
+ const shiftUp = binaryOperator.leadingComments[0].startLine -
46
+ 1 -
47
+ binaryOperator.startLine;
48
+ if (binaryOperator.startLine !==
49
+ ctx.unaryExpression[unaryExpressionIndex].location.startLine) {
50
+ binaryOperator.leadingComments.forEach(comment => {
51
+ comment.startLine += 1;
52
+ comment.endLine += 1;
53
+ });
54
+ }
55
+ binaryOperator.startLine += shiftUp;
56
+ binaryOperator.endLine += shiftUp;
57
+ // Move binaryOperator's leading comments to the following
58
+ // unaryExpression
59
+ ctx.unaryExpression[unaryExpressionIndex].leadingComments =
60
+ ctx.unaryExpression[unaryExpressionIndex].leadingComments || [];
61
+ ctx.unaryExpression[unaryExpressionIndex].leadingComments.unshift(...binaryOperator.leadingComments);
62
+ delete binaryOperator.leadingComments;
63
+ }
64
+ });
65
+ }
66
+ function moveExpressionTrailingCommentsToNextOperator(ctx) {
67
+ const binaryOperators = ctx.BinaryOperator;
68
+ let binaryOperatorIndex = 0;
69
+ if (binaryOperators === null || binaryOperators === void 0 ? void 0 : binaryOperators.length) {
70
+ ctx.unaryExpression.forEach(unaryExpression => {
71
+ var _a;
72
+ if (hasTrailingComments(unaryExpression)) {
73
+ while (binaryOperatorIndex < binaryOperators.length - 1 &&
74
+ unaryExpression.location.endOffset &&
75
+ binaryOperators[binaryOperatorIndex].startOffset <
76
+ unaryExpression.location.endOffset) {
77
+ binaryOperatorIndex++;
78
+ }
79
+ const binaryOperator = binaryOperators[binaryOperatorIndex];
80
+ // Adapt the position of the expression and its trailing comments
81
+ const shiftUp = unaryExpression.trailingComments[0].startLine -
82
+ 1 -
83
+ unaryExpression.location.startLine;
84
+ if (unaryExpression.location.startLine !== binaryOperator.startLine) {
85
+ unaryExpression.trailingComments.forEach(comment => {
86
+ comment.startLine += 1;
87
+ comment.endLine += 1;
88
+ });
89
+ }
90
+ unaryExpression.location.startLine += shiftUp;
91
+ if (unaryExpression.location.endLine !== undefined) {
92
+ unaryExpression.location.endLine += shiftUp;
93
+ }
94
+ // Move unaryExpression's trailing comments to the following
95
+ // binaryOperator
96
+ binaryOperator.trailingComments = (_a = binaryOperator.trailingComments) !== null && _a !== void 0 ? _a : [];
97
+ binaryOperator.trailingComments.unshift(...unaryExpression.trailingComments);
98
+ delete unaryExpression.trailingComments;
99
+ }
100
+ });
101
+ }
102
+ }