Skip to content

Commit

Permalink
Introduce SeverityBasedRuleConfiguration to avoid custom `makeViola…
Browse files Browse the repository at this point in the history
…tion`s (realm#4274)
  • Loading branch information
SimplyDanny authored Oct 4, 2022
1 parent e3f1b56 commit ab3f070
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 58 deletions.
13 changes: 13 additions & 0 deletions Source/SwiftLintFramework/Protocols/RuleConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public protocol RuleConfiguration {
func isEqualTo(_ ruleConfiguration: RuleConfiguration) -> Bool
}

/// A configuration for a rule that allows to configure at least the severity.
public protocol SeverityBasedRuleConfiguration: RuleConfiguration {
/// The configuration of a rule's severity.
var severityConfiguration: SeverityConfiguration { get }
}

public extension SeverityBasedRuleConfiguration {
/// The severity of a rule.
var severity: ViolationSeverity {
severityConfiguration.severity
}
}

public extension RuleConfiguration where Self: Equatable {
func isEqualTo(_ ruleConfiguration: RuleConfiguration) -> Bool {
return self == ruleConfiguration as? Self
Expand Down
3 changes: 2 additions & 1 deletion Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public protocol SwiftSyntaxRule: SourceKitFreeRule {
func makeViolation(file: SwiftLintFile, position: AbsolutePosition) -> StyleViolation
}

public extension SwiftSyntaxRule where Self: ConfigurationProviderRule, ConfigurationType == SeverityConfiguration {
public extension SwiftSyntaxRule where Self: ConfigurationProviderRule,
ConfigurationType: SeverityBasedRuleConfiguration {
func makeViolation(file: SwiftLintFile, position: AbsolutePosition) -> StyleViolation {
StyleViolation(
ruleDescription: Self.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ public struct DiscouragedObjectLiteralRule: SwiftSyntaxRule, OptInRule, Configur
public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor(configuration: configuration)
}

public func makeViolation(file: SwiftLintFile, position: AbsolutePosition) -> StyleViolation {
StyleViolation(
ruleDescription: Self.description,
severity: configuration.severityConfiguration.severity,
location: Location(file: file, position: position)
)
}
}

private extension DiscouragedObjectLiteralRule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public struct UnusedDeclarationRule: ConfigurationProviderRule, AnalyzerRule, Co
allReferencedUSRs: allReferencedUSRs)
.map {
StyleViolation(ruleDescription: Self.description,
severity: configuration.severity,
severity: configuration.severityConfiguration.severity,
location: Location(file: file, byteOffset: $0))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ private func toExplicitInitMethod(typeName: String) -> String {
return "\(typeName).init"
}

public struct DiscouragedDirectInitConfiguration: RuleConfiguration, Equatable {
public struct DiscouragedDirectInitConfiguration: SeverityBasedRuleConfiguration, Equatable {
public var severityConfiguration = SeverityConfiguration(.warning)

public var consoleDescription: String {
return severityConfiguration.consoleDescription + ", types: \(discouragedInits.sorted(by: <))"
}

public var severity: ViolationSeverity {
return severityConfiguration.severity
}

public private(set) var discouragedInits: Set<String>

private let defaultDiscouragedInits = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ private enum ConfigurationKey: String {
case overrideAllowedTerms = "override_allowed_terms"
}

public struct InclusiveLanguageConfiguration: RuleConfiguration, Equatable {
public struct InclusiveLanguageConfiguration: SeverityBasedRuleConfiguration, Equatable {
public var severityConfiguration = SeverityConfiguration(.warning)
public var additionalTerms: Set<String>?
public var overrideTerms: Set<String>?
Expand All @@ -20,10 +20,6 @@ public struct InclusiveLanguageConfiguration: RuleConfiguration, Equatable {
+ ", override_allowed_terms: \(overrideAllowedTerms?.sorted() ?? [])"
}

public var severity: ViolationSeverity {
severityConfiguration.severity
}

private let defaultTerms: Set<String> = [
"whitelist",
"blacklist",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public struct ObjectLiteralConfiguration: RuleConfiguration, Equatable {
private(set) var severityConfiguration = SeverityConfiguration(.warning)
public struct ObjectLiteralConfiguration: SeverityBasedRuleConfiguration, Equatable {
public private(set) var severityConfiguration = SeverityConfiguration(.warning)
private(set) var imageLiteral = true
private(set) var colorLiteral = true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public struct OverriddenSuperCallConfiguration: RuleConfiguration, Equatable {
public struct OverriddenSuperCallConfiguration: SeverityBasedRuleConfiguration, Equatable {
private let defaultIncluded = [
// NSObject
"awakeFromNib()",
Expand Down Expand Up @@ -34,7 +34,7 @@ public struct OverriddenSuperCallConfiguration: RuleConfiguration, Equatable {
"tearDownWithError()"
]

var severityConfiguration = SeverityConfiguration(.warning)
public private(set) var severityConfiguration = SeverityConfiguration(.warning)
var excluded: [String] = []
var included: [String] = ["*"]

Expand Down Expand Up @@ -70,10 +70,6 @@ public struct OverriddenSuperCallConfiguration: RuleConfiguration, Equatable {
resolvedMethodNames = calculateResolvedMethodNames()
}

public var severity: ViolationSeverity {
return severityConfiguration.severity
}

private func calculateResolvedMethodNames() -> [String] {
var names: [String] = []
if included.contains("*") && !excluded.contains("*") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import Foundation

public struct PrivateUnitTestConfiguration: RuleConfiguration, Equatable, CacheDescriptionProvider {
public struct PrivateUnitTestConfiguration: SeverityBasedRuleConfiguration, Equatable, CacheDescriptionProvider {
public let identifier: String
public var name: String?
public var message = "Regex matched."
public var regex: NSRegularExpression!
public var included: NSRegularExpression?
public var severityConfiguration = SeverityConfiguration(.warning)

public var severity: ViolationSeverity {
return severityConfiguration.severity
}

public var consoleDescription: String {
return "\(severity.rawValue): \(regex.pattern)"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public struct ProhibitedSuperConfiguration: RuleConfiguration, Equatable {
var severityConfiguration = SeverityConfiguration(.warning)
public struct ProhibitedSuperConfiguration: SeverityBasedRuleConfiguration, Equatable {
public private(set) var severityConfiguration = SeverityConfiguration(.warning)
var excluded = [String]()
var included = ["*"]

Expand Down Expand Up @@ -42,10 +42,6 @@ public struct ProhibitedSuperConfiguration: RuleConfiguration, Equatable {
resolvedMethodNames = calculateResolvedMethodNames()
}

public var severity: ViolationSeverity {
return severityConfiguration.severity
}

private func calculateResolvedMethodNames() -> [String] {
var names = [String]()
if included.contains("*") && !excluded.contains("*") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import SourceKittenFramework

public struct RegexConfiguration: RuleConfiguration, Hashable, CacheDescriptionProvider {
public struct RegexConfiguration: SeverityBasedRuleConfiguration, Hashable, CacheDescriptionProvider {
public let identifier: String
public var name: String?
public var message = "Regex matched."
Expand All @@ -12,10 +12,6 @@ public struct RegexConfiguration: RuleConfiguration, Hashable, CacheDescriptionP
public var severityConfiguration = SeverityConfiguration(.warning)
public var captureGroup: Int = 0

public var severity: ViolationSeverity {
return severityConfiguration.severity
}

public var consoleDescription: String {
return "\(severity.rawValue): \(regex.pattern)"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
public struct SeverityConfiguration: RuleConfiguration, Equatable {
public struct SeverityConfiguration: SeverityBasedRuleConfiguration, Equatable {
public var consoleDescription: String {
return severity.rawValue
}

var severity: ViolationSeverity

public var severityConfiguration: SeverityConfiguration {
self
}

public init(_ severity: ViolationSeverity) {
self.severity = severity
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public struct TestCaseAccessibilityConfiguration: RuleConfiguration, Equatable {
public struct TestCaseAccessibilityConfiguration: SeverityBasedRuleConfiguration, Equatable {
public private(set) var severityConfiguration = SeverityConfiguration(.warning)
public private(set) var allowedPrefixes: Set<String> = []
public private(set) var testParentClasses: Set<String> = ["XCTestCase"]
Expand Down Expand Up @@ -26,8 +26,4 @@ public struct TestCaseAccessibilityConfiguration: RuleConfiguration, Equatable {
self.testParentClasses.formUnion(extraTestParentClasses)
}
}

public var severity: ViolationSeverity {
return severityConfiguration.severity
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ private enum ConfigurationKey: String {

public struct UnusedDeclarationConfiguration: RuleConfiguration, Equatable {
private(set) var includePublicAndOpen: Bool
private(set) var severity: ViolationSeverity
private(set) var severityConfiguration: SeverityConfiguration
private(set) var relatedUSRsToSkip: Set<String>

public var consoleDescription: String {
return "\(ConfigurationKey.severity.rawValue): \(severity.rawValue), " +
return "\(ConfigurationKey.severity.rawValue): \(severityConfiguration.severity.rawValue), " +
"\(ConfigurationKey.includePublicAndOpen.rawValue): \(includePublicAndOpen), " +
"\(ConfigurationKey.relatedUSRsToSkip.rawValue): \(relatedUSRsToSkip.sorted())"
}

public init(severity: ViolationSeverity, includePublicAndOpen: Bool, relatedUSRsToSkip: Set<String>) {
self.includePublicAndOpen = includePublicAndOpen
self.severity = severity
self.severityConfiguration = SeverityConfiguration(severity)
self.relatedUSRsToSkip = relatedUSRsToSkip
}

Expand All @@ -32,11 +32,7 @@ public struct UnusedDeclarationConfiguration: RuleConfiguration, Equatable {
}
switch (key, value) {
case (.severity, let stringValue as String):
if let severityValue = ViolationSeverity(rawValue: stringValue) {
severity = severityValue
} else {
throw ConfigurationError.unknownConfiguration
}
try severityConfiguration.apply(configuration: [key: stringValue])
case (.includePublicAndOpen, let boolValue as Bool):
includePublicAndOpen = boolValue
case (.relatedUSRsToSkip, let value):
Expand Down

0 comments on commit ab3f070

Please sign in to comment.