Skip to content

Commit 25b3800

Browse files
Maxence MottardThomasDutartre
authored andcommitted
Create the method to extract the "inheritedTypes" parameter
1 parent 9033afd commit 25b3800

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Sources/SpyableMacro/Extractors/Extractor.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,46 @@ struct Extractor {
145145
func extractAccessLevel(from protocolDeclSyntax: ProtocolDeclSyntax) -> DeclModifierSyntax? {
146146
protocolDeclSyntax.modifiers.first(where: \.name.isAccessLevelSupportedInProtocol)
147147
}
148+
149+
func extractInheritedTypes(
150+
from attribute: AttributeSyntax,
151+
in context: some MacroExpansionContext
152+
) -> String? {
153+
guard case let .argumentList(argumentList) = attribute.arguments else {
154+
// No arguments are present in the attribute.
155+
return nil
156+
}
157+
158+
let inheritedTypesArgument = argumentList.first { argument in
159+
argument.label?.text == "inheritedTypes"
160+
}
161+
162+
guard let inheritedTypesArgument else {
163+
// The `inheritedTypes` argument is missing.
164+
return nil
165+
}
166+
167+
let segments = inheritedTypesArgument.expression
168+
.as(StringLiteralExprSyntax.self)?
169+
.segments
170+
171+
guard let segments,
172+
segments.count == 1,
173+
case let .stringSegment(literalSegment)? = segments.first
174+
else {
175+
// The `inheritedTypes` argument's value is not a static string literal.
176+
context.diagnose(
177+
Diagnostic(
178+
node: attribute,
179+
message: SpyableDiagnostic.behindPreprocessorFlagArgumentRequiresStaticStringLiteral,
180+
highlights: [Syntax(inheritedTypesArgument.expression)]
181+
)
182+
)
183+
return nil
184+
}
185+
186+
return literalSegment.content.text
187+
}
148188
}
149189

150190
extension TokenSyntax {

0 commit comments

Comments
 (0)