diff --git a/Sources/SwiftEnvironmentMacro/EnvironmentValueMacro.swift b/Sources/SwiftEnvironmentMacro/EnvironmentValueMacro.swift index 43a6fa1..eb5780a 100644 --- a/Sources/SwiftEnvironmentMacro/EnvironmentValueMacro.swift +++ b/Sources/SwiftEnvironmentMacro/EnvironmentValueMacro.swift @@ -26,17 +26,21 @@ public struct EnvironmentValueMacro: MemberMacro { } struct EnvironmentDeclaration: CustomStringConvertible { + let isPublic: Bool let baseName: String let type: String var derivedName: String { "\(baseName.firstCapitalized)SwiftEnvironmentKey" } + var accessModifier: String { + isPublic ? "public " : "" + } var description: String { """ struct \(derivedName): EnvironmentKey { - static let defaultValue: \(type) = EnvironmentValues.\(baseName) + \(accessModifier)static let defaultValue: \(type) = EnvironmentValues.\(baseName) } var \(baseName): \(type) { @@ -55,7 +59,8 @@ struct EnvironmentDeclaration: CustomStringConvertible { private extension ExtensionDeclSyntax { var environmentDeclaration: [EnvironmentDeclaration] { - memberBlock.members + let isPublic = self.modifiers.contains { $0.trimmedDescription == "public" } + return memberBlock.members .compactMap { $0.decl.as(VariableDeclSyntax.self) } .filter { $0.isStatic } .compactMap { variable in @@ -63,7 +68,7 @@ private extension ExtensionDeclSyntax { let type = variable.typeAnnotation ?? variable.initializer else { return nil } - return EnvironmentDeclaration(baseName: name, type: type) + return EnvironmentDeclaration(isPublic: isPublic, baseName: name, type: type) } } diff --git a/Tests/SwiftEnvironmentTests/EnvironmentValueMacroTests.swift b/Tests/SwiftEnvironmentTests/EnvironmentValueMacroTests.swift index 1a21321..455d6f6 100644 --- a/Tests/SwiftEnvironmentTests/EnvironmentValueMacroTests.swift +++ b/Tests/SwiftEnvironmentTests/EnvironmentValueMacroTests.swift @@ -49,18 +49,18 @@ final class EnvironmentValueMacroTests: XCTestCase { private let oneImplicitArg: String = """ @EnvironmentValue -extension EnvironmentValues { +public extension EnvironmentValues { static let dummy = Some.Dependency() } """ private let oneImplicitArgExpansion: String = """ -extension EnvironmentValues { +public extension EnvironmentValues { static let dummy = Some.Dependency() struct DummySwiftEnvironmentKey: EnvironmentKey { - static let defaultValue: Some.Dependency = EnvironmentValues.dummy + public static let defaultValue: Some.Dependency = EnvironmentValues.dummy } var dummy: Some.Dependency { @@ -103,18 +103,18 @@ extension EnvironmentValues { private let oneArg: String = """ @EnvironmentValue -extension EnvironmentValues { +public extension EnvironmentValues { static let dummy: DummyDependency = DummyDependency() } """ private let oneArgExpansion: String = """ -extension EnvironmentValues { +public extension EnvironmentValues { static let dummy: DummyDependency = DummyDependency() struct DummySwiftEnvironmentKey: EnvironmentKey { - static let defaultValue: DummyDependency = EnvironmentValues.dummy + public static let defaultValue: DummyDependency = EnvironmentValues.dummy } var dummy: DummyDependency { @@ -157,7 +157,7 @@ extension EnvironmentValues { private let multiArgs: String = """ @EnvironmentValue("one", "two", "three") -extension EnvironmentValues { +public extension EnvironmentValues { static let one = DummyDependency() static let two = DummyDependency() static let three = DummyDependency() @@ -166,13 +166,13 @@ extension EnvironmentValues { private let multiArgsExpansion: String = """ -extension EnvironmentValues { +public extension EnvironmentValues { static let one = DummyDependency() static let two = DummyDependency() static let three = DummyDependency() struct OneSwiftEnvironmentKey: EnvironmentKey { - static let defaultValue: DummyDependency = EnvironmentValues.one + public static let defaultValue: DummyDependency = EnvironmentValues.one } var one: DummyDependency { @@ -185,7 +185,7 @@ extension EnvironmentValues { } struct TwoSwiftEnvironmentKey: EnvironmentKey { - static let defaultValue: DummyDependency = EnvironmentValues.two + public static let defaultValue: DummyDependency = EnvironmentValues.two } var two: DummyDependency { @@ -198,7 +198,7 @@ extension EnvironmentValues { } struct ThreeSwiftEnvironmentKey: EnvironmentKey { - static let defaultValue: DummyDependency = EnvironmentValues.three + public static let defaultValue: DummyDependency = EnvironmentValues.three } var three: DummyDependency {