Skip to content

Add a way for emittable diagnostics (e.g. Swift.Errors) to provide custom DiagnosticLocations #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/TSCUtility/Diagnostics.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
Expand All @@ -18,6 +18,13 @@ public protocol DiagnosticDataConvertible {
var diagnosticData: DiagnosticData { get }
}


/// Protocol for types that can provide a diagnostic location. A common use is
/// for specializations of Swift.Error that can have diagnostic locations.
public protocol DiagnosticLocationProviding {
var diagnosticLocation: DiagnosticLocation? { get }
}

/// DiagnosticData wrapper for Swift errors.
public struct AnyDiagnostic: DiagnosticData {
public let anyError: Swift.Error
Expand Down Expand Up @@ -69,6 +76,7 @@ extension DiagnosticsEngine {
_ error: Swift.Error,
location: DiagnosticLocation? = nil
) {
let location = location ?? (error as? DiagnosticLocationProviding)?.diagnosticLocation
if let diagnosticData = error as? DiagnosticData {
emit(.error(diagnosticData), location: location)
} else if case let convertible as DiagnosticDataConvertible = error {
Expand Down
60 changes: 60 additions & 0 deletions Tests/TSCUtilityTests/DiagnosticsUtilityTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2020 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest

import TSCBasic
import TSCUtility

class DiagnosticsTests: XCTestCase {

func testDiagnosticsLocationProviding() throws {
let diagnostics = DiagnosticsEngine()

struct BazLocation: DiagnosticLocation {
let name: String

var description: String {
return name
}
}

struct CustomError: Error, CustomStringConvertible, DiagnosticLocationProviding {
var location: String?
var diagnosticLocation: DiagnosticLocation? {
return location.flatMap{ BazLocation(name: $0) }
}
var description: String {
return "provided location is '\(location ?? "nil")'"
}
}

diagnostics.with(location: BazLocation(name: "elsewhere")) { diagnostics in
diagnostics.wrap {
throw CustomError(location: "somewhere")
}
diagnostics.wrap {
throw CustomError(location: nil)
}
}

XCTAssertEqual(diagnostics.diagnostics.count, 2)

let firstDiagnostic = try XCTUnwrap(diagnostics.diagnostics.first)
XCTAssertEqual(firstDiagnostic.location.description, "somewhere")
XCTAssertEqual(firstDiagnostic.description, "provided location is 'somewhere'")
XCTAssertEqual(firstDiagnostic.message.behavior, .error)

let secondDiagnostic = try XCTUnwrap(diagnostics.diagnostics.last)
XCTAssertEqual(secondDiagnostic.location.description, "elsewhere")
XCTAssertEqual(secondDiagnostic.description, "provided location is 'nil'")
XCTAssertEqual(secondDiagnostic.message.behavior, .error)
}
}