Skip to content

Commit 34e77db

Browse files
committed
Fix removing empty trailing closure parentheses in a curried call
Fixes #1070
1 parent f0f3a83 commit 34e77db

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Sources/SwiftFormat/Rules/NoEmptyTrailingClosureParentheses.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
3838
return super.visit(node)
3939
}
4040

41+
// Keep the empty parentheses when in a curried call to avoid the trailing closure
42+
// getting associated with the called call expression.
43+
guard
44+
!node.calledExpression.is(FunctionCallExprSyntax.self)
45+
&& !node.calledExpression.is(SubscriptCallExprSyntax.self)
46+
else {
47+
return super.visit(node)
48+
}
49+
4150
diagnose(.removeEmptyTrailingParentheses(name: "\(name.trimmedDescription)"), on: leftParen)
4251

4352
// Need to visit `calledExpression` before creating a new node so that the location data (column

Tests/SwiftFormatTests/Rules/NoEmptyTrailingClosureParenthesesTests.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class NoEmptyTrailingClosureParenthesesTests: LintOrFormatRuleTestCase {
4646
greetEnthusiastically8️⃣() { "Willis" }
4747
}
4848
}
49-
foo(bar🔟() { baz })9️⃣() { blah }
49+
foo(bar9️⃣() { baz }) { blah }
5050
""",
5151
expected: """
5252
func greetEnthusiastically(_ nameProvider: () -> String) {
@@ -89,8 +89,7 @@ final class NoEmptyTrailingClosureParenthesesTests: LintOrFormatRuleTestCase {
8989
FindingSpec("6️⃣", message: "remove the empty parentheses following 'async'"),
9090
FindingSpec("7️⃣", message: "remove the empty parentheses following 'greetEnthusiastically'"),
9191
FindingSpec("8️⃣", message: "remove the empty parentheses following 'greetEnthusiastically'"),
92-
FindingSpec("9️⃣", message: "remove the empty parentheses following ')'"),
93-
FindingSpec("🔟", message: "remove the empty parentheses following 'bar'"),
92+
FindingSpec("9️⃣", message: "remove the empty parentheses following 'bar'")
9493
]
9594
)
9695
}
@@ -119,4 +118,21 @@ final class NoEmptyTrailingClosureParenthesesTests: LintOrFormatRuleTestCase {
119118
findings: []
120119
)
121120
}
121+
122+
func testDoNotRemoveParensInCurriedCalls() {
123+
assertFormatting(
124+
NoEmptyTrailingClosureParentheses.self,
125+
input: """
126+
perform()() { foo }
127+
Executor.execute(executor)() { bar }
128+
withSubscript[baz]() { blah }
129+
""",
130+
expected: """
131+
perform()() { foo }
132+
Executor.execute(executor)() { bar }
133+
withSubscript[baz]() { blah }
134+
""",
135+
findings: []
136+
)
137+
}
122138
}

0 commit comments

Comments
 (0)