@@ -97,6 +97,8 @@ extension SourceRange {
97
97
98
98
/// Represents the kind of ignore directive encountered in the source.
99
99
enum IgnoreDirective : CustomStringConvertible {
100
+ typealias RegexExpression = Regex < ( Substring , ruleNames: Substring ? ) >
101
+
100
102
/// A node-level directive that disables rules for the following node and its children.
101
103
case node
102
104
/// A file-level directive that disables rules for the entire file.
@@ -113,9 +115,13 @@ enum IgnoreDirective: CustomStringConvertible {
113
115
114
116
/// Regex pattern to match an ignore directive comment.
115
117
/// - 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)
118
+ fileprivate func makeRegex( ) -> RegexExpression {
119
+ switch self {
120
+ case . node:
121
+ return #/^\s*\/\/\s*swift-format-ignore(?:\s*:\s*(?<ruleNames>.+))?$/#
122
+ case . file:
123
+ return #/^\s*\/\/\s*swift-format-ignore-file(?:\s*:\s*(?<ruleNames>.+))?$/#
124
+ }
119
125
}
120
126
}
121
127
@@ -141,10 +147,10 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
141
147
private let sourceLocationConverter : SourceLocationConverter
142
148
143
149
/// Cached regex object for ignoring rules at the node.
144
- private let ignoreRegex : Regex < ( Substring , Substring ? ) >
150
+ private let ignoreRegex : IgnoreDirective . RegexExpression
145
151
146
152
/// Cached regex object for ignoring rules at the file.
147
- private let ignoreFileRegex : Regex < ( Substring , Substring ? ) >
153
+ private let ignoreFileRegex : IgnoreDirective . RegexExpression
148
154
149
155
/// Stores the source ranges in which all rules are ignored.
150
156
var allRulesIgnoredRanges : [ SourceRange ] = [ ]
@@ -203,7 +209,7 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
203
209
private func appendRuleStatus(
204
210
from token: TokenSyntax ,
205
211
of sourceRange: SourceRange ,
206
- using regex: Regex < ( Substring , Substring ? ) >
212
+ using regex: IgnoreDirective . RegexExpression
207
213
) -> SyntaxVisitorContinueKind {
208
214
let isFirstInFile = token. previousToken ( viewMode: . sourceAccurate) == nil
209
215
let comments = loneLineComments ( in: token. leadingTrivia, isFirstToken: isFirstInFile)
@@ -228,12 +234,12 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
228
234
/// match, its contents (e.g. list of rule names) are returned.
229
235
private func ruleStatusDirectiveMatch(
230
236
in text: String ,
231
- using regex: Regex < ( Substring , Substring ? ) >
237
+ using regex: IgnoreDirective . RegexExpression
232
238
) -> RuleStatusDirectiveMatch ? {
233
239
guard let match = text. firstMatch ( of: regex) else {
234
240
return nil
235
241
}
236
- guard let matchedRuleNames = match. output. 1 else {
242
+ guard let matchedRuleNames = match. output. ruleNames else {
237
243
return . all
238
244
}
239
245
let rules = matchedRuleNames. split ( separator: " , " )
0 commit comments