Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Warn if a configured rule is not enabled #1682

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
[Cody Winton](https://github.com/codytwinton)
[#1327](https://github.com/realm/SwiftLint/issues/1327)

* Warn if a configured rule is not enabled.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1350](https://github.com/realm/SwiftLint/issues/1350)

##### Bug Fixes

* Fix false positive on `redundant_discardable_let` rule when using
Expand Down
32 changes: 32 additions & 0 deletions Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public struct Configuration: Equatable {
return optInRules.contains(id) || !(rule is OptInRule)
}
}

if !enableAllRules {
validateConfiguredRulesAreEnabled(configuredRules: configuredRules, rules: rules,
whitelistRules: whitelistRules, optInRules: optInRules,
disabledRules: disabledRules)
}
}

public init?(dict: [String: Any], ruleList: RuleList = masterRuleList, enableAllRules: Bool = false,
Expand Down Expand Up @@ -217,6 +223,32 @@ public struct Configuration: Equatable {
}
return self
}

private func validateConfiguredRulesAreEnabled(configuredRules: [Rule], rules: [Rule],
whitelistRules: [String], optInRules: [String],
disabledRules: [String]) {
for rule in configuredRules {
guard !rules.contains(where: { type(of: $0) == type(of: rule) }) else {
continue
}

let identifier = type(of: rule).description.identifier
let message = "Found a configuration for \(identifier)"
guard whitelistRules.isEmpty else {
queuedPrintError("\(message), but it is not present on " +
"\(ConfigurationKey.whitelistRules.rawValue)")
continue
}

if rule is OptInRule, !optInRules.contains(identifier) {
queuedPrintError("\(message), but it is not enabled on " +
"\(ConfigurationKey.optInRules.rawValue)")
} else {
queuedPrintError("\(message), but it is disabled on " +
"\(ConfigurationKey.disabledRules.rawValue)")
}
}
}
}

private func validateRuleIdentifiers(configuredRules: [Rule], disabledRules: [String]) -> [String] {
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Models/RuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public struct RuleList {

for (key, configuration) in dictionary {
guard let identifier = identifier(for: key), let ruleType = list[identifier] else {
queuedPrintError("Unknown rule for identifier: '\(key)'. Maybe there's a typo?")
continue
}
guard rules[identifier] == nil else {
Expand Down