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
41 changes: 39 additions & 2 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function willPrintOwnComments(path: AstPath<SyntaxNode>): boolean {
return isMember(path.node) && !printer.hasPrettierIgnore(path);
}

export function canAttachComment(node: SyntaxNode) {
export function canAttachComment(node: SyntaxNode, ancestors: SyntaxNode[]) {
if (!node.isNamed) {
return isBinaryOperator(node);
}
Expand All @@ -81,11 +81,25 @@ export function canAttachComment(node: SyntaxNode) {
case SyntaxType.FormalParameters:
case SyntaxType.Modifier:
case SyntaxType.MultilineStringFragment:
case SyntaxType.ParenthesizedExpression:
case SyntaxType.Program:
case SyntaxType.StringFragment:
case SyntaxType.Visibility:
return false;
case SyntaxType.ParenthesizedExpression: {
const [parent] = ancestors;
return !(
parent.isNamed &&
[
SyntaxType.DoStatement,
SyntaxType.IfStatement,
SyntaxType.SwitchExpression,
SyntaxType.SynchronizedStatement,
SyntaxType.TryStatement,
SyntaxType.TryWithResourcesStatement,
SyntaxType.WhileStatement
].includes(parent.type)
);
}
default:
return true;
}
Expand All @@ -102,6 +116,7 @@ export function handleLineComment(
handleIfStatementComments,
handleJumpStatementComments,
handleLabeledStatementComments,
handleLambdaExpressionComments,
handleMemberChainComments,
handleModifiersComments,
handleNameComments,
Expand Down Expand Up @@ -194,6 +209,28 @@ function handleLabeledStatementComments(commentNode: CommentNode) {
return false;
}

function handleLambdaExpressionComments(commentNode: CommentNode) {
const { enclosingNode, precedingNode, followingNode } = commentNode;
if (
enclosingNode?.type === SyntaxType.LambdaExpression &&
precedingNode &&
followingNode &&
enclosingNode.children.find(({ type }) => type === "->")!.end.index <
commentNode.start.index
) {
if (followingNode.type === SyntaxType.Block) {
if (followingNode.namedChildren.length) {
util.addLeadingComment(followingNode.namedChildren[0], commentNode);
} else {
util.addDanglingComment(followingNode, commentNode, undefined);
}
} else {
util.addLeadingComment(followingNode, commentNode);
}
return true;
}
}

function handleMemberChainComments(commentNode: CommentNode) {
const { enclosingNode, precedingNode, followingNode } = commentNode;
if (
Expand Down
3 changes: 2 additions & 1 deletion src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default {
hasPrettierIgnore(path) {
return (
path.node.comments?.some(isPrettierIgnore) === true ||
(canAttachComment(path.node) && isFullyBetweenPrettierIgnore(path))
(canAttachComment(path.node, path.parent ? [path.parent] : []) &&
isFullyBetweenPrettierIgnore(path))
);
},
canAttachComment,
Expand Down
8 changes: 5 additions & 3 deletions src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
indentIfBreak,
join,
line,
lineSuffixBoundary,
softline
} = builders;
const { getNextNonSpaceNonCommentCharacterIndex, hasNewline, isNextLineEmpty } =
Expand Down Expand Up @@ -147,7 +148,7 @@ export default {
}

const parameters = join([",", line], identifiers);
if (identifiers.length > 1) {
if (identifiers.length > 1 || willBreak(identifiers)) {
return group(indentInParentheses(parameters));
}
return options.arrowParens === "avoid"
Expand Down Expand Up @@ -684,8 +685,9 @@ function printLambdaExpressionSignature(
}

if (path.node.parametersNode.type === SyntaxType.Identifier) {
parts.unshift("(");
parts.push(")");
return path.node.parametersNode.comments
? group(["(", indent([softline, ...parts]), lineSuffixBoundary, ")"])
: ["(", ...parts, ")"];
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/unit-test/comments/expression/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ class Example {
void example() {
0 //
+ 1;

a +
// comment
(b);
}
}
4 changes: 4 additions & 0 deletions test/unit-test/comments/expression/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ class Example {
void example() {
0 + //
1;

a +
// comment
(b);
}
}
41 changes: 41 additions & 0 deletions test/unit-test/lambda/arrow-parens-always/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,44 @@ enum Enum {
return n * 2;
}, other)
}

// comment
a -> b;

a // comment
-> b;

a -> // comment
b;

a -> b; // comment

// comment
a -> {};

a // comment
-> {};

a -> // comment
{};

a -> {}; // comment

// comment
a -> {
b;
};

a // comment
-> {
b;
};

a -> // comment
{
b;
};

a -> {
b;
}; // comment
46 changes: 46 additions & 0 deletions test/unit-test/lambda/arrow-parens-always/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,49 @@ enum Enum {
return n * 2;
}, other),
}

// comment
(a) -> b;

(
a // comment
) -> b;

(a) ->
// comment
b;

(a) -> b; // comment

// comment
(a) -> {};

(
a // comment
) -> {};

(a) -> {
// comment
};

(a) -> {}; // comment

// comment
(a) -> {
b;
};

(
a // comment
) -> {
b;
};

(a) -> {
// comment
b;
};

(a) -> {
b;
}; // comment
41 changes: 41 additions & 0 deletions test/unit-test/lambda/arrow-parens-avoid/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,44 @@ enum Enum {
return n * 2;
}, other)
}

// comment
a -> b;

a // comment
-> b;

a -> // comment
b;

a -> b; // comment

// comment
a -> {};

a // comment
-> {};

a -> // comment
{};

a -> {}; // comment

// comment
a -> {
b;
};

a // comment
-> {
b;
};

a -> // comment
{
b;
};

a -> {
b;
}; // comment
46 changes: 46 additions & 0 deletions test/unit-test/lambda/arrow-parens-avoid/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,49 @@ enum Enum {
return n * 2;
}, other),
}

// comment
a -> b;

(
a // comment
) -> b;

a ->
// comment
b;

a -> b; // comment

// comment
a -> {};

(
a // comment
) -> {};

a -> {
// comment
};

a -> {}; // comment

// comment
a -> {
b;
};

(
a // comment
) -> {
b;
};

a -> {
// comment
b;
};

a -> {
b;
}; // comment