From cae4d67c10b2c97dcaff17b7cad89a2ddb26a5ea Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Tue, 26 Nov 2024 11:45:49 +0000 Subject: [PATCH] Add quick fix for `sortTypealiases` unhandled cases --- Sources/ParsingHelpers.swift | 12 ++++++++++++ Tests/Rules/SortTypealiasesTests.swift | 13 +++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Sources/ParsingHelpers.swift b/Sources/ParsingHelpers.swift index 53aa55bc6..84e26ac5c 100644 --- a/Sources/ParsingHelpers.swift +++ b/Sources/ParsingHelpers.swift @@ -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) } diff --git a/Tests/Rules/SortTypealiasesTests.swift b/Tests/Rules/SortTypealiasesTests.swift index 8498e48fe..65e483722 100644 --- a/Tests/Rules/SortTypealiasesTests.swift +++ b/Tests/Rules/SortTypealiasesTests.swift @@ -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() { @@ -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) }