Skip to content

Commit e3e01a1

Browse files
committed
Fix issue where switch case with multiple where clauses could parse incorrectly
1 parent a22fbc3 commit e3e01a1

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

Sources/Tokenizer.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,8 +1788,6 @@ public func tokenize(_ source: String) -> [Token] {
17881788
convertOpeningChevronToOperator(at: scopeIndex)
17891789
processToken()
17901790
return
1791-
case .keyword("where"):
1792-
break
17931791
case .endOfScope, .keyword:
17941792
// If we encountered a keyword, or closing scope token that wasn't >
17951793
// then the opening < must have been an operator after all

Tests/RulesTests+Redundancy.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8788,6 +8788,72 @@ class RedundancyTests: RulesTests {
87888788
options: options, exclude: ["indent", "blankLinesBetweenScopes"])
87898789
}
87908790

8791+
func testRedundantSwitchStatementReturnInFunctionWithMultipleWhereClauses() {
8792+
// https://github.com/nicklockwood/SwiftFormat/issues/1554
8793+
let input = """
8794+
func foo(cases: FooCases, count: Int) -> String? {
8795+
switch cases {
8796+
case .fooCase1 where count == 0:
8797+
return "foo"
8798+
case .fooCase2 where count < 100,
8799+
.fooCase3 where count < 100,
8800+
.fooCase4:
8801+
return "bar"
8802+
default:
8803+
return nil
8804+
}
8805+
}
8806+
"""
8807+
let output = """
8808+
func foo(cases: FooCases, count: Int) -> String? {
8809+
switch cases {
8810+
case .fooCase1 where count == 0:
8811+
"foo"
8812+
case .fooCase2 where count < 100,
8813+
.fooCase3 where count < 100,
8814+
.fooCase4:
8815+
"bar"
8816+
default:
8817+
nil
8818+
}
8819+
}
8820+
"""
8821+
let options = FormatOptions(swiftVersion: "5.9")
8822+
testFormatting(for: input, output, rule: FormatRules.redundantReturn, options: options)
8823+
}
8824+
8825+
func testRedundantSwitchStatementReturnInFunctionWithSingleWhereClause() {
8826+
// https://github.com/nicklockwood/SwiftFormat/issues/1554
8827+
let input = """
8828+
func anotherFoo(cases: FooCases, count: Int) -> String? {
8829+
switch cases {
8830+
case .fooCase1 where count == 0:
8831+
return "foo"
8832+
case .fooCase2 where count < 100,
8833+
.fooCase4:
8834+
return "bar"
8835+
default:
8836+
return nil
8837+
}
8838+
}
8839+
"""
8840+
let output = """
8841+
func anotherFoo(cases: FooCases, count: Int) -> String? {
8842+
switch cases {
8843+
case .fooCase1 where count == 0:
8844+
"foo"
8845+
case .fooCase2 where count < 100,
8846+
.fooCase4:
8847+
"bar"
8848+
default:
8849+
nil
8850+
}
8851+
}
8852+
"""
8853+
let options = FormatOptions(swiftVersion: "5.9")
8854+
testFormatting(for: input, output, rule: FormatRules.redundantReturn, options: options)
8855+
}
8856+
87918857
// MARK: - redundantOptionalBinding
87928858

87938859
func testRemovesRedundantOptionalBindingsInSwift5_7() {

Tests/TokenizerTests.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,26 +3266,6 @@ class TokenizerTests: XCTestCase {
32663266
XCTAssertEqual(tokenize(input), output)
32673267
}
32683268

3269-
func testGenericsWithWhereClause() {
3270-
let input = "<A where A.B == C>"
3271-
let output: [Token] = [
3272-
.startOfScope("<"),
3273-
.identifier("A"),
3274-
.space(" "),
3275-
.keyword("where"),
3276-
.space(" "),
3277-
.identifier("A"),
3278-
.operator(".", .infix),
3279-
.identifier("B"),
3280-
.space(" "),
3281-
.operator("==", .infix),
3282-
.space(" "),
3283-
.identifier("C"),
3284-
.endOfScope(">"),
3285-
]
3286-
XCTAssertEqual(tokenize(input), output)
3287-
}
3288-
32893269
func testIfLessThanGreaterThanExpression() {
32903270
let input = "if x < (y + z), y > (z * w) {}"
32913271
let output: [Token] = [

0 commit comments

Comments
 (0)