11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import Foundation
14
- import RegexBuilder
15
14
import SwiftSyntax
16
15
17
16
/// This class takes the raw source text and scans through it searching for comments that instruct
@@ -113,30 +112,10 @@ enum IgnoreDirective: CustomStringConvertible {
113
112
}
114
113
115
114
/// Regex pattern to match an ignore directive comment.
116
- /// - Capture group #1 captures `":"`, if present.
117
- /// - Capture group #2 captures the rule name(s), if any.
118
- fileprivate func makeRegex( ) -> Regex < ( Substring , Substring ? , Substring ? ) > {
119
- return Regex {
120
- Anchor . startOfLine
121
- ZeroOrMore ( . whitespace)
122
- " // "
123
- ZeroOrMore ( . whitespace)
124
- description
125
- NegativeLookahead {
126
- CharacterClass . any. subtracting ( . anyOf( " : " ) )
127
- }
128
- Optionally {
129
- Capture {
130
- " : "
131
- }
132
- }
133
- Optionally {
134
- Capture {
135
- OneOrMore ( . whitespace)
136
- OneOrMore ( . any)
137
- }
138
- }
139
- }
115
+ /// - Capture group #1 captures the rule names if `":"` is present.
116
+ fileprivate func makeRegex( ) -> Regex < ( Substring , Substring ? ) > {
117
+ let pattern = #"^\s*\/\/\s*"# + description + #"(?:\s*:\s*(.+))?$"#
118
+ return try ! Regex ( pattern)
140
119
}
141
120
}
142
121
@@ -162,10 +141,10 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
162
141
private let sourceLocationConverter : SourceLocationConverter
163
142
164
143
/// Cached regex object for ignoring rules at the node.
165
- private let ignoreRegex : Regex < ( Substring , Substring ? , Substring ? ) >
144
+ private let ignoreRegex : Regex < ( Substring , Substring ? ) >
166
145
167
146
/// Cached regex object for ignoring rules at the file.
168
- private let ignoreFileRegex : Regex < ( Substring , Substring ? , Substring ? ) >
147
+ private let ignoreFileRegex : Regex < ( Substring , Substring ? ) >
169
148
170
149
/// Stores the source ranges in which all rules are ignored.
171
150
var allRulesIgnoredRanges : [ SourceRange ] = [ ]
@@ -224,7 +203,7 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
224
203
private func appendRuleStatus(
225
204
from token: TokenSyntax ,
226
205
of sourceRange: SourceRange ,
227
- using regex: Regex < ( Substring , Substring ? , Substring ? ) >
206
+ using regex: Regex < ( Substring , Substring ? ) >
228
207
) -> SyntaxVisitorContinueKind {
229
208
let isFirstInFile = token. previousToken ( viewMode: . sourceAccurate) == nil
230
209
let comments = loneLineComments ( in: token. leadingTrivia, isFirstToken: isFirstInFile)
@@ -249,18 +228,14 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
249
228
/// match, its contents (e.g. list of rule names) are returned.
250
229
private func ruleStatusDirectiveMatch(
251
230
in text: String ,
252
- using regex: Regex < ( Substring , Substring ? , Substring ? ) >
231
+ using regex: Regex < ( Substring , Substring ? ) >
253
232
) -> RuleStatusDirectiveMatch ? {
254
233
guard let match = text. firstMatch ( of: regex) else {
255
234
return nil
256
235
}
257
- let hasColon = match. output. 1 != nil
258
- guard hasColon else {
236
+ guard let matchedRuleNames = match. output. 1 else {
259
237
return . all
260
238
}
261
- guard let matchedRuleNames = match. output. 2 else {
262
- return nil
263
- }
264
239
let rules = matchedRuleNames. split ( separator: " , " )
265
240
. map { $0. trimmingCharacters ( in: . whitespaces) }
266
241
. filter { $0. count > 0 }
0 commit comments