forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpeningBraceRule.swift
More file actions
147 lines (131 loc) · 5.58 KB
/
OpeningBraceRule.swift
File metadata and controls
147 lines (131 loc) · 5.58 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
//
// 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
private extension File {
func violatingOpeningBraceRanges() -> [(range: NSRange, location: Int)] {
return match(pattern: "(?:[^( ]|[\\s(][\\s]+)\\{",
excludingSyntaxKinds: SyntaxKind.commentAndStringKinds,
excludingPattern: "(?:if|guard|while)\\n[^\\{]+?[\\s\\t\\n]\\{").map {
let branceRange = contents.bridge().range(of: "{", options: .literal, range: $0)
return ($0, branceRange.location)
}
}
}
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.",
kind: .style,
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{ }",
"struct Rule {}\n",
"struct Parent {\n\tstruct Child {\n\t\tlet foo: Int\n\t}\n}\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↓{ }",
"struct Rule↓{}\n",
"struct Rule\n↓{\n}\n",
"struct Rule\n\n\t↓{\n}\n",
"struct Parent {\n\tstruct Child\n\t↓{\n\t\tlet foo: Int\n\t}\n}\n"
],
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.violatingOpeningBraceRanges().filter {
!file.ruleEnabled(violatingRanges: [$0.range], for: self).isEmpty
}
var correctedContents = file.contents
var adjustedLocations = [Location]()
for (violatingRange, location) in violatingRanges.reversed() {
correctedContents = correct(contents: correctedContents, violatingRange: violatingRange)
adjustedLocations.insert(Location(file: file, characterOffset: location), at: 0)
}
file.write(correctedContents)
return adjustedLocations.map {
Correction(ruleDescription: type(of: self).description,
location: $0)
}
}
private func correct(contents: String,
violatingRange: NSRange) -> String {
guard let indexRange = contents.nsrangeToIndexRange(violatingRange) else {
return contents
}
#if swift(>=4.0)
let capturedString = String(contents[indexRange])
#else
let capturedString = contents[indexRange]
#endif
var adjustedRange = violatingRange
var correctString = " {"
// "struct Command{" has violating string = "d{", so ignore first "d"
if capturedString.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
} else {
return contents
}
}
}