forked from swiftlang/swift-format
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request swiftlang#509 from allevato/remove-tsc
Remove the `swift-tools-support-core` dependency from swift-format.
- Loading branch information
Showing
8 changed files
with
254 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftFormatCore | ||
import SwiftSyntax | ||
|
||
/// Diagnostic data that retains the separation of a finding category (if present) from the rest of | ||
/// the message, allowing diagnostic printers that want to print those values separately to do so. | ||
struct Diagnostic { | ||
/// The severity of the diagnostic. | ||
enum Severity { | ||
case note | ||
case warning | ||
case error | ||
} | ||
|
||
/// Represents the location of a diagnostic. | ||
struct Location { | ||
/// The file path associated with the diagnostic. | ||
var file: String | ||
|
||
/// The 1-based line number where the diagnostic occurred. | ||
var line: Int | ||
|
||
/// The 1-based column number where the diagnostic occurred. | ||
var column: Int | ||
|
||
/// Creates a new diagnostic location from the given source location. | ||
init(_ sourceLocation: SourceLocation) { | ||
self.file = sourceLocation.file! | ||
self.line = sourceLocation.line! | ||
self.column = sourceLocation.column! | ||
} | ||
|
||
/// Creates a new diagnostic location with the given finding location. | ||
init(_ findingLocation: Finding.Location) { | ||
self.file = findingLocation.file | ||
self.line = findingLocation.line | ||
self.column = findingLocation.column | ||
} | ||
} | ||
|
||
/// The severity of the diagnostic. | ||
var severity: Severity | ||
|
||
/// The location where the diagnostic occurred, if known. | ||
var location: Location? | ||
|
||
/// The category of the diagnostic, if any. | ||
var category: String? | ||
|
||
/// The message text associated with the diagnostic. | ||
var message: String | ||
|
||
var description: String { | ||
if let category = category { | ||
return "[\(category)] \(message)" | ||
} else { | ||
return message | ||
} | ||
} | ||
|
||
/// Creates a new diagnostic with the given severity, location, optional category, and | ||
/// message. | ||
init(severity: Severity, location: Location?, category: String? = nil, message: String) { | ||
self.severity = severity | ||
self.location = location | ||
self.category = category | ||
self.message = message | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftFormatCore | ||
import SwiftSyntax | ||
import SwiftDiagnostics | ||
|
||
/// Unifies the handling of findings from the linter, parsing errors from the syntax parser, and | ||
/// generic errors from the frontend so that they are emitted in a uniform fashion. | ||
final class DiagnosticsEngine { | ||
/// The handler functions that will be called to process diagnostics that are emitted. | ||
private let handlers: [(Diagnostic) -> Void] | ||
|
||
/// A Boolean value indicating whether any errors were emitted by the diagnostics engine. | ||
private(set) var hasErrors: Bool | ||
|
||
/// A Boolean value indicating whether any warnings were emitted by the diagnostics engine. | ||
private(set) var hasWarnings: Bool | ||
|
||
/// Creates a new diagnostics engine with the given diagnostic handlers. | ||
/// | ||
/// - Parameter diagnosticsHandlers: An array of functions, each of which takes a `Diagnostic` as | ||
/// its sole argument and returns `Void`. The functions are called whenever a diagnostic is | ||
/// received by the engine. | ||
init(diagnosticsHandlers: [(Diagnostic) -> Void]) { | ||
self.handlers = diagnosticsHandlers | ||
self.hasErrors = false | ||
self.hasWarnings = false | ||
} | ||
|
||
/// Emits the diagnostic by passing it to the registered handlers, and tracks whether it was an | ||
/// error or warning diagnostic. | ||
private func emit(_ diagnostic: Diagnostic) { | ||
switch diagnostic.severity { | ||
case .error: self.hasErrors = true | ||
case .warning: self.hasWarnings = true | ||
default: break | ||
} | ||
|
||
for handler in handlers { | ||
handler(diagnostic) | ||
} | ||
} | ||
|
||
/// Emits a generic error message. | ||
/// | ||
/// - Parameters: | ||
/// - message: The message associated with the error. | ||
/// - location: The location in the source code associated with the error, or nil if there is no | ||
/// location associated with the error. | ||
func emitError(_ message: String, location: SourceLocation? = nil) { | ||
emit( | ||
Diagnostic( | ||
severity: .error, | ||
location: location.map(Diagnostic.Location.init), | ||
message: message)) | ||
} | ||
|
||
/// Emits a finding from the linter and any of its associated notes as diagnostics. | ||
/// | ||
/// - Parameter finding: The finding that should be emitted. | ||
func consumeFinding(_ finding: Finding) { | ||
emit(diagnosticMessage(for: finding)) | ||
|
||
for note in finding.notes { | ||
emit( | ||
Diagnostic( | ||
severity: .note, | ||
location: note.location.map(Diagnostic.Location.init), | ||
message: "\(note.message)")) | ||
} | ||
} | ||
|
||
/// Emits a diagnostic from the syntax parser and any of its associated notes. | ||
/// | ||
/// - Parameter diagnostic: The syntax parser diagnostic that should be emitted. | ||
func consumeParserDiagnostic( | ||
_ diagnostic: SwiftDiagnostics.Diagnostic, | ||
_ location: SourceLocation | ||
) { | ||
emit(diagnosticMessage(for: diagnostic.diagMessage, at: location)) | ||
} | ||
|
||
/// Converts a diagnostic message from the syntax parser into a diagnostic message that can be | ||
/// used by the `TSCBasic` diagnostics engine and returns it. | ||
private func diagnosticMessage( | ||
for message: SwiftDiagnostics.DiagnosticMessage, | ||
at location: SourceLocation | ||
) -> Diagnostic { | ||
let severity: Diagnostic.Severity | ||
switch message.severity { | ||
case .error: severity = .error | ||
case .warning: severity = .warning | ||
case .note: severity = .note | ||
} | ||
return Diagnostic( | ||
severity: severity, | ||
location: Diagnostic.Location(location), | ||
category: nil, | ||
message: message.message) | ||
} | ||
|
||
/// Converts a lint finding into a diagnostic message that can be used by the `TSCBasic` | ||
/// diagnostics engine and returns it. | ||
private func diagnosticMessage(for finding: Finding) -> Diagnostic { | ||
let severity: Diagnostic.Severity | ||
switch finding.severity { | ||
case .error: severity = .error | ||
case .warning: severity = .warning | ||
} | ||
return Diagnostic( | ||
severity: severity, | ||
location: finding.location.map(Diagnostic.Location.init), | ||
category: "\(finding.category)", | ||
message: "\(finding.message.text)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
/// Returns a value indicating whether or not the stream is a TTY. | ||
func isTTY(_ fileHandle: FileHandle) -> Bool { | ||
// The implementation of this function is adapted from `TerminalController.swift` in | ||
// swift-tools-support-core. | ||
#if os(Windows) | ||
// The TSC implementation of this function only returns `.file` or `.dumb` for Windows, | ||
// neither of which is a TTY. | ||
return false | ||
#else | ||
if ProcessInfo.processInfo.environment["TERM"] == "dumb" { | ||
return false | ||
} | ||
return isatty(fileHandle.fileDescriptor) != 0 | ||
#endif | ||
} |
Oops, something went wrong.