Files
SwiftLint/Source/SwiftLintFramework/Rules/Style/InclusiveLanguageRule.swift
T
Dalton Claybrook ba5e6d2e1c Fix false positives in inclusive_language rule (#3439)
* 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>
2020-11-20 14:12:24 -05:00

51 lines
2.0 KiB
Swift

import SourceKittenFramework
public struct InclusiveLanguageRule: ASTRule, ConfigurationProviderRule {
public var configuration = InclusiveLanguageConfiguration()
public init() {}
public static let description = RuleDescription(
identifier: "inclusive_language",
name: "Inclusive Language",
description: """
Identifiers should use inclusive language that avoids discrimination against groups of people based on \
race, gender, or socioeconomic status
""",
kind: .style,
nonTriggeringExamples: InclusiveLanguageRuleExamples.nonTriggeringExamples,
triggeringExamples: InclusiveLanguageRuleExamples.triggeringExamples
)
public func validate(file: SwiftLintFile, kind: SwiftDeclarationKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
guard kind != .varParameter, // Will be caught by function declaration
let name = dictionary.name,
let nameByteRange = dictionary.nameByteRange
else { return [] }
let lowercased = name.lowercased()
let violationTerm = configuration.allTerms.first { term in
guard let range = lowercased.range(of: term) else { return false }
let overlapsAllowedTerm = configuration.allAllowedTerms.contains { allowedTerm in
guard let allowedRange = lowercased.range(of: allowedTerm) else { return false }
return range.overlaps(allowedRange)
}
return !overlapsAllowedTerm
}
guard let term = violationTerm else {
return []
}
return [
StyleViolation(
ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, byteOffset: nameByteRange.location),
reason: "Declaration \(name) contains the term \"\(term)\" which is not considered inclusive."
)
]
}
}