Files
SwiftLint/Source/SwiftLintFramework/Rules/Style/InclusiveLanguageRule.swift
T
Dalton Claybrook d305e03905 Add inclusive_language rule (#3243)
Current events have renewed the conversation in our community about the roles of terminology with racist connotations in our software. Many companies and developers are now taking appropriate steps to remove this terminology from their codebases and products. (e.g. [GitHub](https://twitter.com/natfriedman/status/1271253144442253312)) This small rule prevents the use of declarations that contain any of the terms: whitelist, blacklist, master, and slave. It may be appropriate to add more terms to this list now or in the future.
2020-11-07 22:03:08 -05:00

42 lines
1.6 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()
guard let term = configuration.allTerms.first(where: lowercased.contains) 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."
)
]
}
}