mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
small refactoring after #1264
This commit is contained in:
@@ -100,16 +100,14 @@ extension File {
|
||||
return nil
|
||||
}
|
||||
var results = [[SwiftDeclarationKind]](repeating: [], count: lines.count + 1)
|
||||
let structureKinds = structure.kinds()
|
||||
var lineIterator = lines.makeIterator()
|
||||
var structureIterator = structureKinds.makeIterator()
|
||||
var structureIterator = structure.kinds().makeIterator()
|
||||
var maybeLine = lineIterator.next()
|
||||
var maybeStructure = structureIterator.next()
|
||||
while let line = maybeLine, let structure = maybeStructure {
|
||||
if NSLocationInRange(structure.byteRange.location, line.byteRange) {
|
||||
if let swiftDeclarationKind = SwiftDeclarationKind(rawValue:structure.kind) {
|
||||
results[line.index].append(swiftDeclarationKind)
|
||||
}
|
||||
if NSLocationInRange(structure.byteRange.location, line.byteRange),
|
||||
let swiftDeclarationKind = SwiftDeclarationKind(rawValue: structure.kind) {
|
||||
results[line.index].append(swiftDeclarationKind)
|
||||
}
|
||||
let lineEnd = NSMaxRange(line.byteRange)
|
||||
if structure.byteRange.location > lineEnd {
|
||||
@@ -117,7 +115,6 @@ extension File {
|
||||
} else {
|
||||
maybeStructure = structureIterator.next()
|
||||
}
|
||||
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public struct LineLengthRule: ConfigurationProviderRule {
|
||||
)
|
||||
|
||||
public func validate(file: File) -> [StyleViolation] {
|
||||
let minValue = configuration.params.map({ $0.value }).min() ?? Int.max
|
||||
let minValue = configuration.params.map({ $0.value }).min() ?? .max
|
||||
let swiftDeclarationKindsByLine = file.swiftDeclarationKindsByLine() ?? []
|
||||
let syntaxKindsByLine = file.syntaxKindsByLine() ?? []
|
||||
|
||||
|
||||
@@ -15,11 +15,11 @@ public struct LineLengthRuleOptions: OptionSet {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let ignoreUrls = LineLengthRuleOptions(rawValue: 1 << 0)
|
||||
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]
|
||||
public static let all: LineLengthRuleOptions = [.ignoreURLs, .ignoreFunctionDeclarations, .ignoreComments]
|
||||
}
|
||||
|
||||
private enum ConfigurationKey: String {
|
||||
@@ -28,21 +28,6 @@ private enum ConfigurationKey: String {
|
||||
case ignoresURLs = "ignores_urls"
|
||||
case ignoresFunctionDeclarations = "ignores_function_declarations"
|
||||
case ignoresComments = "ignores_comments"
|
||||
|
||||
static func all() -> [ConfigurationKey] {
|
||||
return [
|
||||
.warning,
|
||||
.error,
|
||||
.ignoresURLs,
|
||||
.ignoresFunctionDeclarations,
|
||||
.ignoresComments
|
||||
]
|
||||
}
|
||||
|
||||
static func allValues() -> [String] {
|
||||
return all().map { $0.rawValue }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct LineLengthConfiguration: RuleConfiguration, Equatable {
|
||||
@@ -61,7 +46,7 @@ public struct LineLengthConfiguration: RuleConfiguration, Equatable {
|
||||
|
||||
public init(warning: Int, error: Int?, options: LineLengthRuleOptions = []) {
|
||||
self.length = SeverityLevelsConfiguration(warning: warning, error: error)
|
||||
self.ignoresURLs = options.contains(.ignoreUrls)
|
||||
self.ignoresURLs = options.contains(.ignoreURLs)
|
||||
self.ignoresFunctionDeclarations = options.contains(.ignoreFunctionDeclarations)
|
||||
self.ignoresComments = options.contains(.ignoreComments)
|
||||
}
|
||||
@@ -74,7 +59,7 @@ public struct LineLengthConfiguration: RuleConfiguration, Equatable {
|
||||
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 {
|
||||
guard let key = ConfigurationKey(rawValue: string) else {
|
||||
throw ConfigurationError.unknownConfiguration
|
||||
}
|
||||
switch (key, value) {
|
||||
|
||||
@@ -25,10 +25,10 @@ class LineLengthConfigurationTests: XCTestCase {
|
||||
XCTAssertEqual(configuration2.length, length2)
|
||||
}
|
||||
|
||||
func testLineLengthConfigurationInitialiserSetsIgnoresUrls() {
|
||||
func testLineLengthConfigurationInitialiserSetsIgnoresURLs() {
|
||||
let configuration1 = LineLengthConfiguration(warning: 100,
|
||||
error: 150,
|
||||
options: [.ignoreUrls])
|
||||
options: [.ignoreURLs])
|
||||
|
||||
XCTAssertTrue(configuration1.ignoresURLs)
|
||||
|
||||
@@ -198,8 +198,8 @@ extension LineLengthConfigurationTests {
|
||||
return [
|
||||
("testLineLengthConfigurationInitializerSetsLength",
|
||||
testLineLengthConfigurationInitializerSetsLength),
|
||||
("testLineLengthConfigurationInitialiserSetsIgnoresUrls",
|
||||
testLineLengthConfigurationInitialiserSetsIgnoresUrls),
|
||||
("testLineLengthConfigurationInitialiserSetsIgnoresURLs",
|
||||
testLineLengthConfigurationInitialiserSetsIgnoresURLs),
|
||||
("testLineLengthConfigurationPartialParams",
|
||||
testLineLengthConfigurationPartialParams),
|
||||
("testLineLengthConfigurationParams",
|
||||
|
||||
Reference in New Issue
Block a user