Skip to content

Commit e457306

Browse files
committed
If parsing of string interpolation fails, print the error messages using DiagnosticFormatter
1 parent 7e3dc35 commit e457306

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public struct DiagnosticsFormatter {
2424
static let contextSize = 2
2525

2626
/// Print given diagnostics for a given syntax tree on the command line
27-
public static func annotatedSource(tree: SourceFileSyntax, diags: [Diagnostic]) -> String {
27+
public static func annotatedSource<SyntaxType: SyntaxProtocol>(tree: SyntaxType, diags: [Diagnostic]) -> String {
2828
let slc = SourceLocationConverter(file: "", tree: tree)
2929

3030
// First, we need to put each line and its diagnostics together

Sources/SwiftSyntaxBuilder/Syntax+StringInterpolation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ public protocol SyntaxExpressibleByStringInterpolation:
9999
enum SyntaxStringInterpolationError: Error, CustomStringConvertible {
100100
case didNotConsumeAllTokens(remainingTokens: [TokenSyntax])
101101
case producedInvalidNodeType(expectedType: SyntaxProtocol.Type, actualType: SyntaxProtocol.Type)
102-
case diagnostics([Diagnostic])
102+
case diagnostics([Diagnostic], tree: Syntax)
103103

104104
var description: String {
105105
switch self {
106106
case .didNotConsumeAllTokens(remainingTokens: let tokens):
107107
return "Extraneous text in snippet: '\(tokens.map(\.description).joined())'"
108108
case .producedInvalidNodeType(expectedType: let expectedType, actualType: let actualType):
109109
return "Parsing the code snippet was expected to produce a \(expectedType) but produced a \(actualType)"
110-
case .diagnostics(let diagnostics):
111-
return diagnostics.map(\.debugDescription).joined(separator: "\n")
110+
case .diagnostics(let diagnostics, let tree):
111+
return DiagnosticsFormatter.annotatedSource(tree: tree, diags: diagnostics)
112112
}
113113
}
114114
}
@@ -145,7 +145,7 @@ extension SyntaxExpressibleByStringInterpolation {
145145
if result.hasError {
146146
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: result)
147147
assert(!diagnostics.isEmpty)
148-
throw SyntaxStringInterpolationError.diagnostics(diagnostics)
148+
throw SyntaxStringInterpolationError.diagnostics(diagnostics, tree: Syntax(result))
149149
}
150150
return result
151151
}

0 commit comments

Comments
 (0)