Skip to content

Commit

Permalink
Add missing public access modifier check
Browse files Browse the repository at this point in the history
  • Loading branch information
hainayanda committed Apr 7, 2024
1 parent 9a2aab8 commit 8ef24f2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
11 changes: 8 additions & 3 deletions Sources/SwiftEnvironmentMacro/EnvironmentValueMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -55,15 +59,16 @@ 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
guard let name = variable.name,
let type = variable.typeAnnotation ?? variable.initializer else {
return nil
}
return EnvironmentDeclaration(baseName: name, type: type)
return EnvironmentDeclaration(isPublic: isPublic, baseName: name, type: type)

}
}
Expand Down
22 changes: 11 additions & 11 deletions Tests/SwiftEnvironmentTests/EnvironmentValueMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 8ef24f2

Please sign in to comment.