Skip to content

Commit

Permalink
Ensures that the DependencyClient macro doesn't attempt to do anythin…
Browse files Browse the repository at this point in the history
…g with static properties (#196)
  • Loading branch information
rhysm94 authored Mar 15, 2024
1 parent d3a5af3 commit 083ff59
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "23cbf2294e350076ea4dbd7d5d047c1e76b03631",
"version" : "1.0.2"
"revision" : "b13b1d1a8e787a5ffc71ac19dcaf52183ab27ba2",
"version" : "1.1.1"
}
}
],
Expand Down
12 changes: 11 additions & 1 deletion Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public enum DependencyClientMacro: MemberAttributeMacro, MemberMacro {
var hasEndpoints = false
var accesses: Set<Access> = Access(modifiers: declaration.modifiers).map { [$0] } ?? []
for member in declaration.memberBlock.members {
guard var property = member.decl.as(VariableDeclSyntax.self) else { continue }
guard
var property = member.decl.as(VariableDeclSyntax.self),
!property.isStatic
else { continue }

let isEndpoint =
property.hasDependencyEndpointMacroAttached
|| property.bindingSpecifier.tokenKind != .keyword(.let) && property.isClosure
Expand Down Expand Up @@ -249,6 +253,12 @@ private struct Property {
}

extension VariableDeclSyntax {
fileprivate var isStatic: Bool {
self.modifiers.contains { modifier in
modifier.name.tokenKind == .keyword(.static)
}
}

fileprivate var hasDependencyEndpointMacroAttached: Bool {
self.attributes.contains {
guard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,35 @@ final class DependencyClientMacroTests: BaseTestCase {
}
}

func testStaticVar() {
assertMacro {
"""
@DependencyClient
struct Client {
var config: () -> Void
static var value = Client()
}
"""
} expansion: {
"""
struct Client {
@DependencyEndpoint
var config: () -> Void
static var value = Client()
init(
config: @escaping () -> Void
) {
self.config = config
}
init() {
}
}
"""
}
}

func testDefaultValue() {
assertMacro {
"""
Expand Down

0 comments on commit 083ff59

Please sign in to comment.