Skip to content

Commit aa24a30

Browse files
committed
chore: improve diagnostic flow
1 parent 31934fb commit aa24a30

File tree

7 files changed

+48
-35
lines changed

7 files changed

+48
-35
lines changed

Example/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"originHash" : "6a124a9d1b72d90a9989b97c9c4aeef9531842ce12391f89eaad7e6d57fb6fb4",
23
"pins" : [
34
{
45
"identity" : "fakery",
@@ -14,10 +15,10 @@
1415
"kind" : "remoteSourceControl",
1516
"location" : "https://github.com/apple/swift-syntax.git",
1617
"state" : {
17-
"revision" : "fa8f95c2d536d6620cc2f504ebe8a6167c9fc2dd",
18-
"version" : "510.0.1"
18+
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
19+
"version" : "510.0.2"
1920
}
2021
}
2122
],
22-
"version" : 2
23+
"version" : 3
2324
}

Package.resolved

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@
1414
"kind" : "remoteSourceControl",
1515
"location" : "https://github.com/pointfreeco/swift-macro-testing",
1616
"state" : {
17-
"revision" : "5c4a1b9d7c23cd5c08ea50677d8e89080365cb00",
18-
"version" : "0.4.0"
17+
"revision" : "20c1a8f3b624fb5d1503eadcaa84743050c350f4",
18+
"version" : "0.5.2"
1919
}
2020
},
2121
{
2222
"identity" : "swift-snapshot-testing",
2323
"kind" : "remoteSourceControl",
2424
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
2525
"state" : {
26-
"revision" : "625ccca8570773dd84a34ee51a81aa2bc5a4f97a",
27-
"version" : "1.16.0"
26+
"revision" : "6d932a79e7173b275b96c600c86c603cf84f153c",
27+
"version" : "1.17.4"
2828
}
2929
},
3030
{
3131
"identity" : "swift-syntax",
3232
"kind" : "remoteSourceControl",
3333
"location" : "https://github.com/apple/swift-syntax.git",
3434
"state" : {
35-
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
36-
"version" : "510.0.2"
35+
"revision" : "2bc86522d115234d1f588efe2bcb4ce4be8f8b82",
36+
"version" : "510.0.3"
3737
}
3838
}
3939
],
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Diagnostic.swift
3+
//
4+
//
5+
// Created by Enes Karaosman on 10.08.2024.
6+
//
7+
8+
import SwiftDiagnostics
9+
10+
enum LoremSwiftifyMacroDiagnostic: DiagnosticMessage, Error {
11+
case unsupportedType
12+
case noMemberToMock
13+
case noEnumCase
14+
15+
var message: String {
16+
switch self {
17+
case .unsupportedType:
18+
return "You can only lorem struct, class and enum"
19+
case .noMemberToMock:
20+
return "There is no member to lorem"
21+
case .noEnumCase:
22+
return "There is no enum case to lorem"
23+
}
24+
}
25+
26+
var diagnosticID: SwiftDiagnostics.MessageID {
27+
MessageID(domain: "Swift", id: "LoremSwiftifyMacroDiagnostic.\(self)")
28+
}
29+
30+
var severity: SwiftDiagnostics.DiagnosticSeverity { .error }
31+
}

Sources/LoremSwiftifyMacros/LoremSwiftifyMacro.swift

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@ import SwiftSyntaxMacros
66

77
let protocolName = "LoremIpsumize"
88

9-
// TODO: Use better diagnostics
10-
// https://github.com/apple/swift-syntax/blob/main/Examples/Sources/MacroExamples/Implementation/Diagnostics.swift
11-
enum LoremSwiftifyMacroDiagnostic: DiagnosticMessage, Error {
12-
case unsupportedType
13-
case noMemberToMock
14-
case noEnumCase
15-
16-
var message: String {
17-
switch self {
18-
case .unsupportedType:
19-
return "You can only lorem struct, class and enum"
20-
case .noMemberToMock:
21-
return "There is no member to lorem"
22-
case .noEnumCase:
23-
return "There is no enum case to lorem"
24-
}
25-
}
26-
27-
var diagnosticID: SwiftDiagnostics.MessageID {
28-
MessageID(domain: "Swift", id: "LoremSwiftifyMacroDiagnostic.\(self)")
29-
}
30-
31-
var severity: SwiftDiagnostics.DiagnosticSeverity { .error }
32-
}
33-
349
public enum LoremMacro: PeerMacro {
3510
public static func expansion(of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] {
3611
// LoremMacro does not generate something, this will be controlled in LoremSwiftify macro
@@ -52,7 +27,7 @@ public enum LoremSwiftifyMacro: ExtensionMacro {
5227

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

55-
throw LoremSwiftifyMacroDiagnostic.unsupportedType
30+
return []
5631
}
5732

5833
let extensionDecl = try ExtensionDeclSyntax(

Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyClass.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class LoremSwiftifyClass {
2020
) throws -> DeclSyntax {
2121
if declaration.memberBlock.members.isEmpty {
2222
context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.noMemberToMock))
23+
24+
return ""
2325
}
2426

2527
return try LoremSwiftifyMacroParsingShared.handleClassOrStructDeclSyntax(

Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyEnum.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class LoremSwiftifyEnum {
2424

2525
if cases.isEmpty {
2626
context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.noEnumCase))
27+
28+
return ""
2729
}
2830

2931
let caseExpr = generateEnumCreationFunctionBody(for: cases)

Sources/LoremSwiftifyMacros/SupportedTypes/LoremSwiftifyStruct.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class LoremSwiftifyStruct {
2020
) throws -> DeclSyntax {
2121
if declaration.memberBlock.members.isEmpty {
2222
context.diagnose(.init(node: declaration, message: LoremSwiftifyMacroDiagnostic.noMemberToMock))
23+
24+
return ""
2325
}
2426

2527
return try LoremSwiftifyMacroParsingShared.handleClassOrStructDeclSyntax(

0 commit comments

Comments
 (0)