mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix issue where closure with if expression in method call would be unexpectedly removed
This commit is contained in:
committed by
Nick Lockwood
parent
b31b80ffb6
commit
2b0c472d5e
@@ -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
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user