Skip to content

Commit

Permalink
Small updates from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosen081 committed Jan 11, 2024
1 parent 0b08f73 commit 8322646
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Sources/WhoopDIKitMacros/InjectableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ private struct VariableDeclaration {
let injectedName: String?
}


struct InjectableMacro: ExtensionMacro, MemberMacro {
/// Adds the `inject` and `init` function that we use for the `Injectable` protocol
static func expansion(of node: AttributeSyntax, providingMembersOf declaration: some DeclGroupSyntax, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
Expand All @@ -25,10 +24,10 @@ struct InjectableMacro: ExtensionMacro, MemberMacro {
// Only do this for stored properties that are not `let` with a value (since those are constant)
guard let declSyntax = memberBlock.decl.as(VariableDeclSyntax.self),
declSyntax.isStoredProperty,
let propertyName = declSyntax.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier.text,
let typeName = declSyntax.bindings.first?.typeAnnotation?.type.as(IdentifierTypeSyntax.self)?.name.text,
!declSyntax.isLetWithValue,
!declSyntax.isStaticOrLazy
!declSyntax.isStaticOrLazy,
let propertyName = declSyntax.variableName,
let typeName = declSyntax.typeName
else { return nil }

// If the code has `InjectableName` on it, get the name to use
Expand Down Expand Up @@ -195,4 +194,12 @@ extension VariableDeclSyntax {
syntax.name.tokenKind == .keyword(.static) || syntax.name.tokenKind == .keyword(.lazy)
}
}

var variableName: String? {
self.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier.text
}

var typeName: String? {
self.bindings.first?.typeAnnotation?.type.as(IdentifierTypeSyntax.self)?.name.text
}
}

0 comments on commit 8322646

Please sign in to comment.