From 1be4d8b99f9fcedcf41869b274515dc4f8418de3 Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Thu, 9 Nov 2023 15:17:56 -0800 Subject: [PATCH] Fix issue where enumNamespaces rule wouldn't be applied following import statement --- Snapshots/Layout/Layout/ReloadManager.swift | 2 +- Sources/Rules.swift | 5 +-- Tests/RulesTests+Syntax.swift | 50 +++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Snapshots/Layout/Layout/ReloadManager.swift b/Snapshots/Layout/Layout/ReloadManager.swift index c1d25f059..1879d2a66 100755 --- a/Snapshots/Layout/Layout/ReloadManager.swift +++ b/Snapshots/Layout/Layout/ReloadManager.swift @@ -3,7 +3,7 @@ import Foundation import UIKit -class ReloadManager { +enum ReloadManager { private static var boxes = [ObserverBox]() private struct ObserverBox { diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 98e7beb32..29ca288c0 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -954,9 +954,8 @@ public struct _FormatRules { .keyword("class"), .keyword("struct"), ].contains($0) }) { i, token in - guard formatter.last(.keyword, before: i) != .keyword("import"), - // exit if class is a type modifier - let next = formatter.next(.nonSpaceOrCommentOrLinebreak, after: i), + // exit if class is a type modifier + guard let next = formatter.next(.nonSpaceOrCommentOrLinebreak, after: i), !(next.isKeyword || next.isModifierKeyword), // exit for class as protocol conformance formatter.last(.nonSpaceOrCommentOrLinebreak, before: i) != .delimiter(":"), diff --git a/Tests/RulesTests+Syntax.swift b/Tests/RulesTests+Syntax.swift index 445341cbe..bc5b9999d 100644 --- a/Tests/RulesTests+Syntax.swift +++ b/Tests/RulesTests+Syntax.swift @@ -928,6 +928,56 @@ class SyntaxTests: RulesTests { testFormatting(for: input, rule: FormatRules.enumNamespaces, options: options) } + func testEnumNamespacesAfterImport1() { + // https://github.com/nicklockwood/SwiftFormat/issues/1569 + let input = """ + import Foundation + + final class MyViewModel2 { + static let = "A" + } + """ + + let output = """ + import Foundation + + enum MyViewModel2 { + static let = "A" + } + """ + + testFormatting(for: input, output, rule: FormatRules.enumNamespaces) + } + + func testEnumNamespacesAfterImport2() { + // https://github.com/nicklockwood/SwiftFormat/issues/1569 + let input = """ + final class MyViewModel { + static let = "A" + } + + import Foundation + + final class MyViewModel2 { + static let = "A" + } + """ + + let output = """ + enum MyViewModel { + static let = "A" + } + + import Foundation + + enum MyViewModel2 { + static let = "A" + } + """ + + testFormatting(for: input, output, rule: FormatRules.enumNamespaces) + } + // MARK: - numberFormatting // hex case