mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Fix range crash
This commit is contained in:
@@ -741,7 +741,7 @@ public extension Formatter {
|
||||
for i in range.reversed() {
|
||||
let token = tokens[i]
|
||||
switch token {
|
||||
case .startOfScope(":") where scopeStack.last == .endOfScope("#endif"):
|
||||
case .startOfScope(":") where [.endOfScope("#endif"), .endOfScope("}")].contains(scopeStack.last):
|
||||
break
|
||||
case .startOfScope where scopeStack.last?.isEndOfScope(token) == true:
|
||||
scopeStack.removeLast()
|
||||
@@ -758,7 +758,7 @@ public extension Formatter {
|
||||
case .linebreak:
|
||||
linebreakEncountered = true
|
||||
case .endOfScope("case"), .endOfScope("default"):
|
||||
if scopeStack.last != .endOfScope("#endif") {
|
||||
if ![.endOfScope("#endif"), .endOfScope("}")].contains(scopeStack.last) {
|
||||
fallthrough
|
||||
}
|
||||
case .endOfScope:
|
||||
|
||||
@@ -173,12 +173,14 @@ public extension Formatter {
|
||||
$0.isEndOfScope(startToken) || $0 == .endOfScope("#endif")
|
||||
})
|
||||
}
|
||||
assert(endIndex ?? index >= index)
|
||||
return endIndex
|
||||
}
|
||||
while let endIndex = self.index(after: startIndex, where: {
|
||||
$0.isEndOfScope(startToken)
|
||||
}), let token = token(at: endIndex) {
|
||||
if token == .endOfScope("}") {
|
||||
assert(endIndex >= index)
|
||||
return endIndex
|
||||
}
|
||||
startIndex = endIndex
|
||||
|
||||
@@ -1524,4 +1524,38 @@ final class UnusedArgumentsTests: XCTestCase {
|
||||
|
||||
testFormatting(for: input, rule: .unusedArguments)
|
||||
}
|
||||
|
||||
func testIfdefCrash() {
|
||||
let input = """
|
||||
func test(unused: Int) {
|
||||
foo {
|
||||
if true {
|
||||
#if FOO
|
||||
switch 1 {
|
||||
default: ()
|
||||
}
|
||||
#endif
|
||||
} else if true {
|
||||
()
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
let output = """
|
||||
func test(unused _: Int) {
|
||||
foo {
|
||||
if true {
|
||||
#if FOO
|
||||
switch 1 {
|
||||
default: ()
|
||||
}
|
||||
#endif
|
||||
} else if true {
|
||||
()
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
testFormatting(for: input, output, rule: .unusedArguments)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user