diff --git a/Sources/RelaxMacros/APIEndpointMacro.swift b/Sources/RelaxMacros/APIEndpointMacro.swift index 571004c..729d69e 100644 --- a/Sources/RelaxMacros/APIEndpointMacro.swift +++ b/Sources/RelaxMacros/APIEndpointMacro.swift @@ -22,26 +22,20 @@ public struct APIEndpointMacro: ExtensionMacro { .arguments?.as(LabeledExprListSyntax.self)? .first?.expression.as(StringLiteralExprSyntax.self)? .segments.trimmedDescription, - !path.isEmpty + !(path.trimmingCharacters(in: .whitespacesAndNewlines)).isEmpty else { let error = Diagnostic(node: node, message: RelaxMacroDiagnostic.invalidPath) context.diagnose(error) return [] } + guard let parent = node.attributeName.as(IdentifierTypeSyntax.self)? .genericArgumentClause?.as(GenericArgumentClauseSyntax.self)? .arguments .description else { - let fixIt = FixIt.replace( - message: RelaxFixItMessage.missingParent, - oldNode: Syntax(node), - newNode: FixItRewriter().visit(node) - ) - let error = Diagnostic(node: node, message: RelaxMacroDiagnostic.missingParent, fixIt: fixIt) - context.diagnose(error) - return [] } + let decl: DeclSyntax = """ extension \(type.trimmed): Endpoint { @@ -49,23 +43,9 @@ public struct APIEndpointMacro: ExtensionMacro { static let path: String = \"\(raw: path)\" } """ + guard let extensionDecl = decl.as(ExtensionDeclSyntax.self) else { return [] } + return [extensionDecl] } - - final private class FixItRewriter: SyntaxRewriter { - override func visitAny(_ node: Syntax) -> Syntax? { - guard let attributeName = node.as(IdentifierTypeSyntax.self), - attributeName.genericArgumentClause == nil - else { return node } - let listSyntax = GenericArgumentListSyntax { - GenericArgumentSyntax(argument: TypeSyntax(stringLiteral: "<#APIComponent#>")) - } - let placeholder = GenericArgumentClauseSyntax(arguments: listSyntax) - guard let genericDecl = placeholder.as(GenericArgumentClauseSyntax.self) else { return node } - var newAttributeName = attributeName - newAttributeName.genericArgumentClause = genericDecl - return newAttributeName.as(Syntax.self) - } - } } diff --git a/Sources/RelaxMacros/RelaxMacroDiagnostic.swift b/Sources/RelaxMacros/RelaxMacroDiagnostic.swift index 8fab725..74e50c6 100644 --- a/Sources/RelaxMacros/RelaxMacroDiagnostic.swift +++ b/Sources/RelaxMacros/RelaxMacroDiagnostic.swift @@ -10,7 +10,6 @@ import SwiftDiagnostics enum RelaxMacroDiagnostic: String, DiagnosticMessage { case invalidBaseURL case invalidPath - case missingParent var severity: DiagnosticSeverity { .error } @@ -19,9 +18,7 @@ enum RelaxMacroDiagnostic: String, DiagnosticMessage { case .invalidBaseURL: "The base URL is invalid." case .invalidPath: - "The path is invalid." - case .missingParent: - "The parent APIComponent must be specified as a generic argument." + "The path must not be empty." } } @@ -29,19 +26,3 @@ enum RelaxMacroDiagnostic: String, DiagnosticMessage { MessageID(domain: "RelaxMacros", id: rawValue) } } - -enum RelaxFixItMessage: String, FixItMessage { - case missingParent - var message: String { - switch self { - case .missingParent: - "Add generic argument 'APIComponent'" - } - } - - var fixItID: SwiftDiagnostics.MessageID { - MessageID(domain: "RelaxMacros", id: rawValue) - } - - -} diff --git a/Tests/RelaxMacrosTests/APIEndpointMacroTests.swift b/Tests/RelaxMacrosTests/APIEndpointMacroTests.swift index 82cd1bf..360e3fe 100644 --- a/Tests/RelaxMacrosTests/APIEndpointMacroTests.swift +++ b/Tests/RelaxMacrosTests/APIEndpointMacroTests.swift @@ -42,11 +42,11 @@ final class APIEndpointMacroTests: XCTestCase { #endif } - func testAPIEndpointNoParent() throws { + func testAPIEndpointInvalidPath() throws { #if canImport(RelaxMacros) assertMacroExpansion( """ - @APIEndpoint("path") + @APIEndpoint("") enum TestService { } """, @@ -54,28 +54,13 @@ final class APIEndpointMacroTests: XCTestCase { enum TestService { } """, - diagnostics: [ - DiagnosticSpec( - message: RelaxMacroDiagnostic.missingParent.message, - line: 1, - column: 1, - fixIts: [ - FixItSpec(message: RelaxFixItMessage.missingParent.message) - ] - ) - ], + diagnostics: [.init(message: RelaxMacroDiagnostic.invalidPath.message, line: 1, column: 1)], macros: testMacros ) - #else - throw XCTSkip("macros are only supported when running tests for the host platform") - #endif - } - - func testAPIEndpointInvalidPath() throws { - #if canImport(RelaxMacros) + assertMacroExpansion( """ - @APIEndpoint("") + @APIEndpoint(" ") enum TestService { } """,