Skip to content

Commit

Permalink
Fix more bugs with unhoisting pattern let
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 14, 2020
1 parent c34c916 commit d5bb6fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3337,21 +3337,27 @@ public struct _FormatRules {
// Check if pattern already starts with let/var
guard let endIndex = formatter.index(of: .endOfScope(")"), after: i),
let prevIndex = formatter.index(before: i, where: {
[.endOfScope("case"), .keyword("case"), .keyword("catch"),
.delimiter(","), .endOfScope("}"), .operator("=", .infix)].contains($0)
switch $0 {
case .operator(".", _), .keyword("let"), .keyword("var"):
return false
case .endOfScope, .delimiter, .operator, .keyword:
return true
default:
return false
}
})
else {
return
}
switch formatter.tokens[prevIndex] {
case .endOfScope("}"), .operator("=", .infix):
return
case .endOfScope("case"), .keyword("case"), .keyword("catch"):
break
case .delimiter(","):
loop: for token in formatter.tokens[0 ..< prevIndex].reversed() {
switch token {
case .endOfScope("case"), .keyword("catch"):
break loop
case .keyword("as"), .keyword("is"), .keyword("var"), .keyword("let"):
case .keyword("var"), .keyword("let"):
break
case .keyword:
// Tuple assignment
Expand All @@ -3361,7 +3367,7 @@ public struct _FormatRules {
}
}
default:
break
return
}
let startIndex = prevIndex + 1
if let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: prevIndex),
Expand Down
22 changes: 22 additions & 0 deletions Tests/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,28 @@ class RulesTests: XCTestCase {
testFormatting(for: input, rule: FormatRules.hoistPatternLet, options: options)
}

func testNoUnhoistSwitchCaseLetFollowedByWhere() {
let input = """
switch foo {
case let bar? where bar >= baz(quux):
break
}
"""
let options = FormatOptions(hoistPatternLet: false)
testFormatting(for: input, rule: FormatRules.hoistPatternLet, options: options)
}

func testNoUnhoistSwitchCaseLetFollowedByAs() {
let input = """
switch foo {
case let bar as (String, String):
break
}
"""
let options = FormatOptions(hoistPatternLet: false)
testFormatting(for: input, rule: FormatRules.hoistPatternLet, options: options)
}

// MARK: - enumNamespaces

func testEnumNamespacesClassAsProtocolRestriction() {
Expand Down
2 changes: 2 additions & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,8 @@ extension RulesTests {
("testNoUnhoistIfArgIsNamespacedEnumCaseLiteralInParens", testNoUnhoistIfArgIsNamespacedEnumCaseLiteralInParens),
("testNoUnhoistIfCaseFollowedByLetTuple", testNoUnhoistIfCaseFollowedByLetTuple),
("testNoUnhoistIfLetTuple", testNoUnhoistIfLetTuple),
("testNoUnhoistSwitchCaseLetFollowedByAs", testNoUnhoistSwitchCaseLetFollowedByAs),
("testNoUnhoistSwitchCaseLetFollowedByWhere", testNoUnhoistSwitchCaseLetFollowedByWhere),
("testNoUnhoistTupleLet", testNoUnhoistTupleLet),
("testNoWrapAfterFirstArgumentInStringInterpolation", testNoWrapAfterFirstArgumentInStringInterpolation),
("testNoWrapAfterFirstArgumentInStringInterpolation2", testNoWrapAfterFirstArgumentInStringInterpolation2),
Expand Down

0 comments on commit d5bb6fa

Please sign in to comment.