Skip to content

Commit a51d245

Browse files
committed
test regex literals
1 parent d952156 commit a51d245

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Sources/SwiftFormat/Core/RuleMask.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ extension SourceRange {
9797

9898
/// Represents the kind of ignore directive encountered in the source.
9999
enum IgnoreDirective: CustomStringConvertible {
100+
typealias RegexExpression = Regex<(Substring, ruleNames: Substring?)>
101+
100102
/// A node-level directive that disables rules for the following node and its children.
101103
case node
102104
/// A file-level directive that disables rules for the entire file.
@@ -113,9 +115,13 @@ enum IgnoreDirective: CustomStringConvertible {
113115

114116
/// Regex pattern to match an ignore directive comment.
115117
/// - 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+
}
119125
}
120126
}
121127

@@ -141,10 +147,10 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
141147
private let sourceLocationConverter: SourceLocationConverter
142148

143149
/// Cached regex object for ignoring rules at the node.
144-
private let ignoreRegex: Regex<(Substring, Substring?)>
150+
private let ignoreRegex: IgnoreDirective.RegexExpression
145151

146152
/// Cached regex object for ignoring rules at the file.
147-
private let ignoreFileRegex: Regex<(Substring, Substring?)>
153+
private let ignoreFileRegex: IgnoreDirective.RegexExpression
148154

149155
/// Stores the source ranges in which all rules are ignored.
150156
var allRulesIgnoredRanges: [SourceRange] = []
@@ -203,7 +209,7 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
203209
private func appendRuleStatus(
204210
from token: TokenSyntax,
205211
of sourceRange: SourceRange,
206-
using regex: Regex<(Substring, Substring?)>
212+
using regex: IgnoreDirective.RegexExpression
207213
) -> SyntaxVisitorContinueKind {
208214
let isFirstInFile = token.previousToken(viewMode: .sourceAccurate) == nil
209215
let comments = loneLineComments(in: token.leadingTrivia, isFirstToken: isFirstInFile)
@@ -228,12 +234,12 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
228234
/// match, its contents (e.g. list of rule names) are returned.
229235
private func ruleStatusDirectiveMatch(
230236
in text: String,
231-
using regex: Regex<(Substring, Substring?)>
237+
using regex: IgnoreDirective.RegexExpression
232238
) -> RuleStatusDirectiveMatch? {
233239
guard let match = text.firstMatch(of: regex) else {
234240
return nil
235241
}
236-
guard let matchedRuleNames = match.output.1 else {
242+
guard let matchedRuleNames = match.output.ruleNames else {
237243
return .all
238244
}
239245
let rules = matchedRuleNames.split(separator: ",")

0 commit comments

Comments
 (0)