From 92e79295c12a1a81f8c1047e7d793606e76e2ef0 Mon Sep 17 00:00:00 2001 From: Rob Amos Date: Sat, 14 Dec 2024 22:35:13 +1100 Subject: [PATCH 1/2] Made sure that our generated wigwags matched the scope of the properties they are attached to --- Sources/VexilMacros/FlagContainerMacro.swift | 2 +- Sources/VexilMacros/FlagGroupMacro.swift | 29 +++++++++------ Sources/VexilMacros/FlagMacro.swift | 31 +++++++++------- .../VexilMacroTests/FlagGroupMacroTests.swift | 35 +++++++++++++++++++ Tests/VexilMacroTests/FlagMacroTests.swift | 35 +++++++++++++++++++ 5 files changed, 108 insertions(+), 24 deletions(-) diff --git a/Sources/VexilMacros/FlagContainerMacro.swift b/Sources/VexilMacros/FlagContainerMacro.swift index 9b21dae7..32feff51 100644 --- a/Sources/VexilMacros/FlagContainerMacro.swift +++ b/Sources/VexilMacros/FlagContainerMacro.swift @@ -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 { diff --git a/Sources/VexilMacros/FlagGroupMacro.swift b/Sources/VexilMacros/FlagGroupMacro.swift index 47341aaf..6a2a984b 100644 --- a/Sources/VexilMacros/FlagGroupMacro.swift +++ b/Sources/VexilMacros/FlagGroupMacro.swift @@ -25,6 +25,7 @@ public struct FlagGroupMacro { let description: ExprSyntax? let displayOption: ExprSyntax? let type: TypeSyntax + let scopes: DeclModifierListSyntax // MARK: - Initialisation @@ -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 @@ -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 { @@ -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 [] diff --git a/Sources/VexilMacros/FlagMacro.swift b/Sources/VexilMacros/FlagMacro.swift index 6bdabee4..4482ae64 100644 --- a/Sources/VexilMacros/FlagMacro.swift +++ b/Sources/VexilMacros/FlagMacro.swift @@ -27,6 +27,7 @@ public struct FlagMacro { let description: ExprSyntax let display: ExprSyntax? let type: TypeSyntax + let scopes: DeclModifierListSyntax // MARK: - Initialisation @@ -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 { @@ -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 { @@ -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 [] diff --git a/Tests/VexilMacroTests/FlagGroupMacroTests.swift b/Tests/VexilMacroTests/FlagGroupMacroTests.swift index a164c2b5..ae68b8c6 100644 --- a/Tests/VexilMacroTests/FlagGroupMacroTests.swift +++ b/Tests/VexilMacroTests/FlagGroupMacroTests.swift @@ -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 { + 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 { diff --git a/Tests/VexilMacroTests/FlagMacroTests.swift b/Tests/VexilMacroTests/FlagMacroTests.swift index c309f050..7a7dc606 100644 --- a/Tests/VexilMacroTests/FlagMacroTests.swift +++ b/Tests/VexilMacroTests/FlagMacroTests.swift @@ -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 { + 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 From 7043c7686129adef9a3c4de082d58682c1592184 Mon Sep 17 00:00:00 2001 From: Rob Amos Date: Sat, 14 Dec 2024 22:37:49 +1100 Subject: [PATCH 2/2] Formatting --- Sources/VexilMacros/FlagGroupMacro.swift | 6 +++--- Sources/VexilMacros/FlagMacro.swift | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/VexilMacros/FlagGroupMacro.swift b/Sources/VexilMacros/FlagGroupMacro.swift index 6a2a984b..97e440fb 100644 --- a/Sources/VexilMacros/FlagGroupMacro.swift +++ b/Sources/VexilMacros/FlagGroupMacro.swift @@ -93,7 +93,7 @@ public struct FlagGroupMacro { } func makeWigwagDeclaration() throws -> VariableDeclSyntax { - return try VariableDeclSyntax("var $\(raw: propertyName): FlagGroupWigwag<\(type)>") { + try VariableDeclSyntax("var $\(raw: propertyName): FlagGroupWigwag<\(type)>") { """ FlagGroupWigwag( keyPath: \(key), @@ -136,8 +136,8 @@ extension FlagGroupMacro: PeerMacro { ) throws -> [DeclSyntax] { do { let macro = try FlagGroupMacro(node: node, declaration: declaration, context: context) - return [ - try DeclSyntax(macro.makeWigwagDeclaration()), + return try [ + DeclSyntax(macro.makeWigwagDeclaration()), ] } catch { return [] diff --git a/Sources/VexilMacros/FlagMacro.swift b/Sources/VexilMacros/FlagMacro.swift index 4482ae64..211e62fe 100644 --- a/Sources/VexilMacros/FlagMacro.swift +++ b/Sources/VexilMacros/FlagMacro.swift @@ -103,7 +103,7 @@ public struct FlagMacro { } func makeWigwagDeclaration() throws -> VariableDeclSyntax { - return try VariableDeclSyntax("var $\(raw: propertyName): FlagWigwag<\(type)>") { + try VariableDeclSyntax("var $\(raw: propertyName): FlagWigwag<\(type)>") { """ FlagWigwag( keyPath: \(key), @@ -171,8 +171,8 @@ extension FlagMacro: PeerMacro { ) throws -> [DeclSyntax] { do { let macro = try FlagMacro(node: node, declaration: declaration, context: context) - return [ - try DeclSyntax(macro.makeWigwagDeclaration()), + return try [ + DeclSyntax(macro.makeWigwagDeclaration()), ] } catch { return []