Files
SwiftLint/Source/SwiftLintCore/Extensions/Configuration+RulesMode.swift
T
JP Simard 86d60400c1 Move core SwiftLint functionality to new SwiftLintCore module
Over the years, SwiftLintFramework had become a fairly massive monolith,
containing over 400 source files with both core infrastructure and
rules.

Architecturally, the rules should rely on the core infrastructure but
not the other way around. There are two exceptions to this:
`custom_rules` and `superfluous_disable_command` which need special
integration with the linter infrastructure.

Now the time has come to formalize this architecture and one way to do
that is to move the core SwiftLint functionality out of
SwiftLintFramework and into a new SwiftLintCore module that the rules
can depend on.

Beyond enforcing architectural patterns, this also has the advantage of
speeding up incremental compilation by skipping rebuilding the core
functionality when iterating on rules.

Because the core functionality is always useful when building rules, I'm
opting to import SwiftLintCore in SwiftLintFramework as `@_exported` so
that it's implicitly available to all files in SwiftLintFramework
without needing to import it directly.

In a follow-up I'll also split the built-in rules and the extra rules
into their own modules. More modularization is possible from there, but
not planned.

The bulk of this PR just moves files from `Source/SwiftLintFramework/*`
to `Source/SwiftLintCore/*`. There are some other changes that can't be
split up into their own PRs:

* Change jazzy to document the SwiftLintCore module instead of
  SwiftLintFramework.
* Change imports in unit tests to reflect where code was moved to.
* Update `sourcery` make rule to reflect where code was moved to.
* Create a new `coreRules` array and register those rules with the
  registry. This allows the `custom_rules` and
  `superfluous_disable_command` rule implementations to remain internal
  to the SwiftLintCore module, preventing more implementation details
  from leaking across architectural layers.
* Move `RuleRegistry.registerAllRulesOnce()` out of the type declaration
  and up one level so it can access rules defined downstream from
  SwiftLintCore.
2023-04-26 21:10:19 -04:00

111 lines
4.7 KiB
Swift

public extension Configuration {
/// Returns the rule for the specified ID, if configured in this configuration.
///
/// - parameter ruleID: The identifier for the rule to look up.
///
/// - returns: The rule for the specified ID, if configured in this configuration.
func configuredRule(forID ruleID: String) -> Rule? {
rules.first { rule in
guard type(of: rule).description.identifier == ruleID else {
return false
}
guard let customRules = rule as? CustomRules else {
return true
}
return !customRules.configuration.customRuleConfigurations.isEmpty
}
}
/// Represents how a Configuration object can be configured with regards to rules.
enum RulesMode {
/// The default rules mode, which will enable all rules that aren't defined as being opt-in
/// (conforming to the `OptInRule` protocol), minus the rules listed in `disabled`, plus the rules listed in
/// `optIn`.
case `default`(disabled: Set<String>, optIn: Set<String>)
/// Only enable the rules explicitly listed.
case only(Set<String>)
/// Enable all available rules.
case allEnabled
internal init(
enableAllRules: Bool,
onlyRules: [String],
optInRules: [String],
disabledRules: [String],
analyzerRules: [String]
) throws {
func warnAboutDuplicates(in identifiers: [String]) {
if Set(identifiers).count != identifiers.count {
let duplicateRules = identifiers.reduce(into: [String: Int]()) { $0[$1, default: 0] += 1 }
.filter { $0.1 > 1 }
for duplicateRule in duplicateRules {
queuedPrintError("warning: '\(duplicateRule.0)' is listed \(duplicateRule.1) times")
}
}
}
if enableAllRules {
self = .allEnabled
} else if onlyRules.isNotEmpty {
if disabledRules.isNotEmpty || optInRules.isNotEmpty {
throw ConfigurationError.generic(
"'\(Configuration.Key.disabledRules.rawValue)' or " +
"'\(Configuration.Key.optInRules.rawValue)' cannot be used in combination " +
"with '\(Configuration.Key.onlyRules.rawValue)'"
)
}
warnAboutDuplicates(in: onlyRules + analyzerRules)
self = .only(Set(onlyRules + analyzerRules))
} else {
warnAboutDuplicates(in: disabledRules)
let effectiveOptInRules: [String]
if optInRules.contains(RuleIdentifier.all.stringRepresentation) {
let allOptInRules = RuleRegistry.shared.list.list.compactMap { ruleID, ruleType in
ruleType is OptInRule.Type && !(ruleType is AnalyzerRule.Type) ? ruleID : nil
}
effectiveOptInRules = Array(Set(allOptInRules + optInRules))
} else {
effectiveOptInRules = optInRules
}
warnAboutDuplicates(in: effectiveOptInRules + analyzerRules)
self = .default(disabled: Set(disabledRules), optIn: Set(effectiveOptInRules + analyzerRules))
}
}
internal func applied(aliasResolver: (String) -> String) -> RulesMode {
switch self {
case let .default(disabled, optIn):
return .default(
disabled: Set(disabled.map(aliasResolver)),
optIn: Set(optIn.map(aliasResolver))
)
case let .only(onlyRules):
return .only(Set(onlyRules.map(aliasResolver)))
case .allEnabled:
return .allEnabled
}
}
internal func activateCustomRuleIdentifiers(allRulesWrapped: [ConfigurationRuleWrapper]) -> RulesMode {
// In the only mode, if the custom rules rule is enabled, all custom rules are also enabled implicitly
// This method makes the implicitly explicit
switch self {
case let .only(onlyRules) where onlyRules.contains { $0 == CustomRules.description.identifier }:
let customRulesRule = (allRulesWrapped.first { $0.rule is CustomRules })?.rule as? CustomRules
let customRuleIdentifiers = customRulesRule?.configuration.customRuleConfigurations.map(\.identifier)
return .only(onlyRules.union(Set(customRuleIdentifiers ?? [])))
default:
return self
}
}
}
}