From aa24a3039d7f393d606c83b669b359994b9c96bc Mon Sep 17 00:00:00 2001 From: Enes Karaosman Date: Sat, 10 Aug 2024 18:59:31 +0300 Subject: [PATCH] chore: improve diagnostic flow --- .../xcshareddata/swiftpm/Package.resolved | 7 +++-- Package.resolved | 12 +++---- Sources/LoremSwiftifyMacros/Diagnostic.swift | 31 +++++++++++++++++++ .../LoremSwiftifyMacro.swift | 27 +--------------- .../SupportedTypes/LoremSwiftifyClass.swift | 2 ++ .../SupportedTypes/LoremSwiftifyEnum.swift | 2 ++ .../SupportedTypes/LoremSwiftifyStruct.swift | 2 ++ 7 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 Sources/LoremSwiftifyMacros/Diagnostic.swift diff --git a/Example/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 3fdaa03..af1c532 100644 --- a/Example/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,4 +1,5 @@ { + "originHash" : "6a124a9d1b72d90a9989b97c9c4aeef9531842ce12391f89eaad7e6d57fb6fb4", "pins" : [ { "identity" : "fakery", @@ -14,10 +15,10 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-syntax.git", "state" : { - "revision" : "fa8f95c2d536d6620cc2f504ebe8a6167c9fc2dd", - "version" : "510.0.1" + "revision" : "303e5c5c36d6a558407d364878df131c3546fad8", + "version" : "510.0.2" } } ], - "version" : 2 + "version" : 3 } diff --git a/Package.resolved b/Package.resolved index 3084bd4..a18b6f7 100644 --- a/Package.resolved +++ b/Package.resolved @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-macro-testing", "state" : { - "revision" : "5c4a1b9d7c23cd5c08ea50677d8e89080365cb00", - "version" : "0.4.0" + "revision" : "20c1a8f3b624fb5d1503eadcaa84743050c350f4", + "version" : "0.5.2" } }, { @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-snapshot-testing", "state" : { - "revision" : "625ccca8570773dd84a34ee51a81aa2bc5a4f97a", - "version" : "1.16.0" + "revision" : "6d932a79e7173b275b96c600c86c603cf84f153c", + "version" : "1.17.4" } }, { @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-syntax.git", "state" : { - "revision" : "303e5c5c36d6a558407d364878df131c3546fad8", - "version" : "510.0.2" + "revision" : "2bc86522d115234d1f588efe2bcb4ce4be8f8b82", + "version" : "510.0.3" } } ], diff --git a/Sources/LoremSwiftifyMacros/Diagnostic.swift b/Sources/LoremSwiftifyMacros/Diagnostic.swift new file mode 100644 index 0000000..bc5afe1 --- /dev/null +++ b/Sources/LoremSwiftifyMacros/Diagnostic.swift @@ -0,0 +1,31 @@ +// +// Diagnostic.swift +// +// +// Created by Enes Karaosman on 10.08.2024. +// + +import SwiftDiagnostics + +enum LoremSwiftifyMacroDiagnostic: DiagnosticMessage, Error { + case unsupportedType + case noMemberToMock + case noEnumCase + + var message: String { + switch self { + case .unsupportedType: + return "You can only lorem struct, class and enum" + case .noMemberToMock: + return "There is no member to lorem" + case .noEnumCase: + return "There is no enum case to lorem" + } + } + + var diagnosticID: SwiftDiagnostics.MessageID { + MessageID(domain: "Swift", id: "LoremSwiftifyMacroDiagnostic.\(self)") + } + + var severity: SwiftDiagnostics.DiagnosticSeverity { .error } +} diff --git a/Sources/LoremSwiftifyMacros/LoremSwiftifyMacro.swift b/Sources/LoremSwiftifyMacros/LoremSwiftifyMacro.swift index b03de1b..02cb34b 100644 --- a/Sources/LoremSwiftifyMacros/LoremSwiftifyMacro.swift +++ b/Sources/LoremSwiftifyMacros/LoremSwiftifyMacro.swift @@ -6,31 +6,6 @@ import SwiftSyntaxMacros let protocolName = "LoremIpsumize" -// TODO: Use better diagnostics -// https://github.com/apple/swift-syntax/blob/main/Examples/Sources/MacroExamples/Implementation/Diagnostics.swift -enum LoremSwiftifyMacroDiagnostic: DiagnosticMessage, Error { - case unsupportedType - case noMemberToMock - case noEnumCase - - var message: String { - switch self { - case .unsupportedType: - return "You can only lorem struct, class and enum" - case .noMemberToMock: - return "There is no member to lorem" - case .noEnumCase: - return "There is no enum case to lorem" - } - } - - var diagnosticID: SwiftDiagnostics.MessageID { - MessageID(domain: "Swift", id: "LoremSwiftifyMacroDiagnostic.\(self)") - } - - var severity: SwiftDiagnostics.DiagnosticSeverity { .error } -} - public enum LoremMacro: PeerMacro { public static func expansion(of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] { // LoremMacro does not generate something, this will be controlled in LoremSwiftify macro @@ -52,7 +27,7 @@ public enum LoremSwiftifyMacro: ExtensionMacro { context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.unsupportedType)) - throw LoremSwiftifyMacroDiagnostic.unsupportedType + return [] } let extensionDecl = try ExtensionDeclSyntax( diff --git a/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyClass.swift b/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyClass.swift index 66edd0c..c2e89cc 100644 --- a/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyClass.swift +++ b/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyClass.swift @@ -20,6 +20,8 @@ class LoremSwiftifyClass { ) throws -> DeclSyntax { if declaration.memberBlock.members.isEmpty { context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.noMemberToMock)) + + return "" } return try LoremSwiftifyMacroParsingShared.handleClassOrStructDeclSyntax( diff --git a/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyEnum.swift b/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyEnum.swift index 7a80307..e042dc9 100644 --- a/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyEnum.swift +++ b/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyEnum.swift @@ -24,6 +24,8 @@ class LoremSwiftifyEnum { if cases.isEmpty { context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.noEnumCase)) + + return "" } let caseExpr = generateEnumCreationFunctionBody(for: cases) diff --git a/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyStruct.swift b/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyStruct.swift index fb4ccca..ec12296 100644 --- a/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyStruct.swift +++ b/Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyStruct.swift @@ -20,6 +20,8 @@ class LoremSwiftifyStruct { ) throws -> DeclSyntax { if declaration.memberBlock.members.isEmpty { context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.noMemberToMock)) + + return "" } return try LoremSwiftifyMacroParsingShared.handleClassOrStructDeclSyntax(