forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeadingWhitespaceRule.swift
34 lines (30 loc) · 1.12 KB
/
LeadingWhitespaceRule.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
//
// LeadingWhitespaceRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct LeadingWhitespaceRule: Rule {
public init() {}
public static let description = RuleDescription(
identifier: "leading_whitespace",
name: "Leading Whitespace",
description: "This rule checks that there's no leading whitespace in your file.",
nonTriggeringExamples: [ "//\n" ],
triggeringExamples: [ "\n", " //\n" ]
)
public func validateFile(file: File) -> [StyleViolation] {
let countOfLeadingWhitespace = file.contents.countOfLeadingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet()
)
if countOfLeadingWhitespace == 0 {
return []
}
return [StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file.path, line: 1),
reason: "File shouldn't start with whitespace: " +
"currently starts with \(countOfLeadingWhitespace) whitespace characters")]
}
}