prettier-plugin-java 2.6.7 → 2.7.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.
Files changed (47) hide show
  1. package/dist/comments.d.ts +17 -0
  2. package/dist/comments.js +199 -0
  3. package/dist/index.d.ts +543 -0
  4. package/dist/index.js +26 -63
  5. package/dist/options.d.ts +23 -0
  6. package/dist/options.js +247 -239
  7. package/dist/parser.d.ts +9 -0
  8. package/dist/parser.js +24 -4
  9. package/dist/printer.d.ts +18 -0
  10. package/dist/printer.js +39 -5
  11. package/dist/printers/arrays.d.ts +9 -0
  12. package/dist/printers/arrays.js +8 -24
  13. package/dist/printers/blocks-and-statements.d.ts +117 -0
  14. package/dist/printers/blocks-and-statements.js +316 -412
  15. package/dist/printers/classes.d.ts +157 -0
  16. package/dist/printers/classes.js +422 -688
  17. package/dist/printers/expressions.d.ts +134 -0
  18. package/dist/printers/expressions.js +548 -560
  19. package/dist/printers/helpers.d.ts +71 -0
  20. package/dist/printers/helpers.js +233 -0
  21. package/dist/printers/index.d.ts +2 -0
  22. package/dist/printers/index.js +13 -0
  23. package/dist/printers/interfaces.d.ts +62 -0
  24. package/dist/printers/interfaces.js +146 -211
  25. package/dist/printers/lexical-structure.d.ts +14 -0
  26. package/dist/printers/lexical-structure.js +26 -28
  27. package/dist/printers/names.d.ts +12 -0
  28. package/dist/printers/names.js +11 -29
  29. package/dist/printers/packages-and-modules.d.ts +46 -0
  30. package/dist/printers/packages-and-modules.js +157 -159
  31. package/dist/printers/types-values-and-variables.d.ts +46 -0
  32. package/dist/printers/types-values-and-variables.js +86 -149
  33. package/package.json +5 -8
  34. package/dist/base-cst-printer.js +0 -55
  35. package/dist/cst-printer.js +0 -29
  36. package/dist/printers/comments/comments-utils.js +0 -21
  37. package/dist/printers/comments/format-comments.js +0 -171
  38. package/dist/printers/comments/handle-comments.js +0 -102
  39. package/dist/printers/prettier-builder.js +0 -45
  40. package/dist/printers/printer-utils.js +0 -598
  41. package/dist/types/utils.js +0 -20
  42. package/dist/utils/expressions-utils.js +0 -25
  43. package/dist/utils/index.js +0 -2
  44. package/dist/utils/isEmptyDoc.js +0 -4
  45. package/dist/utils/printArgumentListWithBraces.js +0 -37
  46. package/dist/utils/printSingleLambdaInvocation.js +0 -18
  47. package/index.d.ts +0 -4
@@ -1,55 +0,0 @@
1
- import { BaseJavaCstVisitor } from "java-parser";
2
- import { printNodeWithComments } from "./printers/comments/format-comments.js";
3
- export class BaseCstPrettierPrinter extends BaseJavaCstVisitor {
4
- constructor() {
5
- super();
6
- this.mapVisit = (elements, params) => {
7
- if (elements === undefined) {
8
- // TODO: can optimize this by returning an immutable empty array singleton.
9
- return [];
10
- }
11
- return elements.map(element => this.visit(element, params));
12
- };
13
- this.getSingle = (ctx) => {
14
- const ctxKeys = Object.keys(ctx);
15
- if (ctxKeys.length !== 1) {
16
- throw Error(`Expecting single key CST ctx but found: <${ctxKeys.length}> keys`);
17
- }
18
- const singleElementKey = ctxKeys[0];
19
- const singleElementValues = ctx[singleElementKey];
20
- if ((singleElementValues === null || singleElementValues === void 0 ? void 0 : singleElementValues.length) !== 1) {
21
- throw Error(`Expecting single item in CST ctx key but found: <${singleElementValues === null || singleElementValues === void 0 ? void 0 : singleElementValues.length}> items`);
22
- }
23
- return singleElementValues[0];
24
- };
25
- // @ts-ignore
26
- this.orgVisit = this.visit;
27
- this.visit = function (ctx, inParam) {
28
- if (ctx === undefined) {
29
- // empty Doc
30
- return "";
31
- }
32
- const node = Array.isArray(ctx) ? ctx[0] : ctx;
33
- if (node.ignore) {
34
- try {
35
- const startOffset = node.leadingComments !== undefined
36
- ? node.leadingComments[0].startOffset
37
- : node.location.startOffset;
38
- const endOffset = (node.trailingComments !== undefined
39
- ? node.trailingComments[node.trailingComments.length - 1].endOffset
40
- : node.location.endOffset);
41
- return this.prettierOptions.originalText.substring(startOffset, endOffset + 1);
42
- }
43
- catch (e) {
44
- throw Error(e +
45
- "\nThere might be a problem with prettier-ignore, please report an issue on https://github.com/jhipster/prettier-java/issues");
46
- }
47
- }
48
- return printNodeWithComments(node, this.orgVisit.call(this, node, inParam));
49
- };
50
- this.visitSingle = function (ctx, params) {
51
- const singleElement = this.getSingle(ctx);
52
- return this.visit(singleElement, params);
53
- };
54
- }
55
- }
@@ -1,29 +0,0 @@
1
- import { BaseCstPrettierPrinter } from "./base-cst-printer.js";
2
- import { ArraysPrettierVisitor } from "./printers/arrays.js";
3
- import { BlocksAndStatementPrettierVisitor } from "./printers/blocks-and-statements.js";
4
- import { ClassesPrettierVisitor } from "./printers/classes.js";
5
- import { ExpressionsPrettierVisitor } from "./printers/expressions.js";
6
- import { InterfacesPrettierVisitor } from "./printers/interfaces.js";
7
- import { LexicalStructurePrettierVisitor } from "./printers/lexical-structure.js";
8
- import { NamesPrettierVisitor } from "./printers/names.js";
9
- import { TypesValuesAndVariablesPrettierVisitor } from "./printers/types-values-and-variables.js";
10
- import { PackagesAndModulesPrettierVisitor } from "./printers/packages-and-modules.js";
11
- // Mixins for the win
12
- mixInMethods(ArraysPrettierVisitor, BlocksAndStatementPrettierVisitor, ClassesPrettierVisitor, ExpressionsPrettierVisitor, InterfacesPrettierVisitor, LexicalStructurePrettierVisitor, NamesPrettierVisitor, TypesValuesAndVariablesPrettierVisitor, PackagesAndModulesPrettierVisitor);
13
- function mixInMethods(...classesToMix) {
14
- classesToMix.forEach(from => {
15
- const fromMethodsNames = Object.getOwnPropertyNames(from.prototype);
16
- const fromPureMethodsName = fromMethodsNames.filter(methodName => methodName !== "constructor");
17
- fromPureMethodsName.forEach(methodName => {
18
- // @ts-ignore
19
- BaseCstPrettierPrinter.prototype[methodName] = from.prototype[methodName];
20
- });
21
- });
22
- }
23
- const prettyPrinter = new BaseCstPrettierPrinter();
24
- // TODO: do we need the "path" and "print" arguments passed by prettier
25
- // see https://github.com/prettier/prettier/issues/5747
26
- export function createPrettierDoc(cstNode, options) {
27
- prettyPrinter.prettierOptions = options;
28
- return prettyPrinter.visit(cstNode);
29
- }
@@ -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,102 +0,0 @@
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
- }
@@ -1,45 +0,0 @@
1
- import { builders } from "prettier/doc";
2
- import * as formatComments from "./comments/format-comments.js";
3
- const processComments = formatComments.processComments;
4
- /*
5
- * ------------------------------------------------------------------
6
- * Wraps the Prettier builder functions to print tokens with comments
7
- * ------------------------------------------------------------------
8
- */
9
- export function concat(docs) {
10
- const concatenation = processComments(docs);
11
- if (!Array.isArray(docs)) {
12
- return "";
13
- }
14
- return concatenation;
15
- }
16
- export function join(sep, docs) {
17
- return builders.join(processComments(sep), processComments(docs));
18
- }
19
- export function group(docs, opts) {
20
- const group = builders.group(processComments(docs), opts);
21
- return group.contents === undefined ? "" : group;
22
- }
23
- export function fill(docs) {
24
- return builders.fill(processComments(docs));
25
- }
26
- export function indent(doc) {
27
- const processedDoc = processComments(doc);
28
- if (processedDoc.length === 0) {
29
- return "";
30
- }
31
- return builders.indent(processedDoc);
32
- }
33
- export function dedent(doc) {
34
- const processedDoc = processComments(doc);
35
- if (processedDoc.length === 0) {
36
- return "";
37
- }
38
- return builders.dedent(processComments(doc));
39
- }
40
- export function ifBreak(breakContents, flatContents) {
41
- return builders.ifBreak(processComments(breakContents), processComments(flatContents));
42
- }
43
- export function indentIfBreak(contents, opts) {
44
- return builders.indentIfBreak(processComments(contents), opts);
45
- }