diff --git a/Sources/Macros/AddInit.swift b/Sources/Macros/AddInit.swift index 59f04aa..fde5d25 100644 --- a/Sources/Macros/AddInit.swift +++ b/Sources/Macros/AddInit.swift @@ -33,7 +33,8 @@ public struct AddInit: MemberMacro { if let patternBinding = member.decl.as(VariableDeclSyntax.self)?.bindings .as(PatternBindingListSyntax.self)?.first?.as(PatternBindingSyntax.self), let identifier = patternBinding.pattern.as(IdentifierPatternSyntax.self)?.identifier, - let type = patternBinding.typeAnnotation?.as(TypeAnnotationSyntax.self)?.type { + let type = patternBinding.typeAnnotation?.as(TypeAnnotationSyntax.self)?.type, + patternBinding.accessorBlock == nil { var parameter = "\(identifier): " if type.is(FunctionTypeSyntax.self) { parameter += "@escaping " @@ -57,7 +58,8 @@ public struct AddInit: MemberMacro { guard let patternBinding = member.decl.as(VariableDeclSyntax.self)?.bindings .as(PatternBindingListSyntax.self)?.first?.as(PatternBindingSyntax.self), let identifier = patternBinding.pattern.as(IdentifierPatternSyntax.self)?.identifier, - let type = patternBinding.typeAnnotation?.as(TypeAnnotationSyntax.self)?.type else { return nil } + let type = patternBinding.typeAnnotation?.as(TypeAnnotationSyntax.self)?.type, + patternBinding.accessorBlock == nil else { return nil } let mockValue = type.mockValue(randomValue: randomValue) ?? type.as(OptionalTypeSyntax.self)?.mockValue(randomValue: randomValue) ?? "nil" diff --git a/Tests/MacroTests/AddInitTests.swift b/Tests/MacroTests/AddInitTests.swift index 18f4488..00049c2 100644 --- a/Tests/MacroTests/AddInitTests.swift +++ b/Tests/MacroTests/AddInitTests.swift @@ -34,6 +34,35 @@ final class AddInitTests: XCTestCase { ) } + func testAddInitWithComputedPropertyMacro() { + assertMacroExpansion( + """ + @AddInit + struct A { + let a: Int? + var b: Int { + 0 + } + } + """, + expandedSource: + """ + + struct A { + let a: Int? + var b: Int { + 0 + } + + init(a: Int? = nil) { + self.a = a + } + } + """, + macros: testMacros + ) + } + func testAddPublicInitMacro() { assertMacroExpansion( """