Skip to content

Commit 90c1390

Browse files
committed
Merge pull request #547 from allevato/downgrade-placeholder-errors
Downgrade `editor placeholder in source file` from error to warning.
1 parent 85bf6cf commit 90c1390

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

Sources/SwiftFormat/Parsing.swift

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,53 @@ func parseAndEmitDiagnostics(
4343
operatorTable.foldAll(Parser.parse(source: source)) { _ in }.as(SourceFileSyntax.self)!
4444

4545
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: sourceFile)
46+
var hasErrors = false
4647
if let parsingDiagnosticHandler = parsingDiagnosticHandler {
4748
let expectedConverter =
4849
SourceLocationConverter(file: url?.path ?? "<unknown>", tree: sourceFile)
4950
for diagnostic in diagnostics {
5051
let location = diagnostic.location(converter: expectedConverter)
51-
parsingDiagnosticHandler(diagnostic, location)
52+
53+
// Downgrade editor placeholders to warnings, because it is useful to support formatting
54+
// in-progress files that contain those.
55+
if diagnostic.diagnosticID == StaticTokenError.editorPlaceholder.diagnosticID {
56+
parsingDiagnosticHandler(downgradedToWarning(diagnostic), location)
57+
} else {
58+
parsingDiagnosticHandler(diagnostic, location)
59+
hasErrors = true
60+
}
5261
}
5362
}
5463

55-
guard diagnostics.isEmpty else {
64+
guard !hasErrors else {
5665
throw SwiftFormatError.fileContainsInvalidSyntax
5766
}
5867

5968
return restoringLegacyTriviaBehavior(sourceFile)
6069
}
70+
71+
// Wraps a `DiagnosticMessage` but forces its severity to be that of a warning instead of an error.
72+
struct DowngradedDiagnosticMessage: DiagnosticMessage {
73+
var originalDiagnostic: DiagnosticMessage
74+
75+
var message: String { originalDiagnostic.message }
76+
77+
var diagnosticID: SwiftDiagnostics.MessageID { originalDiagnostic.diagnosticID }
78+
79+
var severity: DiagnosticSeverity { .warning }
80+
}
81+
82+
/// Returns a new `Diagnostic` that is identical to the given diagnostic, except that its severity
83+
/// has been downgraded to a warning.
84+
func downgradedToWarning(_ diagnostic: Diagnostic) -> Diagnostic {
85+
// `Diagnostic` is immutable, so create a new one with the same values except for the
86+
// severity-downgraded message.
87+
return Diagnostic(
88+
node: diagnostic.node,
89+
position: diagnostic.position,
90+
message: DowngradedDiagnosticMessage(originalDiagnostic: diagnostic.diagMessage),
91+
highlights: diagnostic.highlights,
92+
notes: diagnostic.notes,
93+
fixIts: diagnostic.fixIts
94+
)
95+
}

0 commit comments

Comments
 (0)