@@ -15,8 +15,8 @@ extension Configuration {
15
15
case analyzerRules = " analyzer_rules "
16
16
}
17
17
18
- private static func validKeys ( ruleList : RuleList ) -> [ String ] {
19
- return [
18
+ private static let validGlobalKeys : Set < String > = {
19
+ return Set ( [
20
20
Key . cachePath,
21
21
. disabledRules,
22
22
. enabledRules,
@@ -30,7 +30,13 @@ extension Configuration {
30
30
. whitelistRules,
31
31
. indentation,
32
32
. analyzerRules
33
- ] . map ( { $0. rawValue } ) + ruleList. allValidIdentifiers ( )
33
+ ] . map ( { $0. rawValue } ) )
34
+ } ( )
35
+
36
+ private static func validKeys( ruleList: RuleList ) -> Set < String > {
37
+ var keys = validGlobalKeys
38
+ keys. formUnion ( ruleList. allValidIdentifiers ( ) )
39
+ return keys
34
40
}
35
41
36
42
private static func getIndentationLogIfInvalid( from dict: [ String : Any ] ) -> IndentationStyle {
@@ -90,7 +96,8 @@ extension Configuration {
90
96
swiftlintVersion: swiftlintVersion,
91
97
cachePath: cachePath ?? dict [ Key . cachePath. rawValue] as? String ,
92
98
indentation: indentation,
93
- customRulesIdentifiers: customRulesIdentifiers)
99
+ customRulesIdentifiers: customRulesIdentifiers,
100
+ dict: dict)
94
101
}
95
102
96
103
private init ? ( disabledRules: [ String ] ,
@@ -107,7 +114,8 @@ extension Configuration {
107
114
swiftlintVersion: String ? ,
108
115
cachePath: String ? ,
109
116
indentation: IndentationStyle ,
110
- customRulesIdentifiers: [ String ] ) {
117
+ customRulesIdentifiers: [ String ] ,
118
+ dict: [ String : Any ] ) {
111
119
let rulesMode : RulesMode
112
120
if enableAllRules {
113
121
rulesMode = . allEnabled
@@ -123,6 +131,9 @@ extension Configuration {
123
131
rulesMode = . default( disabled: disabledRules, optIn: optInRules + analyzerRules)
124
132
}
125
133
134
+ Configuration . validateConfiguredRulesAreEnabled ( configurationDictionary: dict, ruleList: ruleList,
135
+ rulesMode: rulesMode)
136
+
126
137
self . init ( rulesMode: rulesMode,
127
138
included: included,
128
139
excluded: excluded,
@@ -178,6 +189,37 @@ extension Configuration {
178
189
queuedPrintError ( " Configuration contains invalid keys: \n \( invalidKeys) " )
179
190
}
180
191
}
192
+
193
+ private static func validateConfiguredRulesAreEnabled( configurationDictionary dict: [ String : Any ] ,
194
+ ruleList: RuleList ,
195
+ rulesMode: RulesMode ) {
196
+ for key in dict. keys where !validGlobalKeys. contains ( key) {
197
+ guard let identifier = ruleList. identifier ( for: key) ,
198
+ let rule = ruleList. list [ identifier] else {
199
+ continue
200
+ }
201
+
202
+ let message = " Found a configuration for ' \( identifier) ' rule "
203
+
204
+ switch rulesMode {
205
+ case . allEnabled:
206
+ return
207
+ case . whitelisted( let whitelist) :
208
+ if Set ( whitelist) . isDisjoint ( with: rule. description. allIdentifiers) {
209
+ queuedPrintError ( " \( message) , but it is not present on " +
210
+ " ' \( Key . whitelistRules. rawValue) '. " )
211
+ }
212
+ case let . default( disabled: disabledRules, optIn: optInRules) :
213
+ if rule is OptInRule . Type , Set ( optInRules) . isDisjoint ( with: rule. description. allIdentifiers) {
214
+ queuedPrintError ( " \( message) , but it is not enabled on " +
215
+ " ' \( Key . optInRules. rawValue) '. " )
216
+ } else if Set ( disabledRules) . isSuperset ( of: rule. description. allIdentifiers) {
217
+ queuedPrintError ( " \( message) , but it is disabled on " +
218
+ " ' \( Key . disabledRules. rawValue) '. " )
219
+ }
220
+ }
221
+ }
222
+ }
181
223
}
182
224
183
225
private func defaultStringArray( _ object: Any ? ) -> [ String ] {
0 commit comments