Skip to content

Commit

Permalink
Merge pull request #3 from Ryu0118/fix-static-property
Browse files Browse the repository at this point in the history
Static properties are excluded from the initializer
  • Loading branch information
Ryu0118 committed Oct 2, 2023
2 parents a66faf7 + 505083e commit 09c6458
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Sources/PublicInitMacroPlugin/PublicInitMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public struct PublicInitMacro: MemberMacro {
) throws -> [DeclSyntax] {
let structDecl = try decodeExpansion(of: node, attachedTo: declaration, in: context)
let storedPropertyBindings = structDecl.memberBlock.members
.compactMap { $0.decl.as(VariableDeclSyntax.self)?.bindings }
.compactMap { $0.decl.as(VariableDeclSyntax.self) }
.filter { !$0.modifiers.contains { $0.as(DeclModifierSyntax.self)?.name.text == "static" } }
.map(\.bindings)
.flatMap { $0 }
.filter { $0.accessorBlock == nil }
.compactMap { (binding: PatternBindingSyntax) -> PatternBindingSyntax? in
Expand Down
8 changes: 7 additions & 1 deletion Tests/PublicInitMacroTests/PublicInitMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ final class PublicInitMacroTests: XCTestCase {
"""
@PublicInit
public struct Test {
static let staticValue = 0
let a: String
let b: () -> Void
let c: @Sendable () -> Void
Expand All @@ -101,11 +102,13 @@ final class PublicInitMacroTests: XCTestCase {
let g: @Sendable (_ arg: String) async throws -> String
var h: @Sendable (_ arg1: String, _ arg2: String) async throws -> String
var i: @Sendable (String, Int) async throws -> String
var j: String = ""
}
"""
} matches: {
"""
public struct Test {
static let staticValue = 0
let a: String
let b: () -> Void
let c: @Sendable () -> Void
Expand All @@ -115,6 +118,7 @@ final class PublicInitMacroTests: XCTestCase {
let g: @Sendable (_ arg: String) async throws -> String
var h: @Sendable (_ arg1: String, _ arg2: String) async throws -> String
var i: @Sendable (String, Int) async throws -> String
var j: String = ""
public init(
a: String,
Expand All @@ -125,7 +129,8 @@ final class PublicInitMacroTests: XCTestCase {
f: @Sendable @escaping (String) async throws -> String,
g: @Sendable @escaping (_ arg: String) async throws -> String,
h: @Sendable @escaping (_ arg1: String, _ arg2: String) async throws -> String,
i: @Sendable @escaping (String, Int) async throws -> String
i: @Sendable @escaping (String, Int) async throws -> String,
j: String = ""
) {
self.a = a
self.b = b
Expand All @@ -136,6 +141,7 @@ final class PublicInitMacroTests: XCTestCase {
self.g = g
self.h = h
self.i = i
self.j = j
}
}
"""
Expand Down

0 comments on commit 09c6458

Please sign in to comment.