forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrailingWhitespaceRule.swift
42 lines (37 loc) · 1.33 KB
/
TrailingWhitespaceRule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// TrailingWhitespaceRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct TrailingWhitespaceRule: Rule {
public init() {}
public let identifier = "trailing_whitespace"
public func validateFile(file: File) -> [StyleViolation] {
return file.contents.lines().map { line in
(
index: line.index,
trailingWhitespaceCount: line.content.countOfTailingCharactersInSet(
NSCharacterSet.whitespaceCharacterSet()
)
)
}.filter {
$0.trailingWhitespaceCount > 0
}.map {
StyleViolation(type: .TrailingWhitespace,
location: Location(file: file.path, line: $0.index),
severity: .Medium,
reason: "Line #\($0.index) should have no trailing whitespace: " +
"current has \($0.trailingWhitespaceCount) trailing whitespace characters")
}
}
public let example = RuleExample(
ruleName: "Trailing Whitespace Rule",
ruleDescription: "This rule checks whether you don't have any trailing whitespace.",
nonTriggeringExamples: [ "//\n" ],
triggeringExamples: [ "// \n" ],
showExamples: false
)
}