Skip to content

Commit 923f1b9

Browse files
committedFeb 4, 2025
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: swiftlang#879
1 parent 22557b7 commit 923f1b9

8 files changed

+43
-15
lines changed
 

‎Sources/SwiftFormat/API/FindingCategorizing.swift

+14-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,23 @@
1717
/// to be displayed as part of the diagnostic message when the finding is presented to the user.
1818
/// For example, the category `Indentation` in the message `[Indentation] Indent by 2 spaces`.
1919
public protocol FindingCategorizing: CustomStringConvertible {
20-
/// The default severity of findings emitted in this category.
20+
/// The severity of findings emitted in this category.
2121
///
22-
/// By default, all findings are warnings. Individual categories may choose to override this to
22+
/// By default, all findings are warnings. Individual categories or configuration may choose to override this to
2323
/// make the findings in those categories more severe.
24-
var defaultSeverity: Finding.Severity { get }
24+
func severity(configuration: Configuration) -> Finding.Severity
25+
26+
/// The name of the category.
27+
var name: String {get}
2528
}
2629

2730
extension FindingCategorizing {
28-
public var defaultSeverity: Finding.Severity { .warning }
31+
func severity(configuration: Configuration) -> Finding.Severity {
32+
return severityFromConfig(configuration: configuration)
33+
}
34+
35+
func severityFromConfig(configuration: Configuration) -> Finding.Severity {
36+
guard let customSeverity = configuration.ruleSeverity[self.name] else { return .warning }
37+
return customSeverity.findingSeverity
38+
}
2939
}

‎Sources/SwiftFormat/Core/FindingEmitter.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ final class FindingEmitter {
4444
_ message: Finding.Message,
4545
category: FindingCategorizing,
4646
location: Finding.Location? = nil,
47-
notes: [Finding.Note] = []
47+
notes: [Finding.Note] = [],
48+
context: Context
4849
) {
4950
guard let consumer = self.consumer else { return }
5051

@@ -54,7 +55,7 @@ final class FindingEmitter {
5455
Finding(
5556
category: category,
5657
message: message,
57-
severity: category.defaultSeverity,
58+
severity: category.severity(configuration: context.configuration),
5859
location: location,
5960
notes: notes
6061
)

‎Sources/SwiftFormat/Core/Rule.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,15 @@ extension Rule {
9393
message,
9494
category: category,
9595
location: syntaxLocation.flatMap(Finding.Location.init),
96-
notes: notes
96+
notes: notes,
97+
context: context
9798
)
9899
}
99100
}
100101

101102
extension Configuration {
102103
func findingSeverity(for rule: any Rule.Type) -> Finding.Severity? {
103104
guard let severity = self.ruleSeverity[rule.ruleName] else { return nil }
104-
switch severity {
105-
case .warning: return .warning
106-
case .error: return .error
107-
}
105+
return severity.findingSeverity
108106
}
109107
}

‎Sources/SwiftFormat/Core/RuleBasedFindingCategory.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@ struct RuleBasedFindingCategory: FindingCategorizing {
2424

2525
var severity: Finding.Severity?
2626

27-
public var defaultSeverity: Finding.Severity {
28-
return severity ?? .warning
27+
var name: String {
28+
return description
2929
}
3030

3131
/// Creates a finding category that wraps the given rule type.
3232
init(ruleType: Rule.Type, severity: Finding.Severity? = nil) {
3333
self.ruleType = ruleType
3434
self.severity = severity
3535
}
36+
37+
func severity(configuration: Configuration) -> Finding.Severity {
38+
if let severity = severity {
39+
return severity
40+
}
41+
return severityFromConfig(configuration: configuration)
42+
}
3643
}

‎Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,8 @@ public class PrettyPrinter {
819819
context.findingEmitter.emit(
820820
message,
821821
category: category,
822-
location: Finding.Location(file: context.fileURL.path, line: outputBuffer.lineNumber, column: column)
822+
location: Finding.Location(file: context.fileURL.path, line: outputBuffer.lineNumber, column: column),
823+
context: context
823824
)
824825
}
825826
}

‎Sources/SwiftFormat/PrettyPrint/PrettyPrintFindingCategory.swift

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
/// Categories for findings emitted by the pretty printer.
1414
enum PrettyPrintFindingCategory: FindingCategorizing {
15+
1516
/// Finding related to an end-of-line comment.
1617
case endOfLineComment
1718

@@ -24,4 +25,9 @@ enum PrettyPrintFindingCategory: FindingCategorizing {
2425
case .trailingComma: return "TrailingComma"
2526
}
2627
}
28+
29+
var name: String {
30+
self.description
31+
}
32+
2733
}

‎Sources/SwiftFormat/PrettyPrint/WhitespaceFindingCategory.swift

+4
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ enum WhitespaceFindingCategory: FindingCategorizing {
4444
case .lineLength: return "LineLength"
4545
}
4646
}
47+
48+
var name: String {
49+
return self.description
50+
}
4751
}

‎Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ public class WhitespaceLinter {
380380
context.findingEmitter.emit(
381381
message,
382382
category: category,
383-
location: Finding.Location(sourceLocation)
383+
location: Finding.Location(sourceLocation),
384+
context: context
384385
)
385386
}
386387

0 commit comments

Comments
 (0)
Please sign in to comment.