forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReturnArrowWhitespaceRule.swift
59 lines (51 loc) · 1.97 KB
/
ReturnArrowWhitespaceRule.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// ReturningWhitespaceRule.swift
// SwiftLint
//
// Created by Akira Hirakawa on 2/6/15.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ReturnArrowWhitespaceRule: Rule {
public init() {}
public let identifier = "return_arrow_whitespace"
public func validateFile(file: File) -> [StyleViolation] {
// space doesn't include \n so that "func abc()->\n" can pass validation
let space = "[ \\f\\r\\t\\v]"
let spaceRegex = "(\(space){0}|\(space){2,})"
// ex: func abc()-> Int {
let pattern1 = file.matchPattern("\\)\(spaceRegex)\\->\\s*\\S+",
withSyntaxKinds: [.Typeidentifier])
// ex: func abc() ->Int {
let pattern2 = file.matchPattern("\\)\\s\\->\(spaceRegex)\\S+",
withSyntaxKinds: [.Typeidentifier])
return (pattern1 + pattern2).map { match in
return StyleViolation(type: .ReturnArrowWhitespace,
location: Location(file: file, offset: match.location),
severity: .Low,
reason: "File should have 1 space before return arrow and return type")
}
}
public let example = RuleExample(
ruleName: "Returning Whitespace Rule",
ruleDescription: "This rule checks whether you have 1 space before " +
"return arrow and return type",
nonTriggeringExamples: [
"func abc() -> Int {}\n",
"func abc() -> [Int] {}\n",
"func abc() -> (Int, Int) {}\n",
"var abc = {(param: Int) -> Void in }\n",
"func abc() ->\n"
],
triggeringExamples: [
"func abc()->Int {}\n",
"func abc()->[Int] {}\n",
"func abc()->(Int, Int) {}\n",
"func abc()-> Int {}\n",
"func abc() ->Int {}\n",
"func abc() -> Int {}\n",
"var abc = {(param: Int) ->Bool in }\n",
"var abc = {(param: Int)->Bool in }\n"
]
)
}