From 080535c5cc2685295ba8afc7f42922d929ef7918 Mon Sep 17 00:00:00 2001 From: Bernd Kolb Date: Fri, 22 Nov 2024 15:17:04 +0100 Subject: [PATCH] Allow to customize Severity for formating rules In order to check during CI for propper formatting, it is now also possible to specify severity for the formating rules. Issue: #879 --- Sources/SwiftFormat/API/Configuration.swift | 9 +++++++++ .../SwiftFormat/API/FindingCategorizing.swift | 18 ++++++++++++++---- Sources/SwiftFormat/Core/FindingEmitter.swift | 5 +++-- Sources/SwiftFormat/Core/Rule.swift | 8 +++----- .../Core/RuleBasedFindingCategory.swift | 11 +++++++++-- .../SwiftFormat/PrettyPrint/PrettyPrint.swift | 3 ++- .../PrettyPrintFindingCategory.swift | 6 ++++++ .../WhitespaceFindingCategory.swift | 4 ++++ .../PrettyPrint/WhitespaceLinter.swift | 3 ++- 9 files changed, 52 insertions(+), 15 deletions(-) diff --git a/Sources/SwiftFormat/API/Configuration.swift b/Sources/SwiftFormat/API/Configuration.swift index 966e29bc1..d04ef4a49 100644 --- a/Sources/SwiftFormat/API/Configuration.swift +++ b/Sources/SwiftFormat/API/Configuration.swift @@ -513,3 +513,12 @@ fileprivate extension URL { #endif } } + +extension Configuration.RuleSeverity { + var findingSeverity: Finding.Severity { + switch self { + case .warning: return .warning + case .error: return .error + } + } +} diff --git a/Sources/SwiftFormat/API/FindingCategorizing.swift b/Sources/SwiftFormat/API/FindingCategorizing.swift index 46ad563ff..416bca6ca 100644 --- a/Sources/SwiftFormat/API/FindingCategorizing.swift +++ b/Sources/SwiftFormat/API/FindingCategorizing.swift @@ -17,13 +17,23 @@ /// to be displayed as part of the diagnostic message when the finding is presented to the user. /// For example, the category `Indentation` in the message `[Indentation] Indent by 2 spaces`. public protocol FindingCategorizing: CustomStringConvertible { - /// The default severity of findings emitted in this category. + /// The severity of findings emitted in this category. /// - /// By default, all findings are warnings. Individual categories may choose to override this to + /// By default, all findings are warnings. Individual categories or configuration may choose to override this to /// make the findings in those categories more severe. - var defaultSeverity: Finding.Severity { get } + func severity(configuration: Configuration) -> Finding.Severity + + /// The name of the category. + var name: String {get} } extension FindingCategorizing { - public var defaultSeverity: Finding.Severity { .warning } + func severity(configuration: Configuration) -> Finding.Severity { + return severityFromConfig(configuration: configuration) + } + + func severityFromConfig(configuration: Configuration) -> Finding.Severity { + guard let customSeverity = configuration.ruleSeverity[self.name] else { return .warning } + return customSeverity.findingSeverity + } } diff --git a/Sources/SwiftFormat/Core/FindingEmitter.swift b/Sources/SwiftFormat/Core/FindingEmitter.swift index b42e101f1..2b0e97b8a 100644 --- a/Sources/SwiftFormat/Core/FindingEmitter.swift +++ b/Sources/SwiftFormat/Core/FindingEmitter.swift @@ -44,7 +44,8 @@ final class FindingEmitter { _ message: Finding.Message, category: FindingCategorizing, location: Finding.Location? = nil, - notes: [Finding.Note] = [] + notes: [Finding.Note] = [], + context: Context ) { guard let consumer = self.consumer else { return } @@ -54,7 +55,7 @@ final class FindingEmitter { Finding( category: category, message: message, - severity: category.defaultSeverity, + severity: category.severity(configuration: context.configuration), location: location, notes: notes ) diff --git a/Sources/SwiftFormat/Core/Rule.swift b/Sources/SwiftFormat/Core/Rule.swift index 190e93b7e..ef34b4fcb 100644 --- a/Sources/SwiftFormat/Core/Rule.swift +++ b/Sources/SwiftFormat/Core/Rule.swift @@ -93,7 +93,8 @@ extension Rule { message, category: category, location: syntaxLocation.flatMap(Finding.Location.init), - notes: notes + notes: notes, + context: context ) } } @@ -101,9 +102,6 @@ extension Rule { extension Configuration { func findingSeverity(for rule: any Rule.Type) -> Finding.Severity? { guard let severity = self.ruleSeverity[rule.ruleName] else { return nil } - switch severity { - case .warning: return .warning - case .error: return .error - } + return severity.findingSeverity } } diff --git a/Sources/SwiftFormat/Core/RuleBasedFindingCategory.swift b/Sources/SwiftFormat/Core/RuleBasedFindingCategory.swift index 42521b236..117508f09 100644 --- a/Sources/SwiftFormat/Core/RuleBasedFindingCategory.swift +++ b/Sources/SwiftFormat/Core/RuleBasedFindingCategory.swift @@ -24,8 +24,8 @@ struct RuleBasedFindingCategory: FindingCategorizing { var severity: Finding.Severity? - public var defaultSeverity: Finding.Severity { - return severity ?? .warning + var name: String { + return description } /// Creates a finding category that wraps the given rule type. @@ -33,4 +33,11 @@ struct RuleBasedFindingCategory: FindingCategorizing { self.ruleType = ruleType self.severity = severity } + + func severity(configuration: Configuration) -> Finding.Severity { + if let severity = severity { + return severity + } + return severityFromConfig(configuration: configuration) + } } diff --git a/Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift b/Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift index 607364ee8..8f8f5b9d1 100644 --- a/Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift +++ b/Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift @@ -819,7 +819,8 @@ public class PrettyPrinter { context.findingEmitter.emit( message, category: category, - location: Finding.Location(file: context.fileURL.path, line: outputBuffer.lineNumber, column: column) + location: Finding.Location(file: context.fileURL.path, line: outputBuffer.lineNumber, column: column), + context: context ) } } diff --git a/Sources/SwiftFormat/PrettyPrint/PrettyPrintFindingCategory.swift b/Sources/SwiftFormat/PrettyPrint/PrettyPrintFindingCategory.swift index ee81342f0..9e9f9e5df 100644 --- a/Sources/SwiftFormat/PrettyPrint/PrettyPrintFindingCategory.swift +++ b/Sources/SwiftFormat/PrettyPrint/PrettyPrintFindingCategory.swift @@ -12,6 +12,7 @@ /// Categories for findings emitted by the pretty printer. enum PrettyPrintFindingCategory: FindingCategorizing { + /// Finding related to an end-of-line comment. case endOfLineComment @@ -24,4 +25,9 @@ enum PrettyPrintFindingCategory: FindingCategorizing { case .trailingComma: return "TrailingComma" } } + + var name: String { + self.description + } + } diff --git a/Sources/SwiftFormat/PrettyPrint/WhitespaceFindingCategory.swift b/Sources/SwiftFormat/PrettyPrint/WhitespaceFindingCategory.swift index bc50fb38f..c153c0aea 100644 --- a/Sources/SwiftFormat/PrettyPrint/WhitespaceFindingCategory.swift +++ b/Sources/SwiftFormat/PrettyPrint/WhitespaceFindingCategory.swift @@ -44,4 +44,8 @@ enum WhitespaceFindingCategory: FindingCategorizing { case .lineLength: return "LineLength" } } + + var name: String { + return self.description + } } diff --git a/Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift b/Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift index c5c5e2ae8..62a83b15c 100644 --- a/Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift +++ b/Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift @@ -373,7 +373,8 @@ public class WhitespaceLinter { context.findingEmitter.emit( message, category: category, - location: Finding.Location(sourceLocation) + location: Finding.Location(sourceLocation), + context: context ) }