Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { SyntaxType, type NamedNode } from "../node-types.ts";
import {
definedKeys,
hasChild,
hasLeadingComments,
indentInParentheses,
printAssignment,
printBlock,
printBlockStatements,
printBodyDeclarations,
Expand All @@ -16,7 +16,7 @@ import {
type NamedNodePrinters
} from "./helpers.ts";

const { group, hardline, indent, indentIfBreak, join, line } = builders;
const { group, hardline, indent, join, line } = builders;

export default {
class_declaration(path, print) {
Expand Down Expand Up @@ -96,31 +96,16 @@ export default {
field_declaration: printVariableDeclaration,

variable_declarator(path, print) {
const declarator = [path.call(print, "nameNode")];
const leftDoc = [path.call(print, "nameNode")];
if (hasChild(path, "dimensionsNode")) {
declarator.push(path.call(print, "dimensionsNode"));
leftDoc.push(path.call(print, "dimensionsNode"));
}
if (!hasChild(path, "valueNode")) {
return declarator;
}
declarator.push(" =");
const value = path.call(print, "valueNode");
if (
path.node.valueNode.type === SyntaxType.BinaryExpression ||
(path.node.valueNode.type === SyntaxType.TernaryExpression &&
path.node.valueNode.conditionNode.type ===
SyntaxType.BinaryExpression) ||
hasLeadingComments(path.node.valueNode)
) {
declarator.push(group(indent([line, value])));
} else {
const groupId = Symbol("assignment");
declarator.push(
group(indent(line), { id: groupId }),
indentIfBreak(value, { groupId })
);
}
return group(declarator);
return printAssignment(
leftDoc,
" =",
hasChild(path, "valueNode") ? path.call(print, "valueNode") : undefined,
path.node.valueNode
);
},

method_declaration(path, print) {
Expand Down
29 changes: 8 additions & 21 deletions src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
} from "../node-types.ts";
import {
hasChild,
hasLeadingComments,
hasType,
indentInParentheses,
isReturnOrThrowStatement,
printAssignment,
printDanglingComments,
shouldFlatten,
type JavaParserOptions,
Expand Down Expand Up @@ -232,26 +232,13 @@ export default {
},

assignment_expression(path, print) {
const { operatorNode, rightNode } = path.node;
const parts = [path.call(print, "leftNode"), " ", operatorNode.type];
const right = path.call(print, "rightNode");

if (
rightNode.type === SyntaxType.BinaryExpression ||
(rightNode.type === SyntaxType.TernaryExpression &&
rightNode.conditionNode.type === SyntaxType.BinaryExpression) ||
hasLeadingComments(rightNode)
) {
parts.push(group(indent([line, right])));
} else {
const groupId = Symbol("assignment");
parts.push(
group(indent(line), { id: groupId }),
indentIfBreak(right, { groupId })
);
}

return parts;
const { node } = path;
return printAssignment(
path.call(print, "leftNode"),
[" ", node.operatorNode.type],
path.call(print, "rightNode"),
node.rightNode
);
},

binary_expression(path, print, options) {
Expand Down
48 changes: 47 additions & 1 deletion src/printers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ import type { AstPath, Doc, Options, ParserOptions } from "prettier";
import { builders, utils } from "prettier/doc";
import {
SyntaxType,
type ArrayInitializerNode,
type CommentNode,
type ExpressionNode,
type NamedNode,
type NamedType,
type SyntaxNode,
type TypeString
} from "../node-types.ts";

const { group, hardline, ifBreak, indent, join, line, softline } = builders;
const {
group,
hardline,
ifBreak,
indent,
indentIfBreak,
join,
line,
lineSuffixBoundary,
softline
} = builders;
const { mapDoc } = utils;

export function hasType<T extends NamedType>(
Expand Down Expand Up @@ -347,6 +359,40 @@ export function printVariableDeclaration(
return declaration;
}

export function printAssignment(
leftDoc: Doc,
operator: Doc,
rightDoc?: Doc,
rightNode?: ArrayInitializerNode | ExpressionNode
) {
if (!rightDoc || !rightNode) {
return leftDoc;
}

const breakAfterOperator =
rightNode.type === SyntaxType.BinaryExpression ||
rightNode.type === SyntaxType.InstanceofExpression ||
(rightNode.type === SyntaxType.TernaryExpression &&
(rightNode.conditionNode.type === SyntaxType.BinaryExpression ||
rightNode.conditionNode.type === SyntaxType.InstanceofExpression)) ||
hasLeadingComments(rightNode);

if (breakAfterOperator) {
// First break after operator, then right-hand side
return group([leftDoc, operator, group(indent([line, rightDoc]))]);
}

// First break right-hand side, then after operator
const groupId = Symbol("assignment");
return group([
leftDoc,
operator,
group(indent(line), { id: groupId }),
lineSuffixBoundary,
indentIfBreak(rightDoc, { groupId })
]);
}

export function printTextBlock(
path: NamedNodePath<SyntaxType.StringLiteral>,
contents: Doc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ void instanceOf() {

e instanceof @Ann final E baz;
f instanceof final @Ann E qux;

aaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb instanceof Cccccccccccccccccccc cccccccccccccccccccc;
aaaaaaaaaa = bbbbbbbbbb instanceof Cccccccccc cccccccccc ? dddddddddd : eeeeeeeeee;

var aaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb instanceof Cccccccccccccccccccc cccccccccccccccccccc;
var aaaaaaaaaa = bbbbbbbbbb instanceof Cccccccccc cccccccccc ? dddddddddd : eeeeeeeeee;
}

void unaryExpression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ void instanceOf() {

e instanceof @Ann final E baz;
f instanceof final @Ann E qux;

aaaaaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbbbbb instanceof Cccccccccccccccccccc cccccccccccccccccccc;
aaaaaaaaaa =
bbbbbbbbbb instanceof Cccccccccc cccccccccc ? dddddddddd : eeeeeeeeee;

var aaaaaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbbbbb instanceof Cccccccccccccccccccc cccccccccccccccccccc;
var aaaaaaaaaa =
bbbbbbbbbb instanceof Cccccccccc cccccccccc ? dddddddddd : eeeeeeeeee;
}

void unaryExpression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ void instanceOf() {

e instanceof @Ann final E baz;
f instanceof final @Ann E qux;

aaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb instanceof Cccccccccccccccccccc cccccccccccccccccccc;
aaaaaaaaaa = bbbbbbbbbb instanceof Cccccccccc cccccccccc ? dddddddddd : eeeeeeeeee;

var aaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb instanceof Cccccccccccccccccccc cccccccccccccccccccc;
var aaaaaaaaaa = bbbbbbbbbb instanceof Cccccccccc cccccccccc ? dddddddddd : eeeeeeeeee;
}

void unaryExpression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ void instanceOf() {

e instanceof @Ann final E baz;
f instanceof final @Ann E qux;

aaaaaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbbbbb instanceof Cccccccccccccccccccc cccccccccccccccccccc;
aaaaaaaaaa =
bbbbbbbbbb instanceof Cccccccccc cccccccccc ? dddddddddd : eeeeeeeeee;

var aaaaaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbbbbb instanceof Cccccccccccccccccccc cccccccccccccccccccc;
var aaaaaaaaaa =
bbbbbbbbbb instanceof Cccccccccc cccccccccc ? dddddddddd : eeeeeeeeee;
}

void unaryExpression() {
Expand Down