Skip to content

Commit ea90004

Browse files
committed
fix: improve throws formatting
1 parent 983a09d commit ea90004

29 files changed

+3718
-44
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import parse from "./parser.js";
2+
import print from "./printer.js";
3+
import options from "./options.js";
4+
const languages = [
5+
{
6+
name: "Java",
7+
parsers: ["java"],
8+
group: "Java",
9+
tmScope: "text.html.vue",
10+
aceMode: "html",
11+
codemirrorMode: "clike",
12+
codemirrorMimeType: "text/x-java",
13+
extensions: [".java"],
14+
linguistLanguageId: 181,
15+
vscodeLanguageIds: ["java"]
16+
}
17+
];
18+
function locStart( /* node */) {
19+
return -1;
20+
}
21+
function locEnd( /* node */) {
22+
return -1;
23+
}
24+
function hasPragma(text) {
25+
return /^\/\*\*[\n][\t\s]+\*\s@(prettier|format)[\n][\t\s]+\*\//.test(text);
26+
}
27+
const parsers = {
28+
java: {
29+
parse,
30+
astFormat: "java",
31+
locStart,
32+
locEnd,
33+
hasPragma
34+
}
35+
};
36+
function canAttachComment(node) {
37+
return node.ast_type && node.ast_type !== "comment";
38+
}
39+
function printComment(commentPath) {
40+
const comment = commentPath.getValue();
41+
switch (comment.ast_type) {
42+
case "comment":
43+
return comment.value;
44+
default:
45+
throw new Error("Not a comment: " + JSON.stringify(comment));
46+
}
47+
}
48+
function clean(ast, newObj) {
49+
delete newObj.lineno;
50+
delete newObj.col_offset;
51+
}
52+
const printers = {
53+
java: {
54+
print,
55+
// hasPrettierIgnore,
56+
printComment,
57+
canAttachComment,
58+
massageAstNode: clean
59+
}
60+
};
61+
export default {
62+
languages,
63+
printers,
64+
parsers,
65+
options
66+
};
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
export default {
2+
entrypoint: {
3+
type: "choice",
4+
category: "Global",
5+
default: "compilationUnit",
6+
// sed -nr 's/.*\.RULE\(([^,]+),.*/\1/p' $(ls path/to/java-parser/rules/folder/*)
7+
choices: [
8+
{ value: "arrayInitializer" },
9+
{ value: "variableInitializerList" },
10+
{ value: "block" },
11+
{ value: "blockStatements" },
12+
{ value: "blockStatement" },
13+
{ value: "localVariableDeclarationStatement" },
14+
{ value: "localVariableDeclaration" },
15+
{ value: "localVariableType" },
16+
{ value: "statement" },
17+
{ value: "statementWithoutTrailingSubstatement" },
18+
{ value: "emptyStatement" },
19+
{ value: "labeledStatement" },
20+
{ value: "expressionStatement" },
21+
{ value: "statementExpression" },
22+
{ value: "ifStatement" },
23+
{ value: "assertStatement" },
24+
{ value: "switchStatement" },
25+
{ value: "switchBlock" },
26+
{ value: "switchBlockStatementGroup" },
27+
{ value: "switchLabel" },
28+
{ value: "switchRule" },
29+
{ value: "caseConstant" },
30+
{ value: "whileStatement" },
31+
{ value: "doStatement" },
32+
{ value: "forStatement" },
33+
{ value: "basicForStatement" },
34+
{ value: "forInit" },
35+
{ value: "forUpdate" },
36+
{ value: "statementExpressionList" },
37+
{ value: "enhancedForStatement" },
38+
{ value: "breakStatement" },
39+
{ value: "continueStatement" },
40+
{ value: "returnStatement" },
41+
{ value: "throwStatement" },
42+
{ value: "synchronizedStatement" },
43+
{ value: "tryStatement" },
44+
{ value: "catches" },
45+
{ value: "catchClause" },
46+
{ value: "catchFormalParameter" },
47+
{ value: "catchType" },
48+
{ value: "finally" },
49+
{ value: "tryWithResourcesStatement" },
50+
{ value: "resourceSpecification" },
51+
{ value: "resourceList" },
52+
{ value: "resource" },
53+
{ value: "yieldStatement" },
54+
{ value: "variableAccess" },
55+
{ value: "classDeclaration" },
56+
{ value: "normalClassDeclaration" },
57+
{ value: "classModifier" },
58+
{ value: "typeParameters" },
59+
{ value: "typeParameterList" },
60+
{ value: "superclass" },
61+
{ value: "superinterfaces" },
62+
{ value: "interfaceTypeList" },
63+
{ value: "classPermits" },
64+
{ value: "classBody" },
65+
{ value: "classBodyDeclaration" },
66+
{ value: "classMemberDeclaration" },
67+
{ value: "fieldDeclaration" },
68+
{ value: "fieldModifier" },
69+
{ value: "variableDeclaratorList" },
70+
{ value: "variableDeclarator" },
71+
{ value: "variableDeclaratorId" },
72+
{ value: "variableInitializer" },
73+
{ value: "unannType" },
74+
{ value: "unannPrimitiveTypeWithOptionalDimsSuffix" },
75+
{ value: "unannPrimitiveType" },
76+
{ value: "unannReferenceType" },
77+
{ value: "unannClassOrInterfaceType" },
78+
{ value: "unannClassType" },
79+
{ value: "unannInterfaceType" },
80+
{ value: "unannTypeVariable" },
81+
{ value: "methodDeclaration" },
82+
{ value: "methodModifier" },
83+
{ value: "methodHeader" },
84+
{ value: "result" },
85+
{ value: "methodDeclarator" },
86+
{ value: "receiverParameter" },
87+
{ value: "formalParameterList" },
88+
{ value: "formalParameter" },
89+
{ value: "variableParaRegularParameter" },
90+
{ value: "variableArityParameter" },
91+
{ value: "variableModifier" },
92+
{ value: "throws" },
93+
{ value: "exceptionTypeList" },
94+
{ value: "exceptionType" },
95+
{ value: "methodBody" },
96+
{ value: "instanceInitializer" },
97+
{ value: "staticInitializer" },
98+
{ value: "constructorDeclaration" },
99+
{ value: "constructorModifier" },
100+
{ value: "constructorDeclarator" },
101+
{ value: "simpleTypeName" },
102+
{ value: "constructorBody" },
103+
{ value: "explicitConstructorInvocation" },
104+
{ value: "unqualifiedExplicitConstructorInvocation" },
105+
{ value: "qualifiedExplicitConstructorInvocation" },
106+
{ value: "enumDeclaration" },
107+
{ value: "enumBody" },
108+
{ value: "enumConstantList" },
109+
{ value: "enumConstant" },
110+
{ value: "enumConstantModifier" },
111+
{ value: "enumBodyDeclarations" },
112+
{ value: "recordDeclaration" },
113+
{ value: "recordHeader" },
114+
{ value: "recordComponentList" },
115+
{ value: "recordComponent" },
116+
{ value: "variableArityRecordComponent" },
117+
{ value: "recordComponentModifier" },
118+
{ value: "recordBody" },
119+
{ value: "recordBodyDeclaration" },
120+
{ value: "compactConstructorDeclaration" },
121+
{ value: "isDims" },
122+
{ value: "expression" },
123+
{ value: "lambdaExpression" },
124+
{ value: "lambdaParameters" },
125+
{ value: "lambdaParametersWithBraces" },
126+
{ value: "lambdaParameterList" },
127+
{ value: "inferredLambdaParameterList" },
128+
{ value: "explicitLambdaParameterList" },
129+
{ value: "lambdaParameter" },
130+
{ value: "regularLambdaParameter" },
131+
{ value: "lambdaParameterType" },
132+
{ value: "lambdaBody" },
133+
{ value: "ternaryExpression" },
134+
{ value: "binaryExpression" },
135+
{ value: "unaryExpression" },
136+
{ value: "unaryExpressionNotPlusMinus" },
137+
{ value: "primary" },
138+
{ value: "primaryPrefix" },
139+
{ value: "primarySuffix" },
140+
{ value: "fqnOrRefType" },
141+
{ value: "fqnOrRefTypePartRest" },
142+
{ value: "fqnOrRefTypePartCommon" },
143+
{ value: "fqnOrRefTypePartFirst" },
144+
{ value: "parenthesisExpression" },
145+
{ value: "castExpression" },
146+
{ value: "primitiveCastExpression" },
147+
{ value: "referenceTypeCastExpression" },
148+
{ value: "newExpression" },
149+
{ value: "unqualifiedClassInstanceCreationExpression" },
150+
{ value: "classOrInterfaceTypeToInstantiate" },
151+
{ value: "typeArgumentsOrDiamond" },
152+
{ value: "diamond" },
153+
{ value: "methodInvocationSuffix" },
154+
{ value: "argumentList" },
155+
{ value: "arrayCreationExpression" },
156+
{ value: "arrayCreationDefaultInitSuffix" },
157+
{ value: "arrayCreationExplicitInitSuffix" },
158+
{ value: "dimExprs" },
159+
{ value: "dimExpr" },
160+
{ value: "classLiteralSuffix" },
161+
{ value: "arrayAccessSuffix" },
162+
{ value: "methodReferenceSuffix" },
163+
{ value: "pattern" },
164+
{ value: "typePattern" },
165+
{ value: "recordPattern" },
166+
{ value: "componentPatternList" },
167+
{ value: "componentPattern" },
168+
{ value: "unnamedPattern" },
169+
{ value: "guard" },
170+
{ value: "isRefTypeInMethodRef" },
171+
{ value: "interfaceDeclaration" },
172+
{ value: "normalInterfaceDeclaration" },
173+
{ value: "interfaceModifier" },
174+
{ value: "extendsInterfaces" },
175+
{ value: "interfacePermits" },
176+
{ value: "interfaceBody" },
177+
{ value: "interfaceMemberDeclaration" },
178+
{ value: "constantDeclaration" },
179+
{ value: "constantModifier" },
180+
{ value: "interfaceMethodDeclaration" },
181+
{ value: "interfaceMethodModifier" },
182+
{ value: "annotationTypeDeclaration" },
183+
{ value: "annotationTypeBody" },
184+
{ value: "annotationTypeMemberDeclaration" },
185+
{ value: "annotationTypeElementDeclaration" },
186+
{ value: "annotationTypeElementModifier" },
187+
{ value: "defaultValue" },
188+
{ value: "annotation" },
189+
{ value: "elementValuePairList" },
190+
{ value: "elementValuePair" },
191+
{ value: "elementValue" },
192+
{ value: "elementValueArrayInitializer" },
193+
{ value: "elementValueList" },
194+
{ value: "literal" },
195+
{ value: "integerLiteral" },
196+
{ value: "floatingPointLiteral" },
197+
{ value: "booleanLiteral" },
198+
{ value: "moduleName" },
199+
{ value: "packageName" },
200+
{ value: "typeName" },
201+
{ value: "expressionName" },
202+
{ value: "methodName" },
203+
{ value: "packageOrTypeName" },
204+
{ value: "ambiguousName" },
205+
{ value: "compilationUnit" },
206+
{ value: "ordinaryCompilationUnit" },
207+
{ value: "modularCompilationUnit" },
208+
{ value: "packageDeclaration" },
209+
{ value: "packageModifier" },
210+
{ value: "importDeclaration" },
211+
{ value: "typeDeclaration" },
212+
{ value: "moduleDeclaration" },
213+
{ value: "moduleDirective" },
214+
{ value: "requiresModuleDirective" },
215+
{ value: "exportsModuleDirective" },
216+
{ value: "opensModuleDirective" },
217+
{ value: "usesModuleDirective" },
218+
{ value: "providesModuleDirective" },
219+
{ value: "requiresModifier" },
220+
{ value: "primitiveType" },
221+
{ value: "numericType" },
222+
{ value: "integralType" },
223+
{ value: "floatingPointType" },
224+
{ value: "referenceType" },
225+
{ value: "classOrInterfaceType" },
226+
{ value: "classType" },
227+
{ value: "interfaceType" },
228+
{ value: "typeVariable" },
229+
{ value: "dims" },
230+
{ value: "typeParameter" },
231+
{ value: "typeParameterModifier" },
232+
{ value: "typeBound" },
233+
{ value: "additionalBound" },
234+
{ value: "typeArguments" },
235+
{ value: "typeArgumentList" },
236+
{ value: "typeArgument" },
237+
{ value: "wildcard" },
238+
{ value: "wildcardBounds" }
239+
],
240+
description: "Prettify from the entrypoint, allowing to use prettier on snippet."
241+
}
242+
};

0 commit comments

Comments
 (0)