From ab3f070222b4a61873286a9784f6b03bd54fa6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Tue, 4 Oct 2022 07:39:07 +0200 Subject: [PATCH] Introduce `SeverityBasedRuleConfiguration` to avoid custom `makeViolation`s (#4274) --- .../Protocols/RuleConfiguration.swift | 13 +++++++++++++ .../Protocols/SwiftSyntaxRule.swift | 3 ++- .../Idiomatic/DiscouragedObjectLiteralRule.swift | 8 -------- .../Rules/Lint/UnusedDeclarationRule.swift | 2 +- .../DiscouragedDirectInitConfiguration.swift | 6 +----- .../InclusiveLanguageConfiguration.swift | 6 +----- .../ObjectLiteralConfiguration.swift | 4 ++-- .../OverriddenSuperCallConfiguration.swift | 8 ++------ .../PrivateUnitTestConfiguration.swift | 6 +----- .../ProhibitedSuperConfiguration.swift | 8 ++------ .../RuleConfigurations/RegexConfiguration.swift | 6 +----- .../RuleConfigurations/SeverityConfiguration.swift | 6 +++++- .../TestCaseAccessibilityConfiguration.swift | 6 +----- .../UnusedDeclarationConfiguration.swift | 12 ++++-------- 14 files changed, 36 insertions(+), 58 deletions(-) diff --git a/Source/SwiftLintFramework/Protocols/RuleConfiguration.swift b/Source/SwiftLintFramework/Protocols/RuleConfiguration.swift index 0ea7bc8ccd..d0adfe1e55 100644 --- a/Source/SwiftLintFramework/Protocols/RuleConfiguration.swift +++ b/Source/SwiftLintFramework/Protocols/RuleConfiguration.swift @@ -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 diff --git a/Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift b/Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift index b00a29a3d8..9857e0bfa8 100644 --- a/Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift +++ b/Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift @@ -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, diff --git a/Source/SwiftLintFramework/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift b/Source/SwiftLintFramework/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift index 8717c3816c..d868af44b9 100644 --- a/Source/SwiftLintFramework/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift +++ b/Source/SwiftLintFramework/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift @@ -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 { diff --git a/Source/SwiftLintFramework/Rules/Lint/UnusedDeclarationRule.swift b/Source/SwiftLintFramework/Rules/Lint/UnusedDeclarationRule.swift index b32abd4a49..b3ff7565a0 100644 --- a/Source/SwiftLintFramework/Rules/Lint/UnusedDeclarationRule.swift +++ b/Source/SwiftLintFramework/Rules/Lint/UnusedDeclarationRule.swift @@ -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)) } } diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/DiscouragedDirectInitConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/DiscouragedDirectInitConfiguration.swift index 431eaa93a8..a03b076d2f 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/DiscouragedDirectInitConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/DiscouragedDirectInitConfiguration.swift @@ -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 private let defaultDiscouragedInits = [ diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/InclusiveLanguageConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/InclusiveLanguageConfiguration.swift index fcac2b44dc..4fd93e9ef2 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/InclusiveLanguageConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/InclusiveLanguageConfiguration.swift @@ -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? public var overrideTerms: Set? @@ -20,10 +20,6 @@ public struct InclusiveLanguageConfiguration: RuleConfiguration, Equatable { + ", override_allowed_terms: \(overrideAllowedTerms?.sorted() ?? [])" } - public var severity: ViolationSeverity { - severityConfiguration.severity - } - private let defaultTerms: Set = [ "whitelist", "blacklist", diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/ObjectLiteralConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/ObjectLiteralConfiguration.swift index 2aafac3a22..96bc4c62d3 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/ObjectLiteralConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/ObjectLiteralConfiguration.swift @@ -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 diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/OverriddenSuperCallConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/OverriddenSuperCallConfiguration.swift index 97e0abf92b..6b8cab1c75 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/OverriddenSuperCallConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/OverriddenSuperCallConfiguration.swift @@ -1,4 +1,4 @@ -public struct OverriddenSuperCallConfiguration: RuleConfiguration, Equatable { +public struct OverriddenSuperCallConfiguration: SeverityBasedRuleConfiguration, Equatable { private let defaultIncluded = [ // NSObject "awakeFromNib()", @@ -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] = ["*"] @@ -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("*") { diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/PrivateUnitTestConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/PrivateUnitTestConfiguration.swift index 8068b5202b..f064b69c97 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/PrivateUnitTestConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/PrivateUnitTestConfiguration.swift @@ -1,6 +1,6 @@ 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." @@ -8,10 +8,6 @@ public struct PrivateUnitTestConfiguration: RuleConfiguration, Equatable, CacheD 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)" } diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/ProhibitedSuperConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/ProhibitedSuperConfiguration.swift index c36cf7cf6f..9e25e0c1a0 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/ProhibitedSuperConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/ProhibitedSuperConfiguration.swift @@ -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 = ["*"] @@ -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("*") { diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/RegexConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/RegexConfiguration.swift index c293f2aa88..5e12fbb20a 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/RegexConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/RegexConfiguration.swift @@ -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." @@ -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)" } diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/SeverityConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/SeverityConfiguration.swift index 8b80d7a570..ecba99b612 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/SeverityConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/SeverityConfiguration.swift @@ -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 } diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/TestCaseAccessibilityConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/TestCaseAccessibilityConfiguration.swift index 7035f34fbf..c90d003454 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/TestCaseAccessibilityConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/TestCaseAccessibilityConfiguration.swift @@ -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 = [] public private(set) var testParentClasses: Set = ["XCTestCase"] @@ -26,8 +26,4 @@ public struct TestCaseAccessibilityConfiguration: RuleConfiguration, Equatable { self.testParentClasses.formUnion(extraTestParentClasses) } } - - public var severity: ViolationSeverity { - return severityConfiguration.severity - } } diff --git a/Source/SwiftLintFramework/Rules/RuleConfigurations/UnusedDeclarationConfiguration.swift b/Source/SwiftLintFramework/Rules/RuleConfigurations/UnusedDeclarationConfiguration.swift index 9d5fe27b14..d4cb421837 100644 --- a/Source/SwiftLintFramework/Rules/RuleConfigurations/UnusedDeclarationConfiguration.swift +++ b/Source/SwiftLintFramework/Rules/RuleConfigurations/UnusedDeclarationConfiguration.swift @@ -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 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) { self.includePublicAndOpen = includePublicAndOpen - self.severity = severity + self.severityConfiguration = SeverityConfiguration(severity) self.relatedUSRsToSkip = relatedUSRsToSkip } @@ -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):