Skip to content

Commit 083ff59

Browse files
authored
Ensures that the DependencyClient macro doesn't attempt to do anything with static properties (#196)
1 parent d3a5af3 commit 083ff59

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public enum DependencyClientMacro: MemberAttributeMacro, MemberMacro {
8686
var hasEndpoints = false
8787
var accesses: Set<Access> = Access(modifiers: declaration.modifiers).map { [$0] } ?? []
8888
for member in declaration.memberBlock.members {
89-
guard var property = member.decl.as(VariableDeclSyntax.self) else { continue }
89+
guard
90+
var property = member.decl.as(VariableDeclSyntax.self),
91+
!property.isStatic
92+
else { continue }
93+
9094
let isEndpoint =
9195
property.hasDependencyEndpointMacroAttached
9296
|| property.bindingSpecifier.tokenKind != .keyword(.let) && property.isClosure
@@ -249,6 +253,12 @@ private struct Property {
249253
}
250254

251255
extension VariableDeclSyntax {
256+
fileprivate var isStatic: Bool {
257+
self.modifiers.contains { modifier in
258+
modifier.name.tokenKind == .keyword(.static)
259+
}
260+
}
261+
252262
fileprivate var hasDependencyEndpointMacroAttached: Bool {
253263
self.attributes.contains {
254264
guard

Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,35 @@ final class DependencyClientMacroTests: BaseTestCase {
276276
}
277277
}
278278

279+
func testStaticVar() {
280+
assertMacro {
281+
"""
282+
@DependencyClient
283+
struct Client {
284+
var config: () -> Void
285+
static var value = Client()
286+
}
287+
"""
288+
} expansion: {
289+
"""
290+
struct Client {
291+
@DependencyEndpoint
292+
var config: () -> Void
293+
static var value = Client()
294+
295+
init(
296+
config: @escaping () -> Void
297+
) {
298+
self.config = config
299+
}
300+
301+
init() {
302+
}
303+
}
304+
"""
305+
}
306+
}
307+
279308
func testDefaultValue() {
280309
assertMacro {
281310
"""

0 commit comments

Comments
 (0)