mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
ba5e6d2e1c
* Add support for allowed terms in inclusive language rule * Update changelog * Move changelog entry to Enhancements section Co-authored-by: JP Simard <jp@jpsim.com>
66 lines
2.3 KiB
Swift
66 lines
2.3 KiB
Swift
private enum ConfigurationKey: String {
|
|
case severity
|
|
case additionalTerms = "additional_terms"
|
|
case overrideTerms = "override_terms"
|
|
case overrideAllowedTerms = "override_allowed_terms"
|
|
}
|
|
|
|
public struct InclusiveLanguageConfiguration: RuleConfiguration, Equatable {
|
|
public var severityConfiguration = SeverityConfiguration(.warning)
|
|
public var additionalTerms: Set<String>?
|
|
public var overrideTerms: Set<String>?
|
|
public var overrideAllowedTerms: Set<String>?
|
|
public var allTerms: Set<String>
|
|
public var allAllowedTerms: Set<String>
|
|
|
|
public var consoleDescription: String {
|
|
severityConfiguration.consoleDescription
|
|
+ ", additional_terms: \(additionalTerms?.sorted() ?? [])"
|
|
+ ", override_terms: \(overrideTerms?.sorted() ?? [])"
|
|
+ ", override_allowed_terms: \(overrideAllowedTerms?.sorted() ?? [])"
|
|
}
|
|
|
|
public var severity: ViolationSeverity {
|
|
severityConfiguration.severity
|
|
}
|
|
|
|
private let defaultTerms: Set<String> = [
|
|
"whitelist",
|
|
"blacklist",
|
|
"master",
|
|
"slave"
|
|
]
|
|
|
|
private let defaultAllowedTerms: Set<String> = [
|
|
"mastercard"
|
|
]
|
|
|
|
public init() {
|
|
self.allTerms = defaultTerms
|
|
self.allAllowedTerms = defaultAllowedTerms
|
|
}
|
|
|
|
public mutating func apply(configuration: Any) throws {
|
|
guard let configuration = configuration as? [String: Any] else {
|
|
throw ConfigurationError.unknownConfiguration
|
|
}
|
|
|
|
if let severityString = configuration[ConfigurationKey.severity.rawValue] {
|
|
try severityConfiguration.apply(configuration: severityString)
|
|
}
|
|
|
|
additionalTerms = lowercasedSet(for: .additionalTerms, from: configuration)
|
|
overrideTerms = lowercasedSet(for: .overrideTerms, from: configuration)
|
|
overrideAllowedTerms = lowercasedSet(for: .overrideAllowedTerms, from: configuration)
|
|
|
|
allTerms = overrideTerms ?? defaultTerms
|
|
allTerms.formUnion(additionalTerms ?? [])
|
|
allAllowedTerms = overrideAllowedTerms ?? defaultAllowedTerms
|
|
}
|
|
|
|
private func lowercasedSet(for key: ConfigurationKey, from config: [String: Any]) -> Set<String>? {
|
|
guard let list = config[key.rawValue] as? [String] else { return nil }
|
|
return Set(list.map { $0.lowercased() })
|
|
}
|
|
}
|