Skip to content

Commit f36187b

Browse files
committed
Refactor EquatableMacroInfo type
1 parent 6e36f09 commit f36187b

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

Sources/OptionDescriptor.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1247,11 +1247,11 @@ struct _Descriptors {
12471247
help: "Sort SwiftUI props: none, alphabetize, first-appearance-sort",
12481248
keyPath: \.swiftUIPropertiesSortMode
12491249
)
1250-
let equatableMacroInfo = OptionDescriptor(
1250+
let equatableMacro = OptionDescriptor(
12511251
argumentName: "equatablemacro",
12521252
displayName: "The name and module of an Equatable conformance macro",
12531253
help: "For example: \"@Equatable,EquatableMacroLib\"",
1254-
keyPath: \.equatableMacroInfo
1254+
keyPath: \.equatableMacro
12551255
)
12561256
let preferFileMacro = OptionDescriptor(
12571257
argumentName: "filemacro",

Sources/Options.swift

+25-12
Original file line numberDiff line numberDiff line change
@@ -590,21 +590,34 @@ public enum SwiftUIPropertiesSortMode: String, CaseIterable {
590590
case firstAppearanceSort = "first-appearance-sort"
591591
}
592592

593-
public struct EquatableMacroInfo: RawRepresentable {
594-
/// The name of this macro, e.g. `@Equatable`
595-
let macro: String
596-
/// The name of the module defining this macro, e.g. `EquatableMacroLib`
597-
let moduleName: String
593+
public enum EquatableMacro: Equatable, RawRepresentable, CustomStringConvertible {
594+
/// No equatable macro
595+
case none
596+
/// The name and the module for the macro, e.g. `@Equatable,EquatableMacroLib`
597+
case macro(String, module: String)
598598

599599
public init?(rawValue: String) {
600600
let components = rawValue.components(separatedBy: ",")
601-
guard components.count == 2 else { return nil }
602-
macro = components[0]
603-
moduleName = components[1]
601+
if components.count == 2 {
602+
self = .macro(components[0], module: components[1])
603+
} else if rawValue == "none" {
604+
self = .none
605+
} else {
606+
return nil
607+
}
604608
}
605609

606610
public var rawValue: String {
607-
"\(macro),\(moduleName)"
611+
switch self {
612+
case .none:
613+
return "none"
614+
case let .macro(name, module: module):
615+
return "\(name),\(module)"
616+
}
617+
}
618+
619+
public var description: String {
620+
rawValue
608621
}
609622
}
610623

@@ -718,7 +731,7 @@ public struct FormatOptions: CustomStringConvertible {
718731
public var timeZone: FormatTimeZone
719732
public var nilInit: NilInitType
720733
public var preservedPrivateDeclarations: Set<String>
721-
public var equatableMacroInfo: EquatableMacroInfo?
734+
public var equatableMacro: EquatableMacro
722735
public var preferFileMacro: Bool
723736

724737
/// Deprecated
@@ -846,7 +859,7 @@ public struct FormatOptions: CustomStringConvertible {
846859
timeZone: FormatTimeZone = .system,
847860
nilInit: NilInitType = .remove,
848861
preservedPrivateDeclarations: Set<String> = [],
849-
equatableMacroInfo: EquatableMacroInfo? = nil,
862+
equatableMacro: EquatableMacro = .none,
850863
preferFileMacro: Bool = true,
851864
// Doesn't really belong here, but hard to put elsewhere
852865
fragment: Bool = false,
@@ -964,7 +977,7 @@ public struct FormatOptions: CustomStringConvertible {
964977
self.timeZone = timeZone
965978
self.nilInit = nilInit
966979
self.preservedPrivateDeclarations = preservedPrivateDeclarations
967-
self.equatableMacroInfo = equatableMacroInfo
980+
self.equatableMacro = equatableMacro
968981
self.preferFileMacro = preferFileMacro
969982
// Doesn't really belong here, but hard to put elsewhere
970983
self.fragment = fragment

Sources/Rules/RedundantEquatable.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public extension FormatRule {
1919
isEligibleForAutoEquatableConformance = true
2020
case "class":
2121
// Projects can define an `@Equatable` macro that generates the Equatable implementation for classes
22-
isEligibleForAutoEquatableConformance = formatter.options.equatableMacroInfo != nil
22+
isEligibleForAutoEquatableConformance = formatter.options.equatableMacro != .none
2323
default:
2424
// This rule doesn't support other kinds of types.
2525
isEligibleForAutoEquatableConformance = false
@@ -49,7 +49,7 @@ public extension FormatRule {
4949

5050
// In projects using an `@Equatable` macro, the Equatable implementation
5151
// can be generated by that macro instead of written manually.
52-
else if let equatableMacroInfo = formatter.options.equatableMacroInfo {
52+
else if case let .macro(macro, module: module) = formatter.options.equatableMacro {
5353
let declarationWithEquatableConformance = equatableType.declarationWithEquatableConformance
5454

5555
guard let equatableConformance = formatter.parseConformancesOfType(atKeywordIndex: declarationWithEquatableConformance.keywordIndex).first(where: { $0.conformance == "Equatable" || $0.conformance == "Hashable" })
@@ -74,12 +74,12 @@ public extension FormatRule {
7474

7575
// Add the `@Equatable` macro
7676
formatter.insert(
77-
[.keyword(equatableMacroInfo.macro), .space(" ")],
77+
[.keyword(macro), .space(" ")],
7878
at: equatableType.typeDeclaration.startOfModifiersIndex
7979
)
8080

8181
// Import the module that defines the `@Equatable` macro if needed
82-
formatter.addImports([equatableMacroInfo.moduleName])
82+
formatter.addImports([module])
8383
}
8484
}
8585
} examples: {

Tests/Rules/RedundantEquatableTests.swift

+9-6
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ final class RedundantEquatableTests: XCTestCase {
199199

200200
let options = FormatOptions(
201201
typeAttributes: .prevLine,
202-
equatableMacroInfo: EquatableMacroInfo(rawValue: "@Equatable,MyEquatableMacroLib")
202+
equatableMacro: .macro("@Equatable", module: "MyEquatableMacroLib")
203203
)
204204

205205
testFormatting(
@@ -248,7 +248,7 @@ final class RedundantEquatableTests: XCTestCase {
248248

249249
let options = FormatOptions(
250250
typeAttributes: .prevLine,
251-
equatableMacroInfo: EquatableMacroInfo(rawValue: "@Equatable,MyEquatableMacroLib")
251+
equatableMacro: .macro("@Equatable", module: "MyEquatableMacroLib")
252252
)
253253

254254
testFormatting(for: input, rule: .redundantEquatable, options: options)
@@ -286,7 +286,10 @@ final class RedundantEquatableTests: XCTestCase {
286286
}
287287
"""
288288

289-
let options = FormatOptions(typeAttributes: .prevLine, equatableMacroInfo: EquatableMacroInfo(rawValue: "@Equatable,MyEquatableMacroLib"))
289+
let options = FormatOptions(
290+
typeAttributes: .prevLine,
291+
equatableMacro: .macro("@Equatable", module: "MyEquatableMacroLib")
292+
)
290293
testFormatting(for: input, [output], rules: [.redundantEquatable, .blankLinesAtEndOfScope, .wrapAttributes], options: options)
291294
}
292295

@@ -321,7 +324,7 @@ final class RedundantEquatableTests: XCTestCase {
321324

322325
let options = FormatOptions(
323326
typeAttributes: .prevLine,
324-
equatableMacroInfo: EquatableMacroInfo(rawValue: "@Equatable,MyEquatableMacroLib")
327+
equatableMacro: .macro("@Equatable", module: "MyEquatableMacroLib")
325328
)
326329

327330
testFormatting(
@@ -367,7 +370,7 @@ final class RedundantEquatableTests: XCTestCase {
367370

368371
let options = FormatOptions(
369372
typeAttributes: .prevLine,
370-
equatableMacroInfo: EquatableMacroInfo(rawValue: "@Equatable,MyEquatableMacroLib")
373+
equatableMacro: .macro("@Equatable", module: "MyEquatableMacroLib")
371374
)
372375

373376
testFormatting(
@@ -510,7 +513,7 @@ final class RedundantEquatableTests: XCTestCase {
510513

511514
let options = FormatOptions(
512515
typeAttributes: .prevLine,
513-
equatableMacroInfo: EquatableMacroInfo(rawValue: "@Equatable,MyEquatableMacroLib")
516+
equatableMacro: .macro("@Equatable", module: "MyEquatableMacroLib")
514517
)
515518

516519
testFormatting(for: input, [output], rules: [.redundantEquatable, .emptyBraces, .wrapAttributes, .emptyExtensions, .consecutiveBlankLines], options: options)

0 commit comments

Comments
 (0)