Skip to content

Commit 6cee7ce

Browse files
authored
Merge pull request #113 from klaaspieter/fix-use-enum-if-config
Support IfConfigDecl clauses in UseEnumForNamespacing
2 parents 801d068 + 54e5a51 commit 6cee7ce

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

Sources/SwiftFormatRules/UseEnumForNamespacing.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ public final class UseEnumForNamespacing: SyntaxFormatRule {
7676
}
7777
// Do not append private initializer.
7878

79+
case let decl as IfConfigDeclSyntax:
80+
let membersToKeep: [MemberDeclListSyntax] = decl.clauses
81+
.compactMap { clause in
82+
(clause.elements as? MemberDeclListSyntax).flatMap(membersToKeepIfUsedAsNamespace(_:))
83+
}
84+
85+
if membersToKeep.count < decl.clauses.count {
86+
return nil
87+
} else {
88+
declList.append(member)
89+
}
90+
7991
default:
8092
declList.append(member)
8193
}

Tests/SwiftFormatRulesTests/UseEnumForNamespacingTests.swift

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,64 @@ public class UseEnumForNamespacingTests: DiagnosingTestCase {
2727
final class E {
2828
static let a = 123
2929
}
30+
struct Structure {
31+
#if canImport(AppKit)
32+
var native: NSSomething
33+
#elseif canImport(UIKit)
34+
var native: UISomething
35+
#endif
36+
}
37+
struct Structure {
38+
#if canImport(AppKit)
39+
static var native: NSSomething
40+
#elseif canImport(UIKit)
41+
static var native: UISomething
42+
#endif
43+
}
44+
struct Structure {
45+
#if canImport(AppKit)
46+
var native: NSSomething
47+
#elseif canImport(UIKit)
48+
static var native: UISomething
49+
#endif
50+
}
51+
struct Structure {
52+
#if canImport(AppKit)
53+
static var native: NSSomething
54+
#else
55+
static var native: UISomething
56+
#endif
57+
}
58+
struct Structure {
59+
#if canImport(AppKit)
60+
#if swift(>=4.0)
61+
static var native: NSSomething
62+
#else
63+
static var deprecated_native: NSSomething
64+
#endif
65+
#else
66+
#if swift(>=4.0)
67+
static var native: UISomething
68+
#else
69+
static var deprecated_native: UISomething
70+
#endif
71+
#endif
72+
}
73+
struct Structure {
74+
#if canImport(AppKit)
75+
#if swift(>=4.0)
76+
static var native: NSSomething
77+
#else
78+
static var deprecated_native: NSSomething
79+
#endif
80+
#else
81+
#if swift(>=4.0)
82+
static var native: UISomething
83+
#else
84+
var deprecated_native: UISomething
85+
#endif
86+
#endif
87+
}
3088
""",
3189
expected: """
3290
enum A {
@@ -46,6 +104,64 @@ public class UseEnumForNamespacingTests: DiagnosingTestCase {
46104
final class E {
47105
static let a = 123
48106
}
107+
struct Structure {
108+
#if canImport(AppKit)
109+
var native: NSSomething
110+
#elseif canImport(UIKit)
111+
var native: UISomething
112+
#endif
113+
}
114+
enum Structure {
115+
#if canImport(AppKit)
116+
static var native: NSSomething
117+
#elseif canImport(UIKit)
118+
static var native: UISomething
119+
#endif
120+
}
121+
struct Structure {
122+
#if canImport(AppKit)
123+
var native: NSSomething
124+
#elseif canImport(UIKit)
125+
static var native: UISomething
126+
#endif
127+
}
128+
enum Structure {
129+
#if canImport(AppKit)
130+
static var native: NSSomething
131+
#else
132+
static var native: UISomething
133+
#endif
134+
}
135+
enum Structure {
136+
#if canImport(AppKit)
137+
#if swift(>=4.0)
138+
static var native: NSSomething
139+
#else
140+
static var deprecated_native: NSSomething
141+
#endif
142+
#else
143+
#if swift(>=4.0)
144+
static var native: UISomething
145+
#else
146+
static var deprecated_native: UISomething
147+
#endif
148+
#endif
149+
}
150+
struct Structure {
151+
#if canImport(AppKit)
152+
#if swift(>=4.0)
153+
static var native: NSSomething
154+
#else
155+
static var deprecated_native: NSSomething
156+
#endif
157+
#else
158+
#if swift(>=4.0)
159+
static var native: UISomething
160+
#else
161+
var deprecated_native: UISomething
162+
#endif
163+
#endif
164+
}
49165
""")
50166
}
51167
}

0 commit comments

Comments
 (0)