Skip to content

Commit cb59b85

Browse files
committed
Add a severity to diagnostics
1 parent faec856 commit cb59b85

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

Sources/Diagnostics/Message.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public struct MessageID: Hashable {
2525
}
2626
}
2727

28+
public enum DiagnosticSeverity {
29+
case error
30+
case warning
31+
case note
32+
}
33+
2834
/// Types conforming to this protocol represent diagnostic messages that can be
2935
/// shown to the client.
3036
public protocol DiagnosticMessage {
@@ -33,4 +39,6 @@ public protocol DiagnosticMessage {
3339

3440
/// See ``MessageID``.
3541
var diagnosticID: MessageID { get }
42+
43+
var severity: DiagnosticSeverity { get }
3644
}

Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
7171
if shouldSkip(node) {
7272
return .skipChildren
7373
}
74-
addDiagnostic(node, UnexpectedNodesDiagnostic(unexpectedNodes: node))
74+
addDiagnostic(node, UnexpectedNodesError(unexpectedNodes: node))
7575
return .skipChildren
7676
}
7777

@@ -80,7 +80,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
8080
return .skipChildren
8181
}
8282
if node.isMissing {
83-
addDiagnostic(node, MissingTokenDiagnostic(missingToken: node))
83+
addDiagnostic(node, MissingTokenError(missingToken: node))
8484
}
8585
return .skipChildren
8686
}
@@ -95,7 +95,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
9595
if let unexpectedCondition = node.body.unexpectedBeforeLeftBrace {
9696
// Detect C-style for loops based on two semicolons which could not be parsed between the 'for' keyword and the '{'
9797
if unexpectedCondition.tokens(withKind: .semicolon).count == 2 {
98-
addDiagnostic(node, CStyleForLoopDiagnostic())
98+
addDiagnostic(node, CStyleForLoopError())
9999
markNodesAsHandled(node.inKeyword.id, unexpectedCondition.id)
100100
}
101101
}
@@ -108,7 +108,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
108108
}
109109
if let output = node.output, let unexpectedBeforeReturnType = output.unexpectedBetweenArrowAndReturnType {
110110
if let throwsInReturnPosition = unexpectedBeforeReturnType.tokens(withKind: .throwsKeyword).first {
111-
addDiagnostic(throwsInReturnPosition, ThrowsInReturnPositionDiagnostic())
111+
addDiagnostic(throwsInReturnPosition, ThrowsInReturnPositionError())
112112
markNodesAsHandled(unexpectedBeforeReturnType.id, throwsInReturnPosition.id)
113113
return .visitChildren
114114
}

Sources/SwiftParser/Diagnostics/ParserDiagnosticMessages.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,32 @@ extension Syntax {
3636
}
3737
}
3838

39-
/// A diagnostic whose ID is determined by the diagnostic's type.
40-
public protocol DiagnosticMessageType: DiagnosticMessage {
39+
/// A error diagnostic whose ID is determined by the diagnostic's type.
40+
public protocol ParserError: DiagnosticMessage {
4141
var diagnosticID: MessageID { get }
4242
}
4343

44-
public extension DiagnosticMessageType {
44+
public extension ParserError {
4545
static var diagnosticID: MessageID {
4646
return MessageID("\(self)")
4747
}
4848

4949
var diagnosticID: MessageID {
5050
return Self.diagnosticID
5151
}
52+
53+
var severity: DiagnosticSeverity {
54+
return .error
55+
}
5256
}
5357

5458
// MARK: - Diagnostics (please sort alphabetically)
5559

56-
public struct CStyleForLoopDiagnostic: DiagnosticMessageType {
60+
public struct CStyleForLoopError: ParserError {
5761
public var message = "C-style for statement has been removed in Swift 3"
5862
}
5963

60-
public struct MissingTokenDiagnostic: DiagnosticMessageType {
64+
public struct MissingTokenError: ParserError {
6165
public let missingToken: TokenSyntax
6266

6367
public var message: String {
@@ -80,11 +84,11 @@ public struct MissingTokenDiagnostic: DiagnosticMessageType {
8084
}
8185
}
8286

83-
public struct ThrowsInReturnPositionDiagnostic: DiagnosticMessageType {
87+
public struct ThrowsInReturnPositionError: ParserError {
8488
public let message = "'throws' may only occur before '->'"
8589
}
8690

87-
public struct UnexpectedNodesDiagnostic: DiagnosticMessageType {
91+
public struct UnexpectedNodesError: ParserError {
8892
public let unexpectedNodes: UnexpectedNodesSyntax
8993

9094
public var message: String {

Tests/SwiftParserTest/DiagnosticInfrastructureTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import SwiftParser
44

55
public class DiagnosticInfrastructureTests: XCTestCase {
66
public func testDiagnosticID() throws {
7-
struct TestDiagnostic: DiagnosticMessageType {
7+
struct TestDiagnostic: ParserError {
88
let message: String = "My test diagnostic"
99
}
1010

1111
let diag = TestDiagnostic()
1212
XCTAssertEqual(diag.diagnosticID, MessageID("TestDiagnostic"))
1313
XCTAssertEqual(diag.message, "My test diagnostic")
14+
XCTAssertEqual(diag.severity, .error)
1415
}
1516
}

Tests/SwiftParserTest/DiagnosticTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class DiagnosticTests: XCTestCase {
1010
"""
1111
let signature = withParser(source: source) { Syntax(raw: $0.parseFunctionSignature().raw) }
1212

13-
XCTAssertSingleDiagnostic(in: signature, line: 1, column: 15, id: MissingTokenDiagnostic.diagnosticID, message: "Expected ':' in function parameter")
13+
XCTAssertSingleDiagnostic(in: signature, line: 1, column: 15, id: MissingTokenError.diagnosticID, message: "Expected ':' in function parameter")
1414
}
1515

1616
public func testUnexpectedDiags() throws {

0 commit comments

Comments
 (0)