Skip to content

Commit

Permalink
Add quick fix for sortTypealiases unhandled cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 26, 2024
1 parent 6f0d8db commit cae4d67
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
12 changes: 12 additions & 0 deletions Sources/ParsingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2220,6 +2220,18 @@ extension Formatter {

guard !andTokenIndices.isEmpty else { return nil }

// Quick fix for types we don't support yet
let firstRange = tokens[equalsIndex ..< andTokenIndices[0]]
guard !firstRange.contains(where: {
["any", "[", "(", ":"].contains($0.string)
}), firstRange.filter({
$0 == .startOfScope("<")
}).count == firstRange.filter({
$0 == .endOfScope(">")
}).count else {
return nil
}

return (equalsIndex, andTokenIndices, type.range.upperBound)
}

Expand Down
13 changes: 3 additions & 10 deletions Tests/Rules/SortTypealiasesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,8 @@ class SortTypealiasesTests: XCTestCase {
}

func testSortSingleLineTypealiasBeginningWithAny() {
let input = """
typealias Placeholders = any Foo & Bar & Quux & Baaz
"""

let output = """
typealias Placeholders = any Baaz & Bar & Foo & Quux
"""

testFormatting(for: input, output, rule: .sortTypealiases)
let input = "typealias Placeholders = any Bar & Foo"
testFormatting(for: input, rule: .sortTypealiases)
}

func testCollectionTypealiasWithArrayOfExistentialTypes() {
Expand All @@ -199,7 +192,7 @@ class SortTypealiasesTests: XCTestCase {
}

func testCollectionTypealiasWithOptionalExistentialType() {
let input = "public typealias Parameters = (any Hashable & Sendable)?"
let input = "public typealias Parameters = (Hashable & Sendable)?"
testFormatting(for: input, rule: .sortTypealiases)
}

Expand Down

0 comments on commit cae4d67

Please sign in to comment.