-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathMarkRule.swift
222 lines (190 loc) · 9.49 KB
/
MarkRule.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import Foundation
import SourceKittenFramework
public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "mark",
name: "Mark",
description: "MARK comment should be in valid format. e.g. '// MARK: ...' or '// MARK: - ...'",
kind: .lint,
nonTriggeringExamples: [
Example("// MARK: good\n"),
Example("// MARK: - good\n"),
Example("// MARK: -\n"),
Example("// BOOKMARK"),
Example("//BOOKMARK"),
Example("// BOOKMARKS")
],
triggeringExamples: [
Example("↓//MARK: bad"),
Example("↓// MARK:bad"),
Example("↓//MARK:bad"),
Example("↓// MARK: bad"),
Example("↓// MARK: bad"),
Example("↓// MARK: -bad"),
Example("↓// MARK:- bad"),
Example("↓// MARK:-bad"),
Example("↓//MARK: - bad"),
Example("↓//MARK:- bad"),
Example("↓//MARK: -bad"),
Example("↓//MARK:-bad"),
Example("↓//Mark: bad"),
Example("↓// Mark: bad"),
Example("↓// MARK bad"),
Example("↓//MARK bad"),
Example("↓// MARK - bad"),
Example("↓//MARK : bad"),
Example("↓// MARKL:"),
Example("↓// MARKR "),
Example("↓// MARKK -"),
Example("↓/// MARK:"),
Example("↓/// MARK bad"),
issue1029Example
],
corrections: [
Example("↓//MARK: comment"): Example("// MARK: comment"),
Example("↓// MARK: comment"): Example("// MARK: comment"),
Example("↓// MARK:comment"): Example("// MARK: comment"),
Example("↓// MARK: comment"): Example("// MARK: comment"),
Example("↓//MARK: - comment"): Example("// MARK: - comment"),
Example("↓// MARK:- comment"): Example("// MARK: - comment"),
Example("↓// MARK: -comment"): Example("// MARK: - comment"),
Example("↓// MARK: - comment"): Example("// MARK: - comment"),
Example("↓// Mark: comment"): Example("// MARK: comment"),
Example("↓// Mark: - comment"): Example("// MARK: - comment"),
Example("↓// MARK - comment"): Example("// MARK: - comment"),
Example("↓// MARK : comment"): Example("// MARK: comment"),
Example("↓// MARKL:"): Example("// MARK:"),
Example("↓// MARKL: -"): Example("// MARK: -"),
Example("↓// MARKK "): Example("// MARK: "),
Example("↓// MARKK -"): Example("// MARK: -"),
Example("↓/// MARK:"): Example("// MARK:"),
Example("↓/// MARK comment"): Example("// MARK: comment"),
issue1029Example: issue1029Correction
]
)
private let spaceStartPattern = "(?:\(nonSpaceOrTwoOrMoreSpace)\(mark))"
private let endNonSpacePattern = "(?:\(mark)\(nonSpace))"
private let endTwoOrMoreSpacePattern = "(?:\(mark)\(twoOrMoreSpace))"
private let invalidEndSpacesPattern = "(?:\(mark)\(nonSpaceOrTwoOrMoreSpace))"
private let twoOrMoreSpacesAfterHyphenPattern = "(?:\(mark) -\(twoOrMoreSpace))"
private let nonSpaceOrNewlineAfterHyphenPattern = "(?:\(mark) -[^ \n])"
private let invalidSpacesAfterHyphenPattern = "(?:\(mark) -\(nonSpaceOrTwoOrMoreSpaceOrNewline))"
private let invalidLowercasePattern = "(?:// ?[Mm]ark:)"
private let missingColonPattern = "(?:// ?MARK[^:])"
// The below patterns more specifically describe some of the above pattern's failure cases for correction.
private let oneOrMoreSpacesBeforeColonPattern = "(?:// ?MARK +:)"
private let nonWhitespaceBeforeColonPattern = "(?:// ?MARK\\S+:)"
private let nonWhitespaceNorColonBeforeSpacesPattern = "(?:// ?MARK[^\\s:]* +)"
private let threeSlashesInsteadOfTwo = "/// MARK:?"
private var pattern: String {
return [
spaceStartPattern,
invalidEndSpacesPattern,
invalidSpacesAfterHyphenPattern,
invalidLowercasePattern,
missingColonPattern,
threeSlashesInsteadOfTwo
].joined(separator: "|")
}
public func validate(file: SwiftLintFile) -> [StyleViolation] {
return violationRanges(in: file, matching: pattern).map {
StyleViolation(ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
public func correct(file: SwiftLintFile) -> [Correction] {
var result = [Correction]()
result.append(contentsOf: correct(file: file,
pattern: spaceStartPattern,
replaceString: "// MARK:"))
result.append(contentsOf: correct(file: file,
pattern: endNonSpacePattern,
replaceString: "// MARK: ",
keepLastChar: true))
result.append(contentsOf: correct(file: file,
pattern: endTwoOrMoreSpacePattern,
replaceString: "// MARK: "))
result.append(contentsOf: correct(file: file,
pattern: twoOrMoreSpacesAfterHyphenPattern,
replaceString: "// MARK: - "))
result.append(contentsOf: correct(file: file,
pattern: nonSpaceOrNewlineAfterHyphenPattern,
replaceString: "// MARK: - ",
keepLastChar: true))
result.append(contentsOf: correct(file: file,
pattern: oneOrMoreSpacesBeforeColonPattern,
replaceString: "// MARK:",
keepLastChar: false))
result.append(contentsOf: correct(file: file,
pattern: nonWhitespaceBeforeColonPattern,
replaceString: "// MARK:",
keepLastChar: false))
result.append(contentsOf: correct(file: file,
pattern: nonWhitespaceNorColonBeforeSpacesPattern,
replaceString: "// MARK: ",
keepLastChar: false))
result.append(contentsOf: correct(file: file,
pattern: invalidLowercasePattern,
replaceString: "// MARK:"))
result.append(contentsOf: correct(file: file,
pattern: threeSlashesInsteadOfTwo,
replaceString: "// MARK:"))
return result.unique
}
private func correct(file: SwiftLintFile,
pattern: String,
replaceString: String,
keepLastChar: Bool = false) -> [Correction] {
let violations = violationRanges(in: file, matching: pattern)
let matches = file.ruleEnabled(violatingRanges: violations, for: self)
if matches.isEmpty { return [] }
var nsstring = file.contents.bridge()
let description = Self.description
var corrections = [Correction]()
for var range in matches.reversed() {
if keepLastChar {
range.length -= 1
}
let location = Location(file: file, characterOffset: range.location)
nsstring = nsstring.replacingCharacters(in: range, with: replaceString).bridge()
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(nsstring.bridge())
return corrections
}
private func violationRanges(in file: SwiftLintFile, matching pattern: String) -> [NSRange] {
return file.rangesAndTokens(matching: pattern).filter { _, syntaxTokens in
guard let syntaxKind = syntaxTokens.first?.kind else {
return false
}
return syntaxTokens.isNotEmpty && SyntaxKind.commentKinds.contains(syntaxKind)
}.compactMap { range, syntaxTokens in
let byteRange = ByteRange(location: syntaxTokens[0].offset, length: 0)
let identifierRange = file.stringView.byteRangeToNSRange(byteRange)
return identifierRange.map { NSUnionRange($0, range) }
}
}
}
private let issue1029Example = Example("""
↓//MARK:- Top-Level bad mark
↓//MARK:- Another bad mark
struct MarkTest {}
↓// MARK:- Bad mark
extension MarkTest {}
""")
private let issue1029Correction = Example("""
// MARK: - Top-Level bad mark
// MARK: - Another bad mark
struct MarkTest {}
// MARK: - Bad mark
extension MarkTest {}
""")
// These need to be at the bottom of the file to work around https://bugs.swift.org/browse/SR-10486
private let nonSpace = "[^ ]"
private let twoOrMoreSpace = " {2,}"
private let mark = "MARK:"
private let nonSpaceOrTwoOrMoreSpace = "(?:\(nonSpace)|\(twoOrMoreSpace))"
private let nonSpaceOrTwoOrMoreSpaceOrNewline = "(?:[^ \n]|\(twoOrMoreSpace))"