Files
SwiftLint/Source/SwiftLintFramework/Rules/LineLengthRule.swift
T
Norio Nomura 5ebd64629a Improve performance of LineLengthRule
This depends on https://github.com/jpsim/SourceKitten/pull/152
The duration of linting Carthage 0.11 is reduced from 17sec to 16sec
SwiftLint(32b325b) with SourceKitten(0f54e19):
```
swiftlint lint --config ~/.swiftlint-test.yml  17.48s user 1.12s system 83% cpu 22.182 total
```
SwiftLint(this) with SourceKitten(d7f9266):
```
swiftlint lint --config ~/.swiftlint-test.yml  16.31s user 1.05s system 85% cpu 20.313 total
```
2016-01-31 13:57:37 -08:00

49 lines
1.7 KiB
Swift

//
// LineLengthRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct LineLengthRule: ConfigProviderRule {
public var config = SeverityLevelsConfig(warning: 100, error: 200)
public init() {}
public static let description = RuleDescription(
identifier: "line_length",
name: "Line Length",
description: "Lines should not span too many characters.",
nonTriggeringExamples: [
Repeat(count: 100, repeatedValue: "/").joinWithSeparator("") + "\n"
],
triggeringExamples: [
Repeat(count: 101, repeatedValue: "/").joinWithSeparator("") + "\n"
]
)
public func validateFile(file: File) -> [StyleViolation] {
let minValue = config.params.map({$0.value}).minElement(<)
return file.lines.flatMap { line in
// `line.content.characters.count` <= `line.range.length` is true.
// So, `check line.range.length` is larger than minimum parameter value.
// for avoiding using heavy `line.content.characters.count`.
if line.range.length < minValue {
return nil
}
let length = line.content.characters.count
for param in config.params where length > param.value {
return StyleViolation(ruleDescription: self.dynamicType.description,
severity: param.severity,
location: Location(file: file.path, line: line.index),
reason: "Line should be \(config.warning) characters or less: " +
"currently \(length) characters")
}
return nil
}
}
}