Skip to content

Commit

Permalink
custom_rules needs special treatment to get the underlying rule ident…
Browse files Browse the repository at this point in the history
…ifiers. Fixes realm#4754
  • Loading branch information
mildm8nnered committed Feb 7, 2023
1 parent 843198d commit 5a9a3c6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
21 changes: 13 additions & 8 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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 {
return []
}

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)
Expand All @@ -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)
)
}
}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion Source/SwiftLintFramework/Models/Region.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions Source/SwiftLintFramework/Rules/Style/CustomRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5a9a3c6

Please sign in to comment.