Files
SwiftLint/Source/SwiftLintFramework/Rules/RuleConfigurations/LineLengthConfiguration.swift
T
JP Simard b83e0991b9 Remove all file headers
The MIT license doesn't require that all files be prepended with this
licensing or copyright information. Realm confirmed that they're ok with this
change. This will enable some companies to contribute to SwiftLint and the
date & authorship information will remain accessible via git source control.
2018-05-04 13:42:02 -07:00

88 lines
3.4 KiB
Swift

import Foundation
public struct LineLengthRuleOptions: OptionSet {
public let rawValue: Int
public init(rawValue: Int = 0) {
self.rawValue = rawValue
}
public static let ignoreURLs = LineLengthRuleOptions(rawValue: 1 << 0)
public static let ignoreFunctionDeclarations = LineLengthRuleOptions(rawValue: 1 << 1)
public static let ignoreComments = LineLengthRuleOptions(rawValue: 1 << 2)
public static let all: LineLengthRuleOptions = [.ignoreURLs, .ignoreFunctionDeclarations, .ignoreComments]
}
private enum ConfigurationKey: String {
case warning = "warning"
case error = "error"
case ignoresURLs = "ignores_urls"
case ignoresFunctionDeclarations = "ignores_function_declarations"
case ignoresComments = "ignores_comments"
}
public struct LineLengthConfiguration: RuleConfiguration, Equatable {
public var consoleDescription: String {
return length.consoleDescription +
", ignores urls: \(ignoresURLs)" +
", ignores function declarations: \(ignoresFunctionDeclarations)" +
", ignores comments: \(ignoresComments)"
}
var length: SeverityLevelsConfiguration
var ignoresURLs: Bool
var ignoresFunctionDeclarations: Bool
var ignoresComments: Bool
var params: [RuleParameter<Int>] {
return length.params
}
public init(warning: Int, error: Int?, options: LineLengthRuleOptions = []) {
self.length = SeverityLevelsConfiguration(warning: warning, error: error)
self.ignoresURLs = options.contains(.ignoreURLs)
self.ignoresFunctionDeclarations = options.contains(.ignoreFunctionDeclarations)
self.ignoresComments = options.contains(.ignoreComments)
}
public mutating func apply(configuration: Any) throws {
if let configurationArray = [Int].array(of: configuration),
!configurationArray.isEmpty {
let warning = configurationArray[0]
let error = (configurationArray.count > 1) ? configurationArray[1] : nil
length = SeverityLevelsConfiguration(warning: warning, error: error)
} else if let configDict = configuration as? [String: Any], !configDict.isEmpty {
for (string, value) in configDict {
guard let key = ConfigurationKey(rawValue: string) else {
throw ConfigurationError.unknownConfiguration
}
switch (key, value) {
case (.error, let intValue as Int):
length.error = intValue
case (.warning, let intValue as Int):
length.warning = intValue
case (.ignoresFunctionDeclarations, let boolValue as Bool):
ignoresFunctionDeclarations = boolValue
case (.ignoresComments, let boolValue as Bool):
ignoresComments = boolValue
case (.ignoresURLs, let boolValue as Bool):
ignoresURLs = boolValue
default:
throw ConfigurationError.unknownConfiguration
}
}
} else {
throw ConfigurationError.unknownConfiguration
}
}
}
public func == (lhs: LineLengthConfiguration, rhs: LineLengthConfiguration) -> Bool {
return lhs.length == rhs.length &&
lhs.ignoresURLs == rhs.ignoresURLs &&
lhs.ignoresComments == rhs.ignoresComments &&
lhs.ignoresFunctionDeclarations == rhs.ignoresFunctionDeclarations
}