Fix issue where closure with if expression in method call would be unexpectedly removed

This commit is contained in:
Cal Stephens
2023-11-11 07:51:55 +00:00
committed by Nick Lockwood
parent b31b80ffb6
commit 2b0c472d5e
2 changed files with 65 additions and 0 deletions
+1
View File
@@ -6263,6 +6263,7 @@ public struct _FormatRules {
case (nil, nil):
potentialStartOfExpressionContainingClosure = nil
case (.some(let startOfScope), nil):
guard formatter.tokens[startOfScope] == .startOfScope("{") else { return }
potentialStartOfExpressionContainingClosure = startOfScope
case (nil, let .some(assignmentBeforeClosure)):
potentialStartOfExpressionContainingClosure = assignmentBeforeClosure
+64
View File
@@ -8485,6 +8485,70 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, rule: FormatRules.redundantClosure, options: options)
}
func testClosureNotRemovedInMethodCall() {
let input = """
XCTAssert({
if foo {
bar
} else {
baaz
}
}())
"""
let options = FormatOptions(swiftVersion: "5.9")
testFormatting(for: input, rule: FormatRules.redundantClosure, options: options)
}
func testClosureNotRemovedInMethodCall2() {
let input = """
method("foo", {
if foo {
bar
} else {
baaz
}
}())
"""
let options = FormatOptions(swiftVersion: "5.9")
testFormatting(for: input, rule: FormatRules.redundantClosure, options: options)
}
func testClosureNotRemovedInMethodCall3() {
let input = """
XCTAssert({
if foo {
bar
} else {
baaz
}
}(), "message")
"""
let options = FormatOptions(swiftVersion: "5.9")
testFormatting(for: input, rule: FormatRules.redundantClosure, options: options)
}
func testClosureNotRemovedInMethodCall4() {
let input = """
method(
"foo",
{
if foo {
bar
} else {
baaz
}
}(),
"bar"
)
"""
let options = FormatOptions(swiftVersion: "5.9")
testFormatting(for: input, rule: FormatRules.redundantClosure, options: options)
}
// MARK: Redundant optional binding
func testRemovesRedundantOptionalBindingsInSwift5_7() {