Skip to content

If parsing of string interpolation fails, print the error messages using DiagnosticFormatter #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftDiagnostics/DiagnosticsFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct DiagnosticsFormatter {
static let contextSize = 2

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

// First, we need to put each line and its diagnostics together
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftSyntaxBuilder/Syntax+StringInterpolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ public protocol SyntaxExpressibleByStringInterpolation:
enum SyntaxStringInterpolationError: Error, CustomStringConvertible {
case didNotConsumeAllTokens(remainingTokens: [TokenSyntax])
case producedInvalidNodeType(expectedType: SyntaxProtocol.Type, actualType: SyntaxProtocol.Type)
case diagnostics([Diagnostic])
case diagnostics([Diagnostic], tree: Syntax)

var description: String {
switch self {
case .didNotConsumeAllTokens(remainingTokens: let tokens):
return "Extraneous text in snippet: '\(tokens.map(\.description).joined())'"
case .producedInvalidNodeType(expectedType: let expectedType, actualType: let actualType):
return "Parsing the code snippet was expected to produce a \(expectedType) but produced a \(actualType)"
case .diagnostics(let diagnostics):
return diagnostics.map(\.debugDescription).joined(separator: "\n")
case .diagnostics(let diagnostics, let tree):
return DiagnosticsFormatter.annotatedSource(tree: tree, diags: diagnostics)
}
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ extension SyntaxExpressibleByStringInterpolation {
if result.hasError {
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: result)
assert(!diagnostics.isEmpty)
throw SyntaxStringInterpolationError.diagnostics(diagnostics)
throw SyntaxStringInterpolationError.diagnostics(diagnostics, tree: Syntax(result))
}
return result
}
Expand Down