prettier-plugin-java 2.6.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.
@@ -1,21 +0,0 @@
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
- }
@@ -1,171 +0,0 @@
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
- }
@@ -1,73 +0,0 @@
1
- import { hasLeadingComments, hasTrailingComments } from "./comments-utils.js";
2
- export function handleCommentsBinaryExpression(ctx) {
3
- moveOperatorLeadingCommentsToNextExpression(ctx);
4
- moveExpressionTrailingCommentsToNextOperator(ctx);
5
- }
6
- function moveOperatorLeadingCommentsToNextExpression(ctx) {
7
- var _a;
8
- let unaryExpressionIndex = 1;
9
- (_a = ctx.BinaryOperator) === null || _a === void 0 ? void 0 : _a.forEach(binaryOperator => {
10
- if (hasLeadingComments(binaryOperator)) {
11
- while (ctx.unaryExpression[unaryExpressionIndex].location.startOffset <
12
- binaryOperator.endOffset) {
13
- unaryExpressionIndex++;
14
- }
15
- // Adapt the position of the operator and its leading comments
16
- const shiftUp = binaryOperator.leadingComments[0].startLine -
17
- 1 -
18
- binaryOperator.startLine;
19
- if (binaryOperator.startLine !==
20
- ctx.unaryExpression[unaryExpressionIndex].location.startLine) {
21
- binaryOperator.leadingComments.forEach(comment => {
22
- comment.startLine += 1;
23
- comment.endLine += 1;
24
- });
25
- }
26
- binaryOperator.startLine += shiftUp;
27
- binaryOperator.endLine += shiftUp;
28
- // Move binaryOperator's leading comments to the following
29
- // unaryExpression
30
- ctx.unaryExpression[unaryExpressionIndex].leadingComments =
31
- ctx.unaryExpression[unaryExpressionIndex].leadingComments || [];
32
- ctx.unaryExpression[unaryExpressionIndex].leadingComments.unshift(...binaryOperator.leadingComments);
33
- delete binaryOperator.leadingComments;
34
- }
35
- });
36
- }
37
- function moveExpressionTrailingCommentsToNextOperator(ctx) {
38
- const binaryOperators = ctx.BinaryOperator;
39
- let binaryOperatorIndex = 1;
40
- if (binaryOperators === null || binaryOperators === void 0 ? void 0 : binaryOperators.length) {
41
- ctx.unaryExpression.forEach(unaryExpression => {
42
- var _a;
43
- if (hasTrailingComments(unaryExpression)) {
44
- while (binaryOperatorIndex < binaryOperators.length &&
45
- unaryExpression.location.endOffset &&
46
- binaryOperators[binaryOperatorIndex].startOffset <
47
- unaryExpression.location.endOffset) {
48
- binaryOperatorIndex++;
49
- }
50
- const binaryOperator = binaryOperators[binaryOperatorIndex];
51
- // Adapt the position of the expression and its trailing comments
52
- const shiftUp = unaryExpression.trailingComments[0].startLine -
53
- 1 -
54
- unaryExpression.location.startLine;
55
- if (unaryExpression.location.startLine !== binaryOperator.startLine) {
56
- unaryExpression.trailingComments.forEach(comment => {
57
- comment.startLine += 1;
58
- comment.endLine += 1;
59
- });
60
- }
61
- unaryExpression.location.startLine += shiftUp;
62
- if (unaryExpression.location.endLine !== undefined) {
63
- unaryExpression.location.endLine += shiftUp;
64
- }
65
- // Move unaryExpression's trailing comments to the following
66
- // binaryOperator
67
- binaryOperator.trailingComments = (_a = binaryOperator.trailingComments) !== null && _a !== void 0 ? _a : [];
68
- binaryOperator.trailingComments.unshift(...unaryExpression.trailingComments);
69
- delete unaryExpression.trailingComments;
70
- }
71
- });
72
- }
73
- }