mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Fix bug in isStartOfClosure affecting self removal
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
// swiftformat:options --swiftversion 5.2
|
||||
|
||||
final class Value<T> {
|
||||
private let provider: () -> T
|
||||
|
||||
init(provider: @escaping () -> T) {
|
||||
self.provider = provider
|
||||
}
|
||||
}
|
||||
|
||||
final class Consumer {
|
||||
private(set) lazy var value = Value<String> { [unowned self] in
|
||||
self.someProvider()
|
||||
}
|
||||
|
||||
private func someProvider() -> String {
|
||||
"string"
|
||||
}
|
||||
}
|
||||
@@ -325,12 +325,16 @@ extension Formatter {
|
||||
return false
|
||||
}
|
||||
return !isStartOfClosure(at: startOfScope)
|
||||
case .endOfScope(")"):
|
||||
guard let startOfScope = index(of: .startOfScope("("), before: prevIndex),
|
||||
let prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startOfScope)
|
||||
case .endOfScope(")"), .endOfScope(">"):
|
||||
guard var startOfScope = index(of: .startOfScope, before: prevIndex),
|
||||
var prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startOfScope)
|
||||
else {
|
||||
return true
|
||||
}
|
||||
if tokens[prevIndex] == .endOfScope(">"), tokens[prev] == .endOfScope(")") {
|
||||
startOfScope = index(of: .startOfScope, before: prev) ?? startOfScope
|
||||
prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startOfScope) ?? prev
|
||||
}
|
||||
switch tokens[prev] {
|
||||
case .identifier:
|
||||
prevIndex = prev
|
||||
|
||||
@@ -389,6 +389,11 @@ class ParsingHelpersTests: XCTestCase {
|
||||
XCTAssertFalse(formatter.isStartOfClosure(at: 25))
|
||||
}
|
||||
|
||||
func testClosureAfterGenericType() {
|
||||
let formatter = Formatter(tokenize("let foo = Foo<String> {}"))
|
||||
XCTAssert(formatter.isStartOfClosure(at: 11))
|
||||
}
|
||||
|
||||
// MARK: isAccessorKeyword
|
||||
|
||||
func testDidSet() {
|
||||
|
||||
@@ -327,6 +327,7 @@ extension ParsingHelpersTests {
|
||||
("testChainedTrailingClosureInVarChain2", testChainedTrailingClosureInVarChain2),
|
||||
("testChainedTrailingClosureInVarChain3", testChainedTrailingClosureInVarChain3),
|
||||
("testClassBracesNotTreatedAsClosure", testClassBracesNotTreatedAsClosure),
|
||||
("testClosureAfterGenericType", testClosureAfterGenericType),
|
||||
("testClosureInsideIfCondition", testClosureInsideIfCondition),
|
||||
("testComputedVarAllmanBracesNotTreatedAsClosure", testComputedVarAllmanBracesNotTreatedAsClosure),
|
||||
("testComputedVarBracesNotTreatedAsClosure", testComputedVarBracesNotTreatedAsClosure),
|
||||
|
||||
Reference in New Issue
Block a user