Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/VexilMacros/FlagContainerMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ extension FlagContainerMacro: ExtensionMacro {

// MARK: - Scopes

private extension DeclModifierListSyntax {
extension DeclModifierListSyntax {
var scopeSyntax: DeclModifierListSyntax {
filter { modifier in
if case let .keyword(keyword) = modifier.name.tokenKind, keyword == .public {
Expand Down
31 changes: 19 additions & 12 deletions Sources/VexilMacros/FlagGroupMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct FlagGroupMacro {
let description: ExprSyntax?
let displayOption: ExprSyntax?
let type: TypeSyntax
let scopes: DeclModifierListSyntax


// MARK: - Initialisation
Expand Down Expand Up @@ -52,6 +53,7 @@ public struct FlagGroupMacro {
self.propertyName = identifier.text
self.key = strategy.createKey(propertyName)
self.type = type
self.scopes = property.modifiers.scopeSyntax

self.name = arguments[label: "name"]?.expression
self.description = arguments[label: "description"]?.expression
Expand Down Expand Up @@ -90,6 +92,21 @@ public struct FlagGroupMacro {
}
}

func makeWigwagDeclaration() throws -> VariableDeclSyntax {
try VariableDeclSyntax("var $\(raw: propertyName): FlagGroupWigwag<\(type)>") {
"""
FlagGroupWigwag(
keyPath: \(key),
name: \(name ?? "nil"),
description: \(description ?? "nil"),
displayOption: \(displayOption ?? ".navigation"),
lookup: _flagLookup
)
"""
}
.with(\.modifiers, scopes)
}

}

extension FlagGroupMacro: AccessorMacro {
Expand Down Expand Up @@ -119,18 +136,8 @@ extension FlagGroupMacro: PeerMacro {
) throws -> [DeclSyntax] {
do {
let macro = try FlagGroupMacro(node: node, declaration: declaration, context: context)
return [
"""
var $\(raw: macro.propertyName): FlagGroupWigwag<\(macro.type)> {
FlagGroupWigwag(
keyPath: \(macro.key),
name: \(macro.name ?? "nil"),
description: \(macro.description ?? "nil"),
displayOption: \(macro.displayOption ?? ".navigation"),
lookup: _flagLookup
)
}
""",
return try [
DeclSyntax(macro.makeWigwagDeclaration()),
]
} catch {
return []
Expand Down
33 changes: 20 additions & 13 deletions Sources/VexilMacros/FlagMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public struct FlagMacro {
let description: ExprSyntax
let display: ExprSyntax?
let type: TypeSyntax
let scopes: DeclModifierListSyntax


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

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

func makeWigwagDeclaration() throws -> VariableDeclSyntax {
try VariableDeclSyntax("var $\(raw: propertyName): FlagWigwag<\(type)>") {
"""
FlagWigwag(
keyPath: \(key),
name: \(name ?? "nil"),
defaultValue: \(defaultValue),
description: \(description),
displayOption: \(display ?? ".default"),
lookup: _flagLookup
)
"""
}
.with(\.modifiers, scopes)
}

}

private extension AttributeSyntax.Arguments {
Expand Down Expand Up @@ -153,19 +171,8 @@ extension FlagMacro: PeerMacro {
) throws -> [DeclSyntax] {
do {
let macro = try FlagMacro(node: node, declaration: declaration, context: context)
return [
"""
var $\(raw: macro.propertyName): FlagWigwag<\(macro.type)> {
FlagWigwag(
keyPath: \(macro.key),
name: \(macro.name ?? "nil"),
defaultValue: \(macro.defaultValue),
description: \(macro.description),
displayOption: \(macro.display ?? ".default"),
lookup: _flagLookup
)
}
""",
return try [
DeclSyntax(macro.makeWigwagDeclaration()),
]
} catch {
return []
Expand Down
35 changes: 35 additions & 0 deletions Tests/VexilMacroTests/FlagGroupMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,41 @@ final class FlagGroupMacroTests: XCTestCase {
)
}

func testExpandsPublic() throws {
assertMacroExpansion(
"""
struct TestFlags {
@FlagGroup(description: "Test Flag Group")
public var testSubgroup: SubgroupFlags
}
""",
expandedSource:
"""
struct TestFlags {
public var testSubgroup: SubgroupFlags {
get {
SubgroupFlags(_flagKeyPath: _flagKeyPath.append(.automatic("test-subgroup")), _flagLookup: _flagLookup)
}
}

public var $testSubgroup: FlagGroupWigwag<SubgroupFlags> {
FlagGroupWigwag(
keyPath: _flagKeyPath.append(.automatic("test-subgroup")),
name: nil,
description: "Test Flag Group",
displayOption: .navigation,
lookup: _flagLookup
)
}
}
""",
macros: [
"FlagGroup": FlagGroupMacro.self,
]
)
}


// MARK: - Flag Group Detail Tests

func testExpandsName() throws {
Expand Down
35 changes: 35 additions & 0 deletions Tests/VexilMacroTests/FlagMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,41 @@ final class FlagMacroTests: XCTestCase {
)
}

func testExpandsPublic() throws {
assertMacroExpansion(
"""
struct TestFlags {
@Flag(default: false, description: "meow")
public var testProperty: Bool
}
""",
expandedSource:
"""
struct TestFlags {
public var testProperty: Bool {
get {
_flagLookup.value(for: _flagKeyPath.append(.automatic("test-property"))) ?? false
}
}

public var $testProperty: FlagWigwag<Bool> {
FlagWigwag(
keyPath: _flagKeyPath.append(.automatic("test-property")),
name: nil,
defaultValue: false,
description: "meow",
displayOption: .default,
lookup: _flagLookup
)
}
}
""",
macros: [
"Flag": FlagMacro.self,
]
)
}


// MARK: - Property Initialisation Tests

Expand Down
Loading