forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrailingNewlineRule.swift
34 lines (28 loc) · 1.17 KB
/
TrailingNewlineRule.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
//
// TrailingNewlineRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct TrailingNewlineRule: Rule {
public init() {}
public static let description = RuleDescription(
identifier: "trailing_newline",
name: "Trailing Newline",
description: "Files should have a single trailing newline."
)
public func validateFile(file: File) -> [StyleViolation] {
let string = file.contents
let start = string.endIndex.advancedBy(-2, limit: string.startIndex)
let range = Range(start: start, end: string.endIndex)
let substring = string[range].utf16
let newLineSet = NSCharacterSet.newlineCharacterSet()
let slices = substring.split(allowEmptySlices: true) { !newLineSet.characterIsMember($0) }
guard let slice = slices.last where slice.count != 1 else { return [] }
return [StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file.path, line: max(file.lines.count, 1)),
reason: "File should have a single trailing newline")]
}
}