From 21129a6a68ea063996ed5e7280509e4bed002036 Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Fri, 3 Jan 2025 11:38:54 -0800 Subject: [PATCH] Fix issue where organizeDeclarations rule would unexpectedly remove non-mark comment (#1956) --- Sources/Rules/OrganizeDeclarations.swift | 3 +-- Tests/Rules/OrganizeDeclarationsTests.swift | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Sources/Rules/OrganizeDeclarations.swift b/Sources/Rules/OrganizeDeclarations.swift index f9fd123dc..f37e590b7 100644 --- a/Sources/Rules/OrganizeDeclarations.swift +++ b/Sources/Rules/OrganizeDeclarations.swift @@ -524,8 +524,7 @@ extension Formatter { Array(Set([ // The user's specific category separator template $0.markCommentBody(from: options.categoryMarkComment, with: options.organizationMode), - // Other common variants that we would want to replace with the correct variant - $0.markCommentBody(from: "%c", with: options.organizationMode), + // Always look for MARKs even if the user is using a different template $0.markCommentBody(from: "MARK: %c", with: options.organizationMode), ])) }.compactMap { $0 } diff --git a/Tests/Rules/OrganizeDeclarationsTests.swift b/Tests/Rules/OrganizeDeclarationsTests.swift index 8a337568a..53a963eaf 100644 --- a/Tests/Rules/OrganizeDeclarationsTests.swift +++ b/Tests/Rules/OrganizeDeclarationsTests.swift @@ -1568,9 +1568,9 @@ class OrganizeDeclarationsTests: XCTestCase { init() {} - // Public + // mark: Public - // - Public + // mark - Public public func bar() {} @@ -3593,4 +3593,17 @@ class OrganizeDeclarationsTests: XCTestCase { testFormatting(for: input, output, rule: .organizeDeclarations, exclude: [.blankLinesAtStartOfScope]) } + + func testPreservesUnrelatedComments() { + let input = """ + enum Test { + /// Test Properties + static let foo = "foo" + static let bar = "bar" + static let baaz = "baaz" + } + """ + + testFormatting(for: input, rule: .organizeDeclarations) + } }