Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve lint performance by optimizing Swift Regex usage #968

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions Sources/SwiftFormat/Core/RuleMask.swift
Original file line number Diff line number Diff line change
@@ -120,7 +120,7 @@ enum IgnoreDirective: CustomStringConvertible {
/// because Windows did not have full support for regex literals until Swift 5.10.
fileprivate func makeRegex() -> RegexExpression {
let pattern = #"^\s*\/\/\s*"# + description + #"(?:\s*:\s*(?<ruleNames>.+))?$"#
return try! Regex(pattern)
return try! Regex(pattern).matchingSemantics(.unicodeScalar)
}
}

@@ -142,14 +142,14 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
case subset(ruleNames: [String])
}

/// Computes source locations and ranges for syntax nodes in a source file.
private let sourceLocationConverter: SourceLocationConverter

/// Cached regex object for ignoring rules at the node.
private let ignoreRegex: IgnoreDirective.RegexExpression
private static let ignoreRegex: IgnoreDirective.RegexExpression = IgnoreDirective.node.makeRegex()

/// Cached regex object for ignoring rules at the file.
private let ignoreFileRegex: IgnoreDirective.RegexExpression
private static let ignoreFileRegex: IgnoreDirective.RegexExpression = IgnoreDirective.file.makeRegex()

/// Computes source locations and ranges for syntax nodes in a source file.
private let sourceLocationConverter: SourceLocationConverter

/// Stores the source ranges in which all rules are ignored.
var allRulesIgnoredRanges: [SourceRange] = []
@@ -158,9 +158,6 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
var ruleMap: [String: [SourceRange]] = [:]

init(sourceLocationConverter: SourceLocationConverter) {
ignoreRegex = IgnoreDirective.node.makeRegex()
ignoreFileRegex = IgnoreDirective.file.makeRegex()

self.sourceLocationConverter = sourceLocationConverter
super.init(viewMode: .sourceAccurate)
}
@@ -176,23 +173,23 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
afterLeadingTrivia: false,
afterTrailingTrivia: true
)
return appendRuleStatus(from: firstToken, of: sourceRange, using: ignoreFileRegex)
return appendRuleStatus(from: firstToken, of: sourceRange, using: Self.ignoreFileRegex)
}

override func visit(_ node: CodeBlockItemSyntax) -> SyntaxVisitorContinueKind {
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
return .visitChildren
}
let sourceRange = node.sourceRange(converter: sourceLocationConverter)
return appendRuleStatus(from: firstToken, of: sourceRange, using: ignoreRegex)
return appendRuleStatus(from: firstToken, of: sourceRange, using: Self.ignoreRegex)
}

override func visit(_ node: MemberBlockItemSyntax) -> SyntaxVisitorContinueKind {
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
return .visitChildren
}
let sourceRange = node.sourceRange(converter: sourceLocationConverter)
return appendRuleStatus(from: firstToken, of: sourceRange, using: ignoreRegex)
return appendRuleStatus(from: firstToken, of: sourceRange, using: Self.ignoreRegex)
}

// MARK: - Helper Methods