From ada67d4c077805081adb959013aacbdb2942c7a3 Mon Sep 17 00:00:00 2001 From: Ryu0118 Date: Mon, 2 Oct 2023 16:16:11 +0900 Subject: [PATCH 1/2] Static properties are excluded from the initializer. --- Sources/PublicInitMacroPlugin/PublicInitMacro.swift | 5 ++++- Tests/PublicInitMacroTests/PublicInitMacroTests.swift | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Sources/PublicInitMacroPlugin/PublicInitMacro.swift b/Sources/PublicInitMacroPlugin/PublicInitMacro.swift index e6a6d5f..0fdb987 100644 --- a/Sources/PublicInitMacroPlugin/PublicInitMacro.swift +++ b/Sources/PublicInitMacroPlugin/PublicInitMacro.swift @@ -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 @@ -38,6 +40,7 @@ public struct PublicInitMacro: MemberMacro { return nil } + if let functionType = typeAnnotation.type.as(FunctionTypeSyntax.self) { return binding.with( \.typeAnnotation, diff --git a/Tests/PublicInitMacroTests/PublicInitMacroTests.swift b/Tests/PublicInitMacroTests/PublicInitMacroTests.swift index ffb5af4..e527ee1 100644 --- a/Tests/PublicInitMacroTests/PublicInitMacroTests.swift +++ b/Tests/PublicInitMacroTests/PublicInitMacroTests.swift @@ -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 @@ -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 @@ -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, @@ -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 @@ -136,6 +141,7 @@ final class PublicInitMacroTests: XCTestCase { self.g = g self.h = h self.i = i + self.j = j } } """ From 505083e7c4f0e947515429a63605ebc6471d2347 Mon Sep 17 00:00:00 2001 From: Ryu0118 Date: Mon, 2 Oct 2023 16:17:29 +0900 Subject: [PATCH 2/2] fix --- Sources/PublicInitMacroPlugin/PublicInitMacro.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/PublicInitMacroPlugin/PublicInitMacro.swift b/Sources/PublicInitMacroPlugin/PublicInitMacro.swift index 0fdb987..be97747 100644 --- a/Sources/PublicInitMacroPlugin/PublicInitMacro.swift +++ b/Sources/PublicInitMacroPlugin/PublicInitMacro.swift @@ -40,7 +40,6 @@ public struct PublicInitMacro: MemberMacro { return nil } - if let functionType = typeAnnotation.type.as(FunctionTypeSyntax.self) { return binding.with( \.typeAnnotation,