Fix parsing trailing closure after unwrap operator

This commit is contained in:
Nick Lockwood
2023-03-27 18:39:55 +01:00
parent 35dd6f4ddd
commit ace7fc018c
3 changed files with 24 additions and 3 deletions
+3 -3
View File
@@ -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
+7
View File
@@ -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() {
+14
View File
@@ -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 {