mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix parsing trailing closure after unwrap operator
This commit is contained in:
@@ -529,7 +529,8 @@ extension Formatter {
|
||||
}
|
||||
}
|
||||
fallthrough
|
||||
case .endOfScope where tokens[prevIndex].isStringDelimiter, .identifier, .number:
|
||||
case .identifier, .number, .operator("?", .postfix), .operator("!", .postfix),
|
||||
.endOfScope where tokens[prevIndex].isStringDelimiter:
|
||||
if let nextIndex = index(of: .nonSpaceOrCommentOrLinebreak, after: i),
|
||||
isAccessorKeyword(at: nextIndex) || isAccessorKeyword(at: prevIndex)
|
||||
{
|
||||
@@ -573,8 +574,7 @@ extension Formatter {
|
||||
default:
|
||||
return true
|
||||
}
|
||||
case .operator("?", .postfix), .operator("!", .postfix),
|
||||
.keyword, .endOfScope("]"), .endOfScope(">"):
|
||||
case .keyword, .endOfScope("]"), .endOfScope(">"):
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
|
||||
@@ -550,6 +550,13 @@ class ParsingHelpersTests: XCTestCase {
|
||||
XCTAssert(formatter.isStartOfClosure(at: 6))
|
||||
}
|
||||
|
||||
func testTrailingClosureOnOptionalMethod() {
|
||||
let formatter = Formatter(tokenize("""
|
||||
foo.bar? { print("") }
|
||||
"""))
|
||||
XCTAssert(formatter.isStartOfClosure(at: 5))
|
||||
}
|
||||
|
||||
// MARK: isAccessorKeyword
|
||||
|
||||
func testDidSet() {
|
||||
|
||||
@@ -4405,6 +4405,20 @@ class RedundancyTests: RulesTests {
|
||||
XCTAssertNoThrow(try format(input, rules: [FormatRules.redundantSelf]))
|
||||
}
|
||||
|
||||
func testRedundantSelfParsingBug5() {
|
||||
let input = """
|
||||
Button.primary(
|
||||
title: "Title",
|
||||
tapHandler: { [weak self] in
|
||||
self?.dismissBlock? {
|
||||
// something
|
||||
}
|
||||
}
|
||||
)
|
||||
"""
|
||||
XCTAssertNoThrow(try format(input, rules: [FormatRules.redundantSelf]))
|
||||
}
|
||||
|
||||
func testRedundantSelfWithStaticMethodAfterForLoop() {
|
||||
let input = """
|
||||
struct Foo {
|
||||
|
||||
Reference in New Issue
Block a user