forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatementPositionRule.swift
78 lines (67 loc) · 2.64 KB
/
StatementPositionRule.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
//
// StatementPositionRule.swift
// SwiftLint
//
// Created by Alex Culeva on 10/22/15.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct StatementPositionRule: CorrectableRule {
public init() {}
public static let description = RuleDescription(
identifier: "statement_position",
name: "Statement Position",
description: "Else and catch should be on the same line, one space after the previous " +
"declaration.",
nonTriggeringExamples: [
"} else if {",
"} else {",
"} catch {",
"\"}else{\"",
"struct A { let catchphrase: Int }\nlet a = A(\n catchphrase: 0\n)",
"struct A { let `catch`: Int }\nlet a = A(\n `catch`: 0\n)"
],
triggeringExamples: [
"↓}else if {",
"}↓ else {",
"}↓\ncatch {",
"}\n\t↓ catch {"
],
corrections: [
"}\n else {\n": "} else {\n",
"}\n else if {\n": "} else if {\n",
"}\n catch {\n": "} catch {\n"
]
)
public func validateFile(file: File) -> [StyleViolation] {
let pattern = "(?:\\}|[\\s] |[\\n\\t\\r])\\b(?:else|catch)\\b"
return violationRangesInFile(file, withPattern: pattern).flatMap { range in
return StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, characterOffset: range.location))
}
}
public func correctFile(file: File) -> [Correction] {
let pattern = "\\}\\s+((?:else|catch))\\b"
let matches = violationRangesInFile(file, withPattern: pattern)
guard !matches.isEmpty else { return [] }
let regularExpression = regex(pattern)
let description = self.dynamicType.description
var corrections = [Correction]()
var contents = file.contents
for range in matches.reverse() {
contents = regularExpression.stringByReplacingMatchesInString(contents,
options: [], range: range, withTemplate: "} $1")
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents)
return corrections
}
// MARK: - Private Methods
private func violationRangesInFile(file: File, withPattern pattern: String) -> [NSRange] {
return file.matchPattern(pattern).filter { range, syntaxKinds in
return syntaxKinds.startsWith([.Keyword])
}.flatMap { $0.0 }
}
}