Files
SwiftLint/Source/SwiftLintFramework/Rules/RuleConfigurations/RequiredEnumCaseRuleConfiguration.swift
T
JP Simard 0e01e07326 #2441 - Fix inaccessible custom rules in nested configurations (#2556)
* #2441 - Pass custom rules identifiers to the enableRules function to consider custom rules of a parent of a nested configuration

* #2441 - Add custom rules merge

* #2441 - Fix line length violation

* #2441 - Add nested configutaion mocks with custom rules

* #2441 - Add nested configurations tests for custom rules

* #2441 - Disable function body length check

* #2441 - Update changelog

* Move changelog to appropriate position

* Split up and refactor Configuration.init to avoid being too long

* Add tests to LinuxMain.swift

* Remove redundant protocol conformances

Hashable implies Equatable

* Fix typo in changelog entry and add another fixed issue URL
2019-01-13 15:07:27 -08:00

58 lines
2.0 KiB
Swift

public struct RequiredEnumCaseRuleConfiguration: RuleConfiguration, Equatable {
struct RequiredCase: Hashable {
var name: String
var severity: ViolationSeverity
init(name: String, severity: ViolationSeverity = .warning) {
self.name = name
self.severity = severity
}
}
var protocols: [String: Set<RequiredCase>] = [:]
public var consoleDescription: String {
let protocols = self.protocols.sorted(by: { $0.key < $1.key }) .compactMap { name, required in
let caseNames: [String] = required.sorted(by: { $0.name < $1.name }).map {
"[name: \"\($0.name)\", severity: \"\($0.severity.rawValue)\"]"
}
return "[protocol: \"\(name)\", cases: [\(caseNames.joined(separator: ", "))]]"
}.joined(separator: ", ")
let instructions = "No protocols configured. In config add 'required_enum_case' to 'opt_in_rules' and " +
"config using :\n\n" +
"'required_enum_case:\n" +
" {Protocol Name}:\n" +
" {Case Name}:{warning|error}\n" +
" {Case Name}:{warning|error}\n"
return protocols.isEmpty ? instructions : protocols
}
public mutating func apply(configuration: Any) throws {
guard let config = configuration as? [String: [String: String]] else {
throw ConfigurationError.unknownConfiguration
}
register(protocols: config)
}
mutating func register(protocols: [String: [String: String]]) {
for (name, cases) in protocols {
register(protocol: name, cases: cases)
}
}
mutating func register(protocol name: String, cases: [String: String]) {
var requiredCases: Set<RequiredCase> = []
for (caseName, severity) in cases {
let parsedSeverity: ViolationSeverity = (severity == "error") ? .error : .warning
requiredCases.insert(RequiredCase(name: caseName, severity: parsedSeverity))
}
protocols[name] = requiredCases
}
}