Skip to content

Commit

Permalink
Warn if a configured rule is not enabled.
Browse files Browse the repository at this point in the history
Fixes #1350
  • Loading branch information
marcelofabri committed Feb 3, 2019
1 parent cc14185 commit 7ecfce3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

* Make `redundant_objc_attribute` rule autocorrectable.
[Daniel Metzing](https://github.com/dirtydanee)

* Add `required_deinit` opt-in rule to ensure that all classes have a deinit
method. The purpose of this is to make memory leak debugging easier so all
classes have a place to set a breakpoint to track deallocation.
Expand All @@ -41,7 +41,11 @@
* `nimble_operator` now warns about `beTrue()` and `beFalse()`.
[Igor-Palaguta](https://github.com/Igor-Palaguta)
[#2613](https://github.com/realm/SwiftLint/issues/2613)


* 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 `explicit_type_interface` when used in statements.
Expand Down
52 changes: 47 additions & 5 deletions Source/SwiftLintFramework/Extensions/Configuration+Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ extension Configuration {
case analyzerRules = "analyzer_rules"
}

private static func validKeys(ruleList: RuleList) -> [String] {
return [
private static let validGlobalKeys: Set<String> = {
return Set([
Key.cachePath,
.disabledRules,
.enabledRules,
Expand All @@ -30,7 +30,13 @@ extension Configuration {
.whitelistRules,
.indentation,
.analyzerRules
].map({ $0.rawValue }) + ruleList.allValidIdentifiers()
].map({ $0.rawValue }))
}()

private static func validKeys(ruleList: RuleList) -> Set<String> {
var keys = validGlobalKeys
keys.formUnion(ruleList.allValidIdentifiers())
return keys
}

private static func getIndentationLogIfInvalid(from dict: [String: Any]) -> IndentationStyle {
Expand Down Expand Up @@ -90,7 +96,8 @@ extension Configuration {
swiftlintVersion: swiftlintVersion,
cachePath: cachePath ?? dict[Key.cachePath.rawValue] as? String,
indentation: indentation,
customRulesIdentifiers: customRulesIdentifiers)
customRulesIdentifiers: customRulesIdentifiers,
dict: dict)
}

private init?(disabledRules: [String],
Expand All @@ -107,7 +114,8 @@ extension Configuration {
swiftlintVersion: String?,
cachePath: String?,
indentation: IndentationStyle,
customRulesIdentifiers: [String]) {
customRulesIdentifiers: [String],
dict: [String: Any]) {
let rulesMode: RulesMode
if enableAllRules {
rulesMode = .allEnabled
Expand All @@ -123,6 +131,9 @@ extension Configuration {
rulesMode = .default(disabled: disabledRules, optIn: optInRules + analyzerRules)
}

Configuration.validateConfiguredRulesAreEnabled(configurationDictionary: dict, ruleList: ruleList,
rulesMode: rulesMode)

self.init(rulesMode: rulesMode,
included: included,
excluded: excluded,
Expand Down Expand Up @@ -178,6 +189,37 @@ extension Configuration {
queuedPrintError("Configuration contains invalid keys:\n\(invalidKeys)")
}
}

private static func validateConfiguredRulesAreEnabled(configurationDictionary dict: [String: Any],
ruleList: RuleList,
rulesMode: RulesMode) {
for key in dict.keys where !validGlobalKeys.contains(key) {
guard let identifier = ruleList.identifier(for: key),
let rule = ruleList.list[identifier] else {
continue
}

let message = "Found a configuration for '\(identifier)' rule"

switch rulesMode {
case .allEnabled:
return
case .whitelisted(let whitelist):
if Set(whitelist).isDisjoint(with: rule.description.allIdentifiers) {
queuedPrintError("\(message), but it is not present on " +
"'\(Key.whitelistRules.rawValue)'.")
}
case let .default(disabled: disabledRules, optIn: optInRules):
if rule is OptInRule.Type, Set(optInRules).isDisjoint(with: rule.description.allIdentifiers) {
queuedPrintError("\(message), but it is not enabled on " +
"'\(Key.optInRules.rawValue)'.")
} else if Set(disabledRules).isSuperset(of: rule.description.allIdentifiers) {
queuedPrintError("\(message), but it is disabled on " +
"'\(Key.disabledRules.rawValue)'.")
}
}
}
}
}

private func defaultStringArray(_ object: Any?) -> [String] {
Expand Down

0 comments on commit 7ecfce3

Please sign in to comment.