Skip to content

Commit adf3bda

Browse files
committed
[NoEmptyTrailingClosureParentheses] Do not remove parens if they contain comments.
Fixes #665.
1 parent 994f34b commit adf3bda

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Sources/SwiftFormat/Rules/NoEmptyTrailingClosureParentheses.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
2727
guard
2828
let trailingClosure = node.trailingClosure,
2929
let leftParen = node.leftParen,
30-
node.arguments.isEmpty
30+
let rightParen = node.rightParen,
31+
node.arguments.isEmpty,
32+
!leftParen.trailingTrivia.hasAnyComments,
33+
!rightParen.leadingTrivia.hasAnyComments
3134
else {
3235
return super.visit(node)
3336
}

Tests/SwiftFormatTests/Rules/NoEmptyTrailingClosureParenthesesTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,29 @@ final class NoEmptyTrailingClosureParenthesesTests: LintOrFormatRuleTestCase {
8383
]
8484
)
8585
}
86+
87+
func testDoNotRemoveParensContainingOnlyComments() {
88+
assertFormatting(
89+
NoEmptyTrailingClosureParentheses.self,
90+
input: """
91+
greetEnthusiastically(/*oldArg: x*/) { "John" }
92+
greetEnthusiastically(
93+
/*oldArg: x*/
94+
) { "John" }
95+
greetEnthusiastically(
96+
// oldArg: x
97+
) { "John" }
98+
""",
99+
expected: """
100+
greetEnthusiastically(/*oldArg: x*/) { "John" }
101+
greetEnthusiastically(
102+
/*oldArg: x*/
103+
) { "John" }
104+
greetEnthusiastically(
105+
// oldArg: x
106+
) { "John" }
107+
""",
108+
findings: []
109+
)
110+
}
86111
}

0 commit comments

Comments
 (0)