From 5a9a3c68c108fcfe171d77ee7be7ea2550dcbbad Mon Sep 17 00:00:00 2001 From: Martin Redington Date: Tue, 7 Feb 2023 23:41:07 +0000 Subject: [PATCH] custom_rules needs special treatment to get the underlying rule identifiers. Fixes #4754 --- Source/SwiftLintFramework/Models/Linter.swift | 21 ++++++++++++------- Source/SwiftLintFramework/Models/Region.swift | 7 ++++++- .../Rules/Style/CustomRules.swift | 2 ++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Source/SwiftLintFramework/Models/Linter.swift b/Source/SwiftLintFramework/Models/Linter.swift index 687aa0f7e9..17d5623135 100644 --- a/Source/SwiftLintFramework/Models/Linter.swift +++ b/Source/SwiftLintFramework/Models/Linter.swift @@ -16,7 +16,7 @@ private struct LintResult { } private extension Rule { - static func superfluousDisableCommandViolations(regions: [Region], + func superfluousDisableCommandViolations(regions: [Region], superfluousDisableCommandRule: SuperfluousDisableCommandRule?, allViolations: [StyleViolation]) -> [StyleViolation] { guard regions.isNotEmpty, let superfluousDisableCommandRule else { @@ -24,7 +24,7 @@ private extension Rule { } let regionsDisablingCurrentRule = regions.filter { region in - return region.isRuleDisabled(self.init()) + return region.isRuleDisabled(self) } let regionsDisablingSuperfluousDisableRule = regions.filter { region in return region.isRuleDisabled(superfluousDisableCommandRule) @@ -50,7 +50,7 @@ private extension Rule { ruleDescription: type(of: superfluousDisableCommandRule).description, severity: superfluousDisableCommandRule.configuration.severity, location: region.start, - reason: superfluousDisableCommandRule.reason(for: self) + reason: superfluousDisableCommandRule.reason(for: Self.self) ) } } @@ -91,12 +91,17 @@ private extension Rule { return region?.isRuleEnabled(self) ?? true } - let ruleIDs = Self.description.allIdentifiers + - (superfluousDisableCommandRule.map({ type(of: $0) })?.description.allIdentifiers ?? []) + + let ruleIDs: [String] + if let customRules = self as? CustomRules { + ruleIDs = customRules.customRuleIdentifiers + } else { + ruleIDs = Self.description.allIdentifiers + } + let allRuleIDs = ruleIDs + (superfluousDisableCommandRule.map({ type(of: $0) })?.description.allIdentifiers ?? []) + [RuleIdentifier.all.stringRepresentation] - let ruleIdentifiers = Set(ruleIDs.map { RuleIdentifier($0) }) - - let superfluousDisableCommandViolations = Self.superfluousDisableCommandViolations( + let ruleIdentifiers = Set(allRuleIDs.map { RuleIdentifier($0) }) + + let superfluousDisableCommandViolations = superfluousDisableCommandViolations( regions: regions.count > 1 ? file.regions(restrictingRuleIdentifiers: ruleIdentifiers) : regions, superfluousDisableCommandRule: superfluousDisableCommandRule, allViolations: violations diff --git a/Source/SwiftLintFramework/Models/Region.swift b/Source/SwiftLintFramework/Models/Region.swift index efc9f48438..b1c7fd2e54 100644 --- a/Source/SwiftLintFramework/Models/Region.swift +++ b/Source/SwiftLintFramework/Models/Region.swift @@ -50,7 +50,12 @@ public struct Region: Equatable { return true } - let identifiersToCheck = type(of: rule).description.allIdentifiers + let identifiersToCheck: [String] + if let customRules = rule as? CustomRules { + identifiersToCheck = customRules.customRuleIdentifiers + } else { + identifiersToCheck = type(of: rule).description.allIdentifiers + } let regionIdentifiers = Set(disabledRuleIdentifiers.map { $0.stringRepresentation }) return !regionIdentifiers.isDisjoint(with: identifiersToCheck) } diff --git a/Source/SwiftLintFramework/Rules/Style/CustomRules.swift b/Source/SwiftLintFramework/Rules/Style/CustomRules.swift index ec3d1441e8..bab2979dee 100644 --- a/Source/SwiftLintFramework/Rules/Style/CustomRules.swift +++ b/Source/SwiftLintFramework/Rules/Style/CustomRules.swift @@ -79,6 +79,8 @@ struct CustomRules: Rule, ConfigurationProviderRule, CacheDescriptionProvider { kind: .style) var configuration = CustomRulesConfiguration() + + var customRuleIdentifiers: [String] { configuration.customRuleConfigurations.map { $0.identifier } } func validate(file: SwiftLintFile) -> [StyleViolation] { var configurations = configuration.customRuleConfigurations