Skip to content

Commit

Permalink
Allow to customize Severity for formating rules
Browse files Browse the repository at this point in the history
In order to check during CI for propper formatting,
it is now also possible to specify severity for
the formating rules.

Issue: swiftlang#879
  • Loading branch information
bkolb committed Nov 22, 2024
1 parent 0898126 commit 080535c
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 15 deletions.
9 changes: 9 additions & 0 deletions Sources/SwiftFormat/API/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
18 changes: 14 additions & 4 deletions Sources/SwiftFormat/API/FindingCategorizing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 3 additions & 2 deletions Sources/SwiftFormat/Core/FindingEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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
)
Expand Down
8 changes: 3 additions & 5 deletions Sources/SwiftFormat/Core/Rule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,15 @@ extension Rule {
message,
category: category,
location: syntaxLocation.flatMap(Finding.Location.init),
notes: notes
notes: notes,
context: context
)
}
}

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
}
}
11 changes: 9 additions & 2 deletions Sources/SwiftFormat/Core/RuleBasedFindingCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ 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.
init(ruleType: Rule.Type, severity: Finding.Severity? = nil) {
self.ruleType = ruleType
self.severity = severity
}

func severity(configuration: Configuration) -> Finding.Severity {
if let severity = severity {
return severity
}
return severityFromConfig(configuration: configuration)
}
}
3 changes: 2 additions & 1 deletion Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,4 +25,9 @@ enum PrettyPrintFindingCategory: FindingCategorizing {
case .trailingComma: return "TrailingComma"
}
}

var name: String {
self.description
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ enum WhitespaceFindingCategory: FindingCategorizing {
case .lineLength: return "LineLength"
}
}

var name: String {
return self.description
}
}
3 changes: 2 additions & 1 deletion Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ public class WhitespaceLinter {
context.findingEmitter.emit(
message,
category: category,
location: Finding.Location(sourceLocation)
location: Finding.Location(sourceLocation),
context: context
)
}

Expand Down

0 comments on commit 080535c

Please sign in to comment.