Skip to content

Commit

Permalink
Separate custom rules merging & filtering to avoid misleading warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
fredpi committed Jan 14, 2021
1 parent ba49f7d commit 256288a
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions Source/SwiftLintFramework/Extensions/Configuration+Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ internal extension Configuration {
if let cachedResultingRules = cachedResultingRules { return cachedResultingRules }

// Calculate value
let customRulesFilter: (RegexConfiguration) -> (Bool)
var resultingRules = [Rule]()
switch mode {
case .allEnabled:
customRulesFilter = { _ in true }
resultingRules = allRulesWrapped.map { $0.rule }

case var .only(onlyRulesRuleIdentifiers):
customRulesFilter = { onlyRulesRuleIdentifiers.contains($0.identifier) }
onlyRulesRuleIdentifiers = validate(ruleIds: onlyRulesRuleIdentifiers, valid: validRuleIdentifiers)
resultingRules = allRulesWrapped.filter { tuple in
onlyRulesRuleIdentifiers.contains(type(of: tuple.rule).description.identifier)
}.map { $0.rule }

case var .default(disabledRuleIdentifiers, optInRuleIdentifiers):
customRulesFilter = { !disabledRuleIdentifiers.contains($0.identifier) }
disabledRuleIdentifiers = validate(ruleIds: disabledRuleIdentifiers, valid: validRuleIdentifiers)
optInRuleIdentifiers = validate(ruleIds: optInRuleIdentifiers, valid: validRuleIdentifiers)
resultingRules = allRulesWrapped.filter { tuple in
Expand All @@ -53,6 +57,13 @@ internal extension Configuration {
}.map { $0.rule }
}

// Filter custom rules
if var customRulesRule = (resultingRules.first { $0 is CustomRules }) as? CustomRules {
customRulesRule.configuration.customRuleConfigurations =
customRulesRule.configuration.customRuleConfigurations.filter(customRulesFilter)
resultingRules = resultingRules.filter { !($0 is CustomRules) } + [customRulesRule]
}

// Sort by name
resultingRules = resultingRules.sorted {
type(of: $0).description.identifier < type(of: $1).description.identifier
Expand Down Expand Up @@ -145,21 +156,21 @@ internal extension Configuration {
newMode = .allEnabled
}

// Assemble & return merged Rules
// Assemble & return merged rules
return RulesWrapper(
mode: newMode,
allRulesWrapped: merged(
customRules: newAllRulesWrapped,
allRulesWrapped: mergedCustomRules(
newAllRulesWrapped: newAllRulesWrapped,
mode: newMode,
with: child
),
aliasResolver: { child.aliasResolver(self.aliasResolver($0)) }
)
}

private func mergedAllRulesWrapped(with sub: RulesWrapper) -> [ConfigurationRuleWrapper] {
private func mergedAllRulesWrapped(with child: RulesWrapper) -> [ConfigurationRuleWrapper] {
let mainConfigSet = Set(allRulesWrapped.map(HashableConfigurationRuleWrapperWrapper.init))
let childConfigSet = Set(sub.allRulesWrapped.map(HashableConfigurationRuleWrapperWrapper.init))
let childConfigSet = Set(child.allRulesWrapped.map(HashableConfigurationRuleWrapperWrapper.init))
let childConfigRulesWithConfig = childConfigSet.filter {
$0.configurationRuleWrapper.initializedWithNonEmptyConfiguration
}
Expand All @@ -171,42 +182,29 @@ internal extension Configuration {
.map { $0.configurationRuleWrapper }
}

private func merged(
customRules rules: [ConfigurationRuleWrapper], mode: RulesMode, with child: RulesWrapper
private func mergedCustomRules(
newAllRulesWrapped: [ConfigurationRuleWrapper], mode: RulesMode, with child: RulesWrapper
) -> [ConfigurationRuleWrapper] {
guard
let customRulesRule = (allRulesWrapped.first {
$0.rule is CustomRules
})?.rule as? CustomRules,
let childCustomRulesRule = (child.allRulesWrapped.first {
$0.rule is CustomRules
})?.rule as? CustomRules
let parentCustomRulesRule = (allRulesWrapped.first { $0.rule is CustomRules })?.rule
as? CustomRules,
let childCustomRulesRule = (child.allRulesWrapped.first { $0.rule is CustomRules })?.rule
as? CustomRules
else {
// Merging is only needed if both parent & child have a custom rules rule
return rules
}

let customRulesFilter: (RegexConfiguration) -> (Bool)
switch mode {
case .allEnabled:
customRulesFilter = { _ in true }

case let .only(onlyRules):
customRulesFilter = { onlyRules.contains($0.identifier) }

case let .default(disabledRules, _):
customRulesFilter = { !disabledRules.contains($0.identifier) }
return newAllRulesWrapped
}

// Create new custom rules rule, prioritizing child custom rules
var configuration = CustomRulesConfiguration()
configuration.customRuleConfigurations = Set(customRulesRule.configuration.customRuleConfigurations)
.union(Set(childCustomRulesRule.configuration.customRuleConfigurations))
.filter(customRulesFilter)

var customRules = CustomRules()
customRules.configuration = configuration
configuration.customRuleConfigurations = Array(
Set(childCustomRulesRule.configuration.customRuleConfigurations)
.union(Set(parentCustomRulesRule.configuration.customRuleConfigurations))
)
var newCustomRulesRule = CustomRules()
newCustomRulesRule.configuration = configuration

return rules.filter { !($0.rule is CustomRules) } + [(customRules, true)]
return newAllRulesWrapped.filter { !($0.rule is CustomRules) } + [(newCustomRulesRule, true)]
}

private func mergeDefaultMode(
Expand Down

0 comments on commit 256288a

Please sign in to comment.