Skip to content

Commit

Permalink
chore: improve diagnostic flow
Browse files Browse the repository at this point in the history
  • Loading branch information
EnesKaraosman committed Aug 10, 2024
1 parent 31934fb commit aa24a30
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"originHash" : "6a124a9d1b72d90a9989b97c9c4aeef9531842ce12391f89eaad7e6d57fb6fb4",
"pins" : [
{
"identity" : "fakery",
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-macro-testing",
"state" : {
"revision" : "5c4a1b9d7c23cd5c08ea50677d8e89080365cb00",
"version" : "0.4.0"
"revision" : "20c1a8f3b624fb5d1503eadcaa84743050c350f4",
"version" : "0.5.2"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "625ccca8570773dd84a34ee51a81aa2bc5a4f97a",
"version" : "1.16.0"
"revision" : "6d932a79e7173b275b96c600c86c603cf84f153c",
"version" : "1.17.4"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
"revision" : "2bc86522d115234d1f588efe2bcb4ce4be8f8b82",
"version" : "510.0.3"
}
}
],
Expand Down
31 changes: 31 additions & 0 deletions Sources/LoremSwiftifyMacros/Diagnostic.swift
Original file line number Diff line number Diff line change
@@ -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 }
}
27 changes: 1 addition & 26 deletions Sources/LoremSwiftifyMacros/LoremSwiftifyMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,7 +27,7 @@ public enum LoremSwiftifyMacro: ExtensionMacro {

context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.unsupportedType))

throw LoremSwiftifyMacroDiagnostic.unsupportedType
return []
}

let extensionDecl = try ExtensionDeclSyntax(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class LoremSwiftifyEnum {

if cases.isEmpty {
context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.noEnumCase))

return ""
}

let caseExpr = generateEnumCreationFunctionBody(for: cases)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit aa24a30

Please sign in to comment.