Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assigned static properties #196

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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