forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReturnArrowWhitespaceRule.swift
127 lines (107 loc) · 4.66 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// ReturningWhitespaceRule.swift
// SwiftLint
//
// Created by Akira Hirakawa on 2/6/15.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct ReturnArrowWhitespaceRule: CorrectableRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "return_arrow_whitespace",
name: "Returning Whitespace",
description: "Return arrow and return type should be separated by a single space or on a " +
"separate line.",
nonTriggeringExamples: [
"func abc() -> Int {}\n",
"func abc() -> [Int] {}\n",
"func abc() -> (Int, Int) {}\n",
"var abc = {(param: Int) -> Void in }\n",
"func abc() ->\n Int {}\n",
"func abc()\n -> Int {}\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"
],
corrections: [
"func abc()↓->Int {}\n": "func abc() -> Int {}\n",
"func abc()↓-> Int {}\n": "func abc() -> Int {}\n",
"func abc()↓ ->Int {}\n": "func abc() -> Int {}\n",
"func abc()↓ -> Int {}\n": "func abc() -> Int {}\n",
"func abc()↓\n -> Int {}\n": "func abc()\n -> Int {}\n",
"func abc()↓\n-> Int {}\n": "func abc()\n-> Int {}\n",
"func abc()↓ ->\n Int {}\n": "func abc() ->\n Int {}\n",
"func abc()↓ ->\nInt {}\n": "func abc() ->\nInt {}\n"
]
)
public func validate(file: File) -> [StyleViolation] {
return violationRanges(in: file, skipParentheses: true).map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
public func correct(file: File) -> [Correction] {
let violationsRanges = violationRanges(in: file, skipParentheses: false)
let matches = file.ruleEnabled(violatingRanges: violationsRanges, for: self)
if matches.isEmpty { return [] }
let regularExpression = regex(pattern)
let description = type(of: self).description
var corrections = [Correction]()
var contents = file.contents
let results = matches.reversed().flatMap { range in
return regularExpression.firstMatch(in: contents, options: [], range: range)
}
let replacementsByIndex = [2: " -> ", 4: " -> ", 6: " ", 7: " "]
for result in results {
guard result.numberOfRanges > (replacementsByIndex.keys.max() ?? 0) else { break }
for (index, string) in replacementsByIndex {
if let range = contents.nsrangeToIndexRange(result.rangeAt(index)) {
contents.replaceSubrange(range, with: string)
break
}
}
// skip the parentheses when reporting correction
let location = Location(file: file, characterOffset: result.range.location + 1)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents)
return corrections
}
// MARK: - Private
private let pattern: String = {
//just horizontal spacing so that "func abc()->\n" can pass validation
let space = "[ \\f\\r\\t]"
// Either 0 space characters or 2+
let incorrectSpace = "(\(space){0}|\(space){2,})"
// The possible combinations of whitespace around the arrow
let patterns = [
"(\(incorrectSpace)\\->\(space)*)",
"(\(space)\\->\(incorrectSpace))",
"\\n\(space)*\\->\(incorrectSpace)",
"\(incorrectSpace)\\->\\n\(space)*"
]
// ex: `func abc()-> Int {` & `func abc() ->Int {`
return "\\)(\(patterns.joined(separator: "|")))\\S+"
}()
private func violationRanges(in file: File, skipParentheses: Bool) -> [NSRange] {
let matches = file.match(pattern: pattern, with: [.typeidentifier])
guard skipParentheses else {
return matches
}
return matches.map {
// skip first (
NSRange(location: $0.location + 1, length: $0.length - 1)
}
}
}