Skip to content

Commit

Permalink
Expand macros to handle all reference types (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Mateusz Fidos <mateuszfidos@gmail.com>
  • Loading branch information
mateuszfidos and emefde authored Jun 26, 2024
1 parent 6519743 commit 283cd60
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public struct NeedsMacro: MemberMacro {
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard declaration.as(ClassDeclSyntax.self) != nil else {
throw InjectGrailError(message: "@Needs only works on class declaration")
guard InjectGrailMacrosHelper.isReferenceTypeDeclaration(declaration) else {
throw InjectGrailError(message: "@Needs only works on reference type declaration")
}

guard
Expand All @@ -37,12 +37,14 @@ public struct NeedsInjectorMacro: MemberMacro {
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard let classDeclaration = declaration.as(ClassDeclSyntax.self) else {
throw InjectGrailError(message: "@NeedsInjector only works on class declaration")
let classDeclaration = declaration.as(ClassDeclSyntax.self)
let actorDeclaration = declaration.as(ActorDeclSyntax.self)

guard let typeName = classDeclaration?.name.text ?? actorDeclaration?.name.text else {
throw InjectGrailError(message: "@NeedsInjector only works on reference type declaration")
}

let className = classDeclaration.name.text
let injectorProtocolType = className.replacingOccurrences(of: "Impl", with: "") + "Injector"
let injectorProtocolType = typeName.replacingOccurrences(of: "Impl", with: "") + "Injector"

let noLet = declaration.memberBlock.members.first(where: { $0.decl.as(VariableDeclSyntax.self)?.bindings.first(where: { "\($0.pattern)" == "injector" }) != nil}) != nil
let noConstructor = declaration.memberBlock.members.first(where: { $0.decl.as(InitializerDeclSyntax.self) != nil})?.decl.as(InitializerDeclSyntax.self)!.signature.parameterClause.parameters.first?.firstName.text == "injector"
Expand All @@ -60,8 +62,8 @@ public struct InjectsMacro: MemberMacro {
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard declaration.as(ClassDeclSyntax.self) != nil else {
throw InjectGrailError(message: "@Injects only works on class declaration")
guard InjectGrailMacrosHelper.isReferenceTypeDeclaration(declaration) else {
throw InjectGrailError(message: "@Injects only works on reference type declaration")
}

guard
Expand All @@ -85,3 +87,9 @@ struct InjectGrailMacrosPlugin: CompilerPlugin {
InjectsMacro.self
]
}

private enum InjectGrailMacrosHelper {
static func isReferenceTypeDeclaration(_ declaration: some DeclGroupSyntax) -> Bool {
declaration.as(ClassDeclSyntax.self) != nil || declaration.as(ActorDeclSyntax.self) != nil
}
}

0 comments on commit 283cd60

Please sign in to comment.