Skip to content

Commit 21bd870

Browse files
authored
Merge pull request #134 from unsignedapps/public-wigwags
Support public Wigwags
2 parents 01ed3f5 + 7043c76 commit 21bd870

File tree

5 files changed

+110
-26
lines changed

5 files changed

+110
-26
lines changed

Sources/VexilMacros/FlagContainerMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ extension FlagContainerMacro: ExtensionMacro {
186186

187187
// MARK: - Scopes
188188

189-
private extension DeclModifierListSyntax {
189+
extension DeclModifierListSyntax {
190190
var scopeSyntax: DeclModifierListSyntax {
191191
filter { modifier in
192192
if case let .keyword(keyword) = modifier.name.tokenKind, keyword == .public {

Sources/VexilMacros/FlagGroupMacro.swift

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public struct FlagGroupMacro {
2525
let description: ExprSyntax?
2626
let displayOption: ExprSyntax?
2727
let type: TypeSyntax
28+
let scopes: DeclModifierListSyntax
2829

2930

3031
// MARK: - Initialisation
@@ -52,6 +53,7 @@ public struct FlagGroupMacro {
5253
self.propertyName = identifier.text
5354
self.key = strategy.createKey(propertyName)
5455
self.type = type
56+
self.scopes = property.modifiers.scopeSyntax
5557

5658
self.name = arguments[label: "name"]?.expression
5759
self.description = arguments[label: "description"]?.expression
@@ -90,6 +92,21 @@ public struct FlagGroupMacro {
9092
}
9193
}
9294

95+
func makeWigwagDeclaration() throws -> VariableDeclSyntax {
96+
try VariableDeclSyntax("var $\(raw: propertyName): FlagGroupWigwag<\(type)>") {
97+
"""
98+
FlagGroupWigwag(
99+
keyPath: \(key),
100+
name: \(name ?? "nil"),
101+
description: \(description ?? "nil"),
102+
displayOption: \(displayOption ?? ".navigation"),
103+
lookup: _flagLookup
104+
)
105+
"""
106+
}
107+
.with(\.modifiers, scopes)
108+
}
109+
93110
}
94111

95112
extension FlagGroupMacro: AccessorMacro {
@@ -119,18 +136,8 @@ extension FlagGroupMacro: PeerMacro {
119136
) throws -> [DeclSyntax] {
120137
do {
121138
let macro = try FlagGroupMacro(node: node, declaration: declaration, context: context)
122-
return [
123-
"""
124-
var $\(raw: macro.propertyName): FlagGroupWigwag<\(macro.type)> {
125-
FlagGroupWigwag(
126-
keyPath: \(macro.key),
127-
name: \(macro.name ?? "nil"),
128-
description: \(macro.description ?? "nil"),
129-
displayOption: \(macro.displayOption ?? ".navigation"),
130-
lookup: _flagLookup
131-
)
132-
}
133-
""",
139+
return try [
140+
DeclSyntax(macro.makeWigwagDeclaration()),
134141
]
135142
} catch {
136143
return []

Sources/VexilMacros/FlagMacro.swift

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public struct FlagMacro {
2727
let description: ExprSyntax
2828
let display: ExprSyntax?
2929
let type: TypeSyntax
30+
let scopes: DeclModifierListSyntax
3031

3132

3233
// MARK: - Initialisation
@@ -54,6 +55,7 @@ public struct FlagMacro {
5455
else {
5556
throw DiagnosticsError(diagnostics: [ .init(node: node, message: Diagnostic.onlySimpleVariableSupported) ])
5657
}
58+
self.scopes = property.modifiers.scopeSyntax
5759

5860
var defaultExprSyntax: ExprSyntax
5961
if let defaultExpr = arguments[label: "default"]?.expression ?? binding.initializer?.value {
@@ -100,6 +102,22 @@ public struct FlagMacro {
100102
"""
101103
}
102104

105+
func makeWigwagDeclaration() throws -> VariableDeclSyntax {
106+
try VariableDeclSyntax("var $\(raw: propertyName): FlagWigwag<\(type)>") {
107+
"""
108+
FlagWigwag(
109+
keyPath: \(key),
110+
name: \(name ?? "nil"),
111+
defaultValue: \(defaultValue),
112+
description: \(description),
113+
displayOption: \(display ?? ".default"),
114+
lookup: _flagLookup
115+
)
116+
"""
117+
}
118+
.with(\.modifiers, scopes)
119+
}
120+
103121
}
104122

105123
private extension AttributeSyntax.Arguments {
@@ -153,19 +171,8 @@ extension FlagMacro: PeerMacro {
153171
) throws -> [DeclSyntax] {
154172
do {
155173
let macro = try FlagMacro(node: node, declaration: declaration, context: context)
156-
return [
157-
"""
158-
var $\(raw: macro.propertyName): FlagWigwag<\(macro.type)> {
159-
FlagWigwag(
160-
keyPath: \(macro.key),
161-
name: \(macro.name ?? "nil"),
162-
defaultValue: \(macro.defaultValue),
163-
description: \(macro.description),
164-
displayOption: \(macro.display ?? ".default"),
165-
lookup: _flagLookup
166-
)
167-
}
168-
""",
174+
return try [
175+
DeclSyntax(macro.makeWigwagDeclaration()),
169176
]
170177
} catch {
171178
return []

Tests/VexilMacroTests/FlagGroupMacroTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,41 @@ final class FlagGroupMacroTests: XCTestCase {
5454
)
5555
}
5656

57+
func testExpandsPublic() throws {
58+
assertMacroExpansion(
59+
"""
60+
struct TestFlags {
61+
@FlagGroup(description: "Test Flag Group")
62+
public var testSubgroup: SubgroupFlags
63+
}
64+
""",
65+
expandedSource:
66+
"""
67+
struct TestFlags {
68+
public var testSubgroup: SubgroupFlags {
69+
get {
70+
SubgroupFlags(_flagKeyPath: _flagKeyPath.append(.automatic("test-subgroup")), _flagLookup: _flagLookup)
71+
}
72+
}
73+
74+
public var $testSubgroup: FlagGroupWigwag<SubgroupFlags> {
75+
FlagGroupWigwag(
76+
keyPath: _flagKeyPath.append(.automatic("test-subgroup")),
77+
name: nil,
78+
description: "Test Flag Group",
79+
displayOption: .navigation,
80+
lookup: _flagLookup
81+
)
82+
}
83+
}
84+
""",
85+
macros: [
86+
"FlagGroup": FlagGroupMacro.self,
87+
]
88+
)
89+
}
90+
91+
5792
// MARK: - Flag Group Detail Tests
5893

5994
func testExpandsName() throws {

Tests/VexilMacroTests/FlagMacroTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,41 @@ final class FlagMacroTests: XCTestCase {
197197
)
198198
}
199199

200+
func testExpandsPublic() throws {
201+
assertMacroExpansion(
202+
"""
203+
struct TestFlags {
204+
@Flag(default: false, description: "meow")
205+
public var testProperty: Bool
206+
}
207+
""",
208+
expandedSource:
209+
"""
210+
struct TestFlags {
211+
public var testProperty: Bool {
212+
get {
213+
_flagLookup.value(for: _flagKeyPath.append(.automatic("test-property"))) ?? false
214+
}
215+
}
216+
217+
public var $testProperty: FlagWigwag<Bool> {
218+
FlagWigwag(
219+
keyPath: _flagKeyPath.append(.automatic("test-property")),
220+
name: nil,
221+
defaultValue: false,
222+
description: "meow",
223+
displayOption: .default,
224+
lookup: _flagLookup
225+
)
226+
}
227+
}
228+
""",
229+
macros: [
230+
"Flag": FlagMacro.self,
231+
]
232+
)
233+
}
234+
200235

201236
// MARK: - Property Initialisation Tests
202237

0 commit comments

Comments
 (0)