Skip to content

Commit d6fa844

Browse files
committed
Make Diagnostic CustomDebugStringConvertible
1 parent cb59b85 commit d6fa844

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Sources/Diagnostics/Diagnostic.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SwiftSyntax
1414

15-
public struct Diagnostic {
15+
public struct Diagnostic: CustomDebugStringConvertible {
1616
/// The message that should be displayed to the user
1717
public let diagMessage: DiagnosticMessage
1818

@@ -39,5 +39,15 @@ public struct Diagnostic {
3939
public func location(converter: SourceLocationConverter) -> SourceLocation {
4040
return node.startLocation(converter: converter)
4141
}
42+
43+
public var debugDescription: String {
44+
if let root = node.root.as(SourceFileSyntax.self) {
45+
let locationConverter = SourceLocationConverter(file: "", tree: root)
46+
let location = location(converter: locationConverter)
47+
return "\(location): \(message)"
48+
} else {
49+
return "<unknown>: \(message)"
50+
}
51+
}
4252
}
4353

Sources/swift-parser-test/swift-parser-test.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,11 @@ class PrintDiags: ParsableCommand {
132132
enableBareSlashRegexLiteral: enableBareSlashRegex
133133
)
134134
let diags = ParseDiagnosticsGenerator.diagnostics(for: tree)
135-
let locationConverter = SourceLocationConverter(file: sourceFile, tree: tree)
136135
if diags.isEmpty {
137136
print("No diagnostics produced")
138137
}
139138
for diag in diags {
140-
let location = diag.location(converter: locationConverter)
141-
let message = diag.message
142-
print("\(location): \(message)")
139+
print("\(diag.debugDescription)")
143140
}
144141
}
145142
}

0 commit comments

Comments
 (0)