@@ -43,18 +43,53 @@ func parseAndEmitDiagnostics(
43
43
operatorTable. foldAll ( Parser . parse ( source: source) ) { _ in } . as ( SourceFileSyntax . self) !
44
44
45
45
let diagnostics = ParseDiagnosticsGenerator . diagnostics ( for: sourceFile)
46
+ var hasErrors = false
46
47
if let parsingDiagnosticHandler = parsingDiagnosticHandler {
47
48
let expectedConverter =
48
49
SourceLocationConverter ( file: url? . path ?? " <unknown> " , tree: sourceFile)
49
50
for diagnostic in diagnostics {
50
51
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
+ }
52
61
}
53
62
}
54
63
55
- guard diagnostics . isEmpty else {
64
+ guard !hasErrors else {
56
65
throw SwiftFormatError . fileContainsInvalidSyntax
57
66
}
58
67
59
68
return restoringLegacyTriviaBehavior ( sourceFile)
60
69
}
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