Skip to content

Commit 1d741a2

Browse files
chore: update to prettier 3 (#558)
* chore: upgrade to prettier 3 * chore: remove unecessary prettier types package * chore: upgrade to prettier 3.0.0 * refactor: remove require
1 parent 28084f4 commit 1d741a2

8 files changed

Lines changed: 54 additions & 76 deletions

File tree

packages/prettier-plugin-java/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"java-parser": "2.0.4",
1313
"lodash": "4.17.21",
14-
"prettier": "2.8.4"
14+
"prettier": "3.0.0"
1515
},
1616
"scripts": {
1717
"test": "yarn run test:unit && yarn run test:e2e-core",
@@ -37,7 +37,6 @@
3737
"@types/klaw-sync": "6.0.1",
3838
"@types/lodash": "4.14.190",
3939
"@types/node": "18.11.9",
40-
"@types/prettier": "2.7.1",
4140
"@types/sinon": "10.0.13",
4241
"ts-node": "10.9.1",
4342
"typescript": "4.9.3"

packages/prettier-plugin-java/src/parser.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
const javaParser = require("java-parser");
44

55
function parse(text, parsers, opts) {
6-
const cst = javaParser.parse(text, opts.entrypoint);
7-
return cst;
6+
return javaParser.parse(text, opts.entrypoint);
87
}
98

109
module.exports = parse;

packages/prettier-plugin-java/src/printer.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,6 @@ const { createPrettierDoc } = require("./cst-printer");
44
// eslint-disable-next-line no-unused-vars
55
function genericPrint(path, options, print) {
66
const node = path.getValue();
7-
// console.log(node);
8-
// if (node.comments) {
9-
// console.log(node.type, node.comments);
10-
// }
11-
12-
// node["comments"] = [
13-
// {
14-
// ast_type: "comment",
15-
// value: "// a",
16-
// leading: false,
17-
// trailing: true,
18-
// printed: false
19-
// },
20-
// {
21-
// ast_type: "comment",
22-
// value: "// b",
23-
// leading: true,
24-
// trailing: false,
25-
// printed: false
26-
// }
27-
// ];
287
return createPrettierDoc(node, options);
298
}
309

packages/prettier-plugin-java/src/printers/classes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,19 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
126126
const optionalClassPermits = this.visit(ctx.classPermits);
127127
const body = this.visit(ctx.classBody, { isNormalClassDeclaration: true });
128128

129-
let superClassesPart = "";
129+
let superClassesPart: Doc = "";
130130
if (optionalSuperClasses) {
131131
superClassesPart = indent(rejectAndConcat([line, optionalSuperClasses]));
132132
}
133133

134-
let superInterfacesPart = "";
134+
let superInterfacesPart: Doc = "";
135135
if (optionalSuperInterfaces) {
136136
superInterfacesPart = indent(
137137
rejectAndConcat([line, optionalSuperInterfaces])
138138
);
139139
}
140140

141-
let classPermits = "";
141+
let classPermits: Doc = "";
142142
if (optionalClassPermits) {
143143
classPermits = indent(rejectAndConcat([line, optionalClassPermits]));
144144
}
@@ -912,7 +912,7 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
912912

913913
const recordHeader = this.visit(ctx.recordHeader);
914914

915-
let superInterfacesPart = "";
915+
let superInterfacesPart: Doc = "";
916916
const optionalSuperInterfaces = this.visit(ctx.superinterfaces);
917917
if (optionalSuperInterfaces) {
918918
superInterfacesPart = indent(

packages/prettier-plugin-java/src/printers/interfaces.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { doc } from "prettier";
21
import { concat, group, indent } from "./prettier-builder";
32
import { printTokenWithComments } from "./comments/format-comments";
43
import {
@@ -41,7 +40,7 @@ import {
4140
IToken,
4241
NormalInterfaceDeclarationCtx
4342
} from "java-parser";
44-
import Doc = doc.builders.Doc;
43+
import Doc = builders.Doc;
4544

4645
const { line, softline, hardline } = builders;
4746

@@ -68,14 +67,14 @@ export class InterfacesPrettierVisitor extends BaseCstPrettierPrinter {
6867
const optionalInterfacePermits = this.visit(ctx.interfacePermits);
6968
const interfaceBody = this.visit(ctx.interfaceBody);
7069

71-
let extendsInterfacesPart = "";
70+
let extendsInterfacesPart: Doc = "";
7271
if (extendsInterfaces) {
7372
extendsInterfacesPart = indent(
7473
rejectAndConcat([softline, extendsInterfaces])
7574
);
7675
}
7776

78-
let interfacePermits = "";
77+
let interfacePermits: Doc = "";
7978
if (optionalInterfacePermits) {
8079
interfacePermits = indent(
8180
rejectAndConcat([softline, optionalInterfacePermits])
@@ -277,7 +276,7 @@ export class InterfacesPrettierVisitor extends BaseCstPrettierPrinter {
277276
annotation(ctx: AnnotationCtx) {
278277
const fqn = this.visit(ctx.typeName);
279278

280-
let annoArgs = "";
279+
let annoArgs: Doc = "";
281280
if (ctx.LBrace) {
282281
if (ctx.elementValuePairList) {
283282
annoArgs = putIntoBraces(
Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
"use strict";
22
import { IToken } from "java-parser";
3-
import { doc } from "prettier";
4-
import Doc = doc.builders.Doc;
5-
import Concat = doc.builders.Concat;
6-
import Fill = doc.builders.Fill;
7-
8-
const prettier = require("prettier").doc.builders;
3+
import { builders } from "prettier/doc";
4+
import Doc = builders.Doc;
95

106
const { processComments } = require("./comments/format-comments");
11-
127
/*
138
* ------------------------------------------------------------------
149
* Wraps the Prettier builder functions to print tokens with comments
@@ -26,50 +21,46 @@ export function concat(docs: (Doc | IToken)[]): Doc {
2621
}
2722

2823
export function join(sep: any, docs: (Doc | IToken)[]): Doc {
29-
const concatenation = prettier.join(
30-
processComments(sep),
31-
processComments(docs)
32-
);
33-
34-
return processEmptyDocs(concatenation);
24+
return builders.join(processComments(sep), processComments(docs));
3525
}
3626

37-
export function group(doc: Doc | IToken, opts?: any) {
38-
const group = prettier.group(processComments(doc), opts);
27+
export function group(docs: Doc | IToken | (Doc | IToken)[], opts?: any) {
28+
const group = builders.group(processComments(docs), opts);
3929
return group.contents === undefined ? "" : group;
4030
}
4131

4232
export function fill(docs: (Doc | IToken)[]) {
43-
const fill = prettier.fill(processComments(docs));
44-
45-
return processEmptyDocs(fill);
33+
return builders.fill(processComments(docs));
4634
}
4735

4836
export function indent(doc: Doc | IToken) {
49-
const indentedDoc = prettier.indent(processComments(doc));
50-
return indentedDoc.contents.length === 0 ? "" : indentedDoc;
37+
const processedDoc = processComments(doc);
38+
if (processedDoc.length === 0) {
39+
return "";
40+
}
41+
42+
return builders.indent(processedDoc);
5143
}
5244

5345
export function dedent(doc: Doc | IToken) {
54-
const indentedDoc = prettier.dedent(processComments(doc));
55-
return indentedDoc.contents.length === 0 ? "" : indentedDoc;
46+
const processedDoc = processComments(doc);
47+
if (processedDoc.length === 0) {
48+
return "";
49+
}
50+
51+
return builders.dedent(processComments(doc));
5652
}
5753

5854
export function ifBreak(
5955
breakContents: Doc | IToken,
6056
flatContents: Doc | IToken
6157
) {
62-
return prettier.ifBreak(
58+
return builders.ifBreak(
6359
processComments(breakContents),
6460
processComments(flatContents)
6561
);
6662
}
6763

6864
export function indentIfBreak(contents: Doc | IToken, opts?: any) {
69-
return prettier.indentIfBreak(processComments(contents), opts);
65+
return builders.indentIfBreak(processComments(contents), opts);
7066
}
71-
72-
// TODO: remove this once prettier 3.0 is released
73-
const processEmptyDocs = (doc: Fill | Concat): Doc => {
74-
return doc.parts?.length === 0 ? "" : doc;
75-
};

packages/prettier-plugin-java/src/printers/printer-utils.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
} from "./comments/format-comments";
3535

3636
import { concat, group, ifBreak, join } from "./prettier-builder";
37+
import Indent = builders.Indent;
38+
import IfBreak = builders.IfBreak;
3739

3840
const { indent, hardline, line } = builders;
3941

@@ -313,7 +315,7 @@ export function isExplicitLambdaParameter(ctx: LambdaParametersWithBracesCtx) {
313315

314316
export function getBlankLinesSeparator(
315317
ctx: CstNode[] | undefined,
316-
separator: builders.Line | builders.Concat = hardline
318+
separator: builders.Line | builders.Hardline = hardline
317319
): Doc[] {
318320
if (ctx === undefined) {
319321
return [];
@@ -535,20 +537,34 @@ export function getInterfaceBodyDeclarationsSeparator(
535537
);
536538
}
537539

540+
function getAndRemoveLeadingComment(
541+
doc: IToken | builders.Indent | builders.IfBreak | string
542+
) {
543+
const isTokenWithLeadingComment =
544+
typeof doc !== "string" && "leadingComments" in doc;
545+
if (!isTokenWithLeadingComment) {
546+
return [];
547+
}
548+
549+
const leadingComments = getTokenLeadingComments(doc);
550+
delete doc.leadingComments;
551+
552+
return leadingComments;
553+
}
554+
538555
export function putIntoBraces(
539556
argument: Doc,
540557
separator: Doc,
541558
LBrace: IToken,
542-
RBrace: IToken
559+
RBrace: IToken | Indent | IfBreak | string
543560
) {
544-
const rightBraceLeadingComments = getTokenLeadingComments(RBrace);
561+
const rightBraceLeadingComments = getAndRemoveLeadingComment(RBrace);
545562
const lastBreakLine =
546563
// check if last element of the array is a line
547564
rightBraceLeadingComments.length !== 0 &&
548565
rightBraceLeadingComments[rightBraceLeadingComments.length - 1] === hardline
549566
? rightBraceLeadingComments.pop()
550567
: separator;
551-
delete RBrace.leadingComments;
552568

553569
let contentInsideBraces;
554570

yarn.lock

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,11 +2543,6 @@
25432543
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
25442544
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
25452545

2546-
"@types/prettier@2.7.1":
2547-
version "2.7.1"
2548-
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.1.tgz#dfd20e2dc35f027cdd6c1908e80a5ddc7499670e"
2549-
integrity sha512-ri0UmynRRvZiiUJdiz38MmIblKK+oH30MztdBVR95dv/Ubw6neWSb8u1XpRb72L4qsZOhz+L+z9JD40SJmfWow==
2550-
25512546
"@types/sinon@10.0.13":
25522547
version "10.0.13"
25532548
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-10.0.13.tgz#60a7a87a70d9372d0b7b38cc03e825f46981fb83"
@@ -7560,10 +7555,10 @@ prettier@2.8.0:
75607555
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.0.tgz#c7df58393c9ba77d6fba3921ae01faf994fb9dc9"
75617556
integrity sha512-9Lmg8hTFZKG0Asr/kW9Bp8tJjRVluO8EJQVfY2T7FMw9T5jy4I/Uvx0Rca/XWf50QQ1/SS48+6IJWnrb+2yemA==
75627557

7563-
prettier@2.8.4:
7564-
version "2.8.4"
7565-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.4.tgz#34dd2595629bfbb79d344ac4a91ff948694463c3"
7566-
integrity sha512-vIS4Rlc2FNh0BySk3Wkd6xmwxB0FpOndW5fisM5H8hsZSxU2VWVB5CWIkIjWvrHjIhxk2g3bfMKM87zNTrZddw==
7558+
prettier@3.0.0:
7559+
version "3.0.0"
7560+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.0.tgz#e7b19f691245a21d618c68bc54dc06122f6105ae"
7561+
integrity sha512-zBf5eHpwHOGPC47h0zrPyNn+eAEIdEzfywMoYn2XPi0P44Zp0tSq64rq0xAREh4auw2cJZHo9QUob+NqCQky4g==
75677562

75687563
pretty-format@^29.0.0, pretty-format@^29.3.1:
75697564
version "29.3.1"

0 commit comments

Comments
 (0)