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
15 changes: 4 additions & 11 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,12 @@ function handleMemberChainComments(commentNode: CommentNode) {
const { enclosingNode, precedingNode, followingNode } = commentNode;
if (
(enclosingNode?.type === SyntaxType.FieldAccess ||
enclosingNode?.type === SyntaxType.MethodInvocation) &&
(enclosingNode?.type === SyntaxType.MethodInvocation &&
precedingNode?.end.row !== commentNode.start.row)) &&
followingNode?.type === SyntaxType.Identifier
) {
util.addLeadingComment(enclosingNode, commentNode);
return true;
} else if (
followingNode &&
isMember(followingNode) &&
precedingNode !== enclosingNode &&
!isPrettierIgnore(commentNode)
) {
util.addDanglingComment(followingNode, commentNode, undefined);
return true;
}
return false;
}
Expand Down Expand Up @@ -373,11 +366,11 @@ function printTrailingComment(
hasNewline(originalText, parser.locStart(comment), { backwards: true })
) {
// This allows comments at the end of nested structures:
// f(
// {
// 1,
// 2
// // A comment
// );
// }
// Those kinds of comments are almost always leading comments, but
// here it doesn't go "outside" the block and turns it into a
// trailing comment for `2`. We can simulate the above by checking
Expand Down
12 changes: 6 additions & 6 deletions test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ export function testSampleWithOptions({
});

it(`Performs a stable formatting for <${relativeInputPath}>`, async () => {
const onePass = await formatJavaSnippet({
const firstPass = await formatJavaSnippet({
snippet: inputContents,
prettierOptions
});

const secondPass = await formatJavaSnippet({
snippet: onePass,
snippet: firstPass,
prettierOptions
});
expect(onePass).to.equal(secondPass);
expect(secondPass).to.equal(firstPass);
});
}

Expand Down Expand Up @@ -85,9 +85,9 @@ export function testRepositorySample(
"utf8"
);

const onePass = await formatJavaSnippet({ snippet: javaFileText });
const secondPass = await formatJavaSnippet({ snippet: onePass });
expect(onePass).to.equal(secondPass);
const firstPass = await formatJavaSnippet({ snippet: javaFileText });
const secondPass = await formatJavaSnippet({ snippet: firstPass });
expect(secondPass).to.equal(firstPass);
});
});

Expand Down
14 changes: 14 additions & 0 deletions test/unit-test/member_chain/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@ public void doSomething() {
}

public void doSomethingNewWithComment() {
// comment
new Object().something().more();

new Object() // comment
.something().more();

new Object()
// comment
.something().more();

new Object().something() // comment
.more();

new Object().something()
// comment
.more();

new Object().something().more(); // comment

new Object().something().more();
// comment
}

public void doSomethingWithComment() {
Expand Down
19 changes: 17 additions & 2 deletions test/unit-test/member_chain/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ public void doSomething() {
}

public void doSomethingNewWithComment() {
// comment
new Object().something().more();

new Object() // comment
.something()
.more();

new Object()
// comment
.something()
.more();

new Object()
.something() // comment
.more();

new Object()
.something()
// comment
.more();

new Object().something().more(); // comment

new Object().something().more();
// comment
}

public void doSomethingWithComment() {
Expand Down Expand Up @@ -61,8 +77,7 @@ public void doSomethingWithComment() {
.util()
.java.java();

averyveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylong.java
/* comment */
averyveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylong.java /* comment */
.util()
.java.java();

Expand Down
6 changes: 3 additions & 3 deletions test/unit-test/template-expression/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class TemplateExpression {
file.exists() ? "does" : "does not"
} exist";

String time =
STR."The time is \{// The java.time.format package is very useful
DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalTime.now())} right now";
String time = STR."The time is \{DateTimeFormatter.ofPattern("HH:mm:ss")
// The java.time.format package is very useful
.format(LocalTime.now())} right now";

String data = STR."\{index++}, \{index++}, \{index++}, \{index++}";

Expand Down