Skip to content

Commit

Permalink
Merge pull request #547 from allevato/downgrade-placeholder-errors
Browse files Browse the repository at this point in the history
Downgrade `editor placeholder in source file` from error to warning.
  • Loading branch information
allevato committed Jun 27, 2023
2 parents f58791f + c71c897 commit 2480f73
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions Sources/SwiftFormat/Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,53 @@ func parseAndEmitDiagnostics(
operatorTable.foldAll(Parser.parse(source: source)) { _ in }.as(SourceFileSyntax.self)!

let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: sourceFile)
var hasErrors = false
if let parsingDiagnosticHandler = parsingDiagnosticHandler {
let expectedConverter =
SourceLocationConverter(file: url?.path ?? "<unknown>", tree: sourceFile)
for diagnostic in diagnostics {
let location = diagnostic.location(converter: expectedConverter)
parsingDiagnosticHandler(diagnostic, location)

// Downgrade editor placeholders to warnings, because it is useful to support formatting
// in-progress files that contain those.
if diagnostic.diagnosticID == StaticTokenError.editorPlaceholder.diagnosticID {
parsingDiagnosticHandler(downgradedToWarning(diagnostic), location)
} else {
parsingDiagnosticHandler(diagnostic, location)
hasErrors = true
}
}
}

guard diagnostics.isEmpty else {
guard !hasErrors else {
throw SwiftFormatError.fileContainsInvalidSyntax
}

return restoringLegacyTriviaBehavior(sourceFile)
}

// Wraps a `DiagnosticMessage` but forces its severity to be that of a warning instead of an error.
struct DowngradedDiagnosticMessage: DiagnosticMessage {
var originalDiagnostic: DiagnosticMessage

var message: String { originalDiagnostic.message }

var diagnosticID: SwiftDiagnostics.MessageID { originalDiagnostic.diagnosticID }

var severity: DiagnosticSeverity { .warning }
}

/// Returns a new `Diagnostic` that is identical to the given diagnostic, except that its severity
/// has been downgraded to a warning.
func downgradedToWarning(_ diagnostic: Diagnostic) -> Diagnostic {
// `Diagnostic` is immutable, so create a new one with the same values except for the
// severity-downgraded message.
return Diagnostic(
node: diagnostic.node,
position: diagnostic.position,
message: DowngradedDiagnosticMessage(originalDiagnostic: diagnostic.diagMessage),
highlights: diagnostic.highlights,
notes: diagnostic.notes,
fixIts: diagnostic.fixIts
)
}

0 comments on commit 2480f73

Please sign in to comment.