Skip to content

Commit

Permalink
Fix issue where enumNamespaces rule wouldn't be applied following imp…
Browse files Browse the repository at this point in the history
…ort statement
  • Loading branch information
calda authored and nicklockwood committed Nov 10, 2023
1 parent a22fbc3 commit 1be4d8b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Snapshots/Layout/Layout/ReloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Foundation
import UIKit

class ReloadManager {
enum ReloadManager {
private static var boxes = [ObserverBox]()

private struct ObserverBox {
Expand Down
5 changes: 2 additions & 3 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(":"),
Expand Down
50 changes: 50 additions & 0 deletions Tests/RulesTests+Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1be4d8b

Please sign in to comment.