From 0ebc626ad8eab47715db1eb706ffb70f7f06ead1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kowalski?= Date: Wed, 8 Nov 2023 13:26:08 +0100 Subject: [PATCH] small code refactoring --- .../MacroDeclarationHelper.swift | 11 +++++++++++ .../XCTestParametrizedMacroMacro.swift | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Sources/XCTestParametrizedMacroMacros/MacroDeclarationHelper.swift b/Sources/XCTestParametrizedMacroMacros/MacroDeclarationHelper.swift index dae618d..ef7cf38 100644 --- a/Sources/XCTestParametrizedMacroMacros/MacroDeclarationHelper.swift +++ b/Sources/XCTestParametrizedMacroMacros/MacroDeclarationHelper.swift @@ -8,6 +8,17 @@ struct MacroDeclarationHelper { self.declaration = declaration } + + /// Returns 'TokenSyntax' representing name of the input parameter. + var inputParamName: TokenSyntax? { + declaration.signature.input.parameterList.first?.secondName + } + + /// Returns 'TypeSyntax' representing type of the input object. + var inputParamType: TypeSyntax? { + declaration.signature.input.parameterList.first?.type + } + var firstAttribute: AttributeSyntax? { return declaration.attributes.first?.as(AttributeSyntax.self) } diff --git a/Sources/XCTestParametrizedMacroMacros/XCTestParametrizedMacroMacro.swift b/Sources/XCTestParametrizedMacroMacros/XCTestParametrizedMacroMacro.swift index 3b42951..ee2eda2 100644 --- a/Sources/XCTestParametrizedMacroMacros/XCTestParametrizedMacroMacro.swift +++ b/Sources/XCTestParametrizedMacroMacros/XCTestParametrizedMacroMacro.swift @@ -18,11 +18,11 @@ public struct ParametrizeMacro: PeerMacro { let macroDeclarationHelper = MacroDeclarationHelper(declaration) let funcName = declaration.identifier - guard let inputParamName = declaration.signature.input.parameterList.first?.secondName?.text else { + guard let inputParamName = macroDeclarationHelper.inputParamName?.text else { throw ParametrizeMacroError.functionInputParamSecondNameMissing } - guard let inputParamType = declaration.signature.input.parameterList.first?.type else { + guard let inputParamType = macroDeclarationHelper.inputParamType else { throw ParametrizeMacroError.functionInputParamTypeMissing }