Skip to content

Commit 77e3acc

Browse files
committed
[Diagnostics formatter] Consistently apply diagnostic kinds (note/warning/error)
Address two issues regarding the diagnostic type indicators in formatted diagnostics. 1. When colors were disabled, we wouldn't get any of the diagnostic type indicators because the "colorize" check came too early. Make sure they're consistently applied whether colorized or not. 2. There was no "note:" prefix, so it wasn't clear which things were notes.
1 parent 1b8d5e9 commit 77e3acc

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,20 @@ public struct DiagnosticsFormatter {
327327
/// Annotates the given ``DiagnosticMessage`` with an appropriate ANSI color code (if the value of the `colorize`
328328
/// property is `true`) and returns the result as a printable string.
329329
private func colorizeIfRequested(_ message: DiagnosticMessage) -> String {
330-
guard colorize else {
331-
return message.message
332-
}
333-
334330
switch message.severity {
335331
case .error:
336332
let annotation = ANSIAnnotation(color: .red, trait: .bold)
337-
return annotation.applied(to: "error: \(message.message)")
333+
return colorizeIfRequested("error: \(message.message)", annotation: annotation)
334+
338335
case .warning:
339336
let color = ANSIAnnotation(color: .yellow)
340-
let prefix = color.withTrait(.bold).applied(to: "warning: ")
341-
return prefix + color.applied(to: message.message)
337+
let prefix = colorizeIfRequested("warning: ", annotation: color.withTrait(.bold))
338+
339+
return prefix + colorizeIfRequested(message.message, annotation: color);
342340
case .note:
343-
return message.message
341+
let color = ANSIAnnotation(color: .default, trait: .bold)
342+
let prefix = colorizeIfRequested("note: ", annotation: color)
343+
return prefix + message.message
344344
}
345345
}
346346

Tests/SwiftDiagnosticsTest/DiagnosticsFormatterTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class DiagnosticsFormatterTests: XCTestCase {
3030
"""
3131
let expectedOutput = """
3232
1 │ var foo = bar +
33-
∣ ╰─ expected expression after operator
33+
∣ ╰─ error: expected expression after operator
3434
3535
"""
3636
AssertStringsEqualWithDiff(annotate(source: source), expectedOutput)
@@ -42,9 +42,9 @@ final class DiagnosticsFormatterTests: XCTestCase {
4242
"""
4343
let expectedOutput = """
4444
1 │ foo.[].[].[]
45-
∣ │ │ ╰─ expected name in member access
46-
∣ │ ╰─ expected name in member access
47-
∣ ╰─ expected name in member access
45+
∣ │ │ ╰─ error: expected name in member access
46+
∣ │ ╰─ error: expected name in member access
47+
∣ ╰─ error: expected name in member access
4848
4949
"""
5050
AssertStringsEqualWithDiff(annotate(source: source), expectedOutput)
@@ -68,14 +68,14 @@ final class DiagnosticsFormatterTests: XCTestCase {
6868
2 │ i = 2
6969
3 │ i = foo(
7070
4 │ i = 4
71-
∣ ╰─ expected ')' to end function call
71+
∣ ╰─ error: expected ')' to end function call
7272
5 │ i = 5
7373
6 │ i = 6
7474
7575
9 │ i = 9
7676
10 │ i = 10
7777
11 │ i = bar(
78-
∣ ╰─ expected value and ')' to end function call
78+
∣ ╰─ error: expected value and ')' to end function call
7979
8080
"""
8181
AssertStringsEqualWithDiff(annotate(source: source), expectedOutput)
@@ -86,8 +86,8 @@ final class DiagnosticsFormatterTests: XCTestCase {
8686

8787
let expectedOutput = """
8888
1 │ t as (..)
89-
∣ ├─ expected type in tuple type
90-
∣ ╰─ unexpected code '..' in tuple type
89+
∣ ├─ error: expected type in tuple type
90+
∣ ╰─ error: unexpected code '..' in tuple type
9191
9292
"""
9393

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
114114
=== main.swift ===
115115
1 │ let pi = 3.14159
116116
2 │ #myAssert(pi == 3)
117-
∣ ╰─ in expansion of macro 'myAssert' here
117+
∣ ╰─ note: in expansion of macro 'myAssert' here
118118
╭─── #myAssert ───────────────────────────────────────────────────────
119119
│1 │ let __a = pi
120120
│2 │ let __b = 3
121121
│3 │ if !(__a == __b) {
122-
│ ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
122+
│ ∣ ╰─ error: no matching operator '==' for types 'Double' and 'Int'
123123
│4 │ fatalError("assertion failed: pi != 3")
124124
│5 │ }
125125
╰─────────────────────────────────────────────────────────────────────
126126
3 │ print("hello"
127-
∣ ╰─ expected ')' to end function call
127+
∣ ╰─ error: expected ')' to end function call
128128
129129
"""
130130
)
@@ -183,21 +183,21 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
183183
=== main.swift ===
184184
1 │ let pi = 3.14159
185185
2 │ #myAssert(pi == 3)
186-
∣ ╰─ in expansion of macro 'myAssert' here
186+
∣ ╰─ note: in expansion of macro 'myAssert' here
187187
╭─── #myAssert ───────────────────────────────────────────────────────
188188
│1 │ let __a = pi
189189
│2 │ let __b = 3
190190
│3 │ if #invertedEqualityCheck(__a, __b) {
191-
│ ∣ ╰─ in expansion of macro 'invertedEqualityCheck' here
191+
│ ∣ ╰─ note: in expansion of macro 'invertedEqualityCheck' here
192192
│ ╭─── #invertedEqualityCheck ───────────────────────────────────────
193193
│ │1 │ !(__a == __b)
194-
│ │ ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
194+
│ │ ∣ ╰─ error: no matching operator '==' for types 'Double' and 'Int'
195195
│ ╰──────────────────────────────────────────────────────────────────
196196
│4 │ fatalError("assertion failed: pi != 3")
197197
│5 │ }
198198
╰─────────────────────────────────────────────────────────────────────
199199
3 │ print("hello"
200-
∣ ╰─ expected ')' to end function call
200+
∣ ╰─ error: expected ')' to end function call
201201
202202
"""
203203
)

0 commit comments

Comments
 (0)