Skip to content

Commit

Permalink
Made sure that our generated wigwags matched the scope of the propert…
Browse files Browse the repository at this point in the history
…ies they are attached to
  • Loading branch information
bok- committed Dec 14, 2024
1 parent 01ed3f5 commit 92e7929
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 24 deletions.
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
29 changes: 18 additions & 11 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 {
return 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 @@ -120,17 +137,7 @@ extension FlagGroupMacro: PeerMacro {
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
)
}
""",
try DeclSyntax(macro.makeWigwagDeclaration()),
]
} catch {
return []
Expand Down
31 changes: 19 additions & 12 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 {
return 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 @@ -154,18 +172,7 @@ extension FlagMacro: PeerMacro {
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
)
}
""",
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

0 comments on commit 92e7929

Please sign in to comment.