forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpeningBraceRule.swift
135 lines (118 loc) · 5.1 KB
/
OpeningBraceRule.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
128
129
130
131
132
133
134
135
//
// OpeningBraceRule.swift
// SwiftLint
//
// Created by Alex Culeva on 10/21/15.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
private let whitespaceAndNewlineCharacterSet = CharacterSet.whitespacesAndNewlines
extension File {
fileprivate func violatingOpeningBraceRanges() -> [NSRange] {
return match(pattern: "((?:[^( ]|[\\s(][\\s]+)\\{)",
excludingSyntaxKinds: SyntaxKind.commentAndStringKinds(),
excludingPattern: "(?:if|guard|while)\\n[^\\{]+?[\\s\\t\\n]\\{")
}
}
public struct OpeningBraceRule: CorrectableRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "opening_brace",
name: "Opening Brace Spacing",
description: "Opening braces should be preceded by a single space and on the same line " +
"as the declaration.",
nonTriggeringExamples: [
"func abc() {\n}",
"[].map() { $0 }",
"[].map({ })",
"if let a = b { }",
"while a == b { }",
"guard let a = b else { }",
"if\n\tlet a = b,\n\tlet c = d\n\twhere a == c\n{ }",
"while\n\tlet a = b,\n\tlet c = d\n\twhere a == c\n{ }",
"guard\n\tlet a = b,\n\tlet c = d\n\twhere a == c else\n{ }"
],
triggeringExamples: [
"func abc(↓){\n}",
"func abc()↓\n\t{ }",
"[].map(↓){ $0 }",
"[].map↓( { } )",
"if let a = b{ }",
"while a == b{ }",
"guard let a = b else{ }",
"if\n\tlet a = b,\n\tlet c = d\n\twhere a == c{ }",
"while\n\tlet a = b,\n\tlet c = d\n\twhere a == c{ }",
"guard\n\tlet a = b,\n\tlet c = d\n\twhere a == c else{ }"
],
corrections: [
"struct Rule{}\n": "struct Rule {}\n",
"struct Rule\n{\n}\n": "struct Rule {\n}\n",
"struct Rule\n\n\t{\n}\n": "struct Rule {\n}\n",
"struct Parent {\n\tstruct Child\n\t{\n\t\tlet foo: Int\n\t}\n}\n":
"struct Parent {\n\tstruct Child {\n\t\tlet foo: Int\n\t}\n}\n",
"[].map(){ $0 }\n": "[].map() { $0 }\n",
"[].map( { })\n": "[].map({ })\n",
"if a == b{ }\n": "if a == b { }\n",
"if\n\tlet a = b,\n\tlet c = d{ }\n": "if\n\tlet a = b,\n\tlet c = d { }\n"
]
)
public func validate(file: File) -> [StyleViolation] {
return file.violatingOpeningBraceRanges().map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
public func correct(file: File) -> [Correction] {
let violatingRanges = file.ruleEnabled(violatingRanges: file.violatingOpeningBraceRanges(), for: self)
var correctedContents = file.contents
var adjustedLocations = [Int]()
for violatingRange in violatingRanges.reversed() {
let (contents, adjustedRange) = correct(contents: correctedContents, violatingRange: violatingRange)
correctedContents = contents
if let adjustedRange = adjustedRange {
adjustedLocations.insert(adjustedRange.location, at: 0)
}
}
file.write(correctedContents)
return adjustedLocations.map {
Correction(ruleDescription: type(of: self).description,
location: Location(file: file, characterOffset: $0))
}
}
private func correct(contents: String,
violatingRange: NSRange) -> (correctedContents: String, adjustedRange: NSRange?) {
guard let indexRange = contents.nsrangeToIndexRange(violatingRange) else {
return (contents, nil)
}
let capturedString = contents[indexRange]
var adjustedRange = violatingRange
var correctString = " {"
// "struct Command{" has violating string = "d{", so ignore first "d"
if capturedString.characters.count == 2 &&
capturedString.rangeOfCharacter(from: whitespaceAndNewlineCharacterSet) == nil {
adjustedRange = NSRange(
location: violatingRange.location + 1,
length: violatingRange.length - 1
)
}
// "[].map( { } )" has violating string = "( {",
// so ignore first "(" and use "{" as correction string instead
if capturedString.hasPrefix("(") {
adjustedRange = NSRange(
location: violatingRange.location + 1,
length: violatingRange.length - 1
)
correctString = "{"
}
if let indexRange = contents.nsrangeToIndexRange(adjustedRange) {
let correctedContents = contents
.replacingCharacters(in: indexRange, with: correctString)
return (correctedContents, adjustedRange)
} else {
return (contents, nil)
}
}
}