Skip to content

Commit

Permalink
Fix issue where switch case with multiple where clauses could parse i…
Browse files Browse the repository at this point in the history
…ncorrectly
  • Loading branch information
calda committed Nov 10, 2023
1 parent a22fbc3 commit e3e01a1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
2 changes: 0 additions & 2 deletions Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1788,8 +1788,6 @@ public func tokenize(_ source: String) -> [Token] {
convertOpeningChevronToOperator(at: scopeIndex)
processToken()
return
case .keyword("where"):
break
case .endOfScope, .keyword:
// If we encountered a keyword, or closing scope token that wasn't >
// then the opening < must have been an operator after all
Expand Down
66 changes: 66 additions & 0 deletions Tests/RulesTests+Redundancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8788,6 +8788,72 @@ class RedundancyTests: RulesTests {
options: options, exclude: ["indent", "blankLinesBetweenScopes"])
}

func testRedundantSwitchStatementReturnInFunctionWithMultipleWhereClauses() {
// https://github.com/nicklockwood/SwiftFormat/issues/1554
let input = """
func foo(cases: FooCases, count: Int) -> String? {
switch cases {
case .fooCase1 where count == 0:
return "foo"
case .fooCase2 where count < 100,
.fooCase3 where count < 100,
.fooCase4:
return "bar"
default:
return nil
}
}
"""
let output = """
func foo(cases: FooCases, count: Int) -> String? {
switch cases {
case .fooCase1 where count == 0:
"foo"
case .fooCase2 where count < 100,
.fooCase3 where count < 100,
.fooCase4:
"bar"
default:
nil
}
}
"""
let options = FormatOptions(swiftVersion: "5.9")
testFormatting(for: input, output, rule: FormatRules.redundantReturn, options: options)
}

func testRedundantSwitchStatementReturnInFunctionWithSingleWhereClause() {
// https://github.com/nicklockwood/SwiftFormat/issues/1554
let input = """
func anotherFoo(cases: FooCases, count: Int) -> String? {
switch cases {
case .fooCase1 where count == 0:
return "foo"
case .fooCase2 where count < 100,
.fooCase4:
return "bar"
default:
return nil
}
}
"""
let output = """
func anotherFoo(cases: FooCases, count: Int) -> String? {
switch cases {
case .fooCase1 where count == 0:
"foo"
case .fooCase2 where count < 100,
.fooCase4:
"bar"
default:
nil
}
}
"""
let options = FormatOptions(swiftVersion: "5.9")
testFormatting(for: input, output, rule: FormatRules.redundantReturn, options: options)
}

// MARK: - redundantOptionalBinding

func testRemovesRedundantOptionalBindingsInSwift5_7() {
Expand Down
20 changes: 0 additions & 20 deletions Tests/TokenizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3266,26 +3266,6 @@ class TokenizerTests: XCTestCase {
XCTAssertEqual(tokenize(input), output)
}

func testGenericsWithWhereClause() {
let input = "<A where A.B == C>"
let output: [Token] = [
.startOfScope("<"),
.identifier("A"),
.space(" "),
.keyword("where"),
.space(" "),
.identifier("A"),
.operator(".", .infix),
.identifier("B"),
.space(" "),
.operator("==", .infix),
.space(" "),
.identifier("C"),
.endOfScope(">"),
]
XCTAssertEqual(tokenize(input), output)
}

func testIfLessThanGreaterThanExpression() {
let input = "if x < (y + z), y > (z * w) {}"
let output: [Token] = [
Expand Down

0 comments on commit e3e01a1

Please sign in to comment.