Files
SwiftLint/Source/SwiftLintFramework/Extensions/NSRegularExpression+SwiftLint.swift
T
JP Simard e63e8cad0f Add UnusedDeclarationRule (#2814)
This PR adds a new `unused_declaration` analyzer rule to lint for unused declarations.
By default, detects unused `fileprivate`, `private` and `internal` declarations.
Configure the rule with `include_public_and_open: true` to also detect unused `public` and `open` declarations.

Completely remove the `unused_private_declaration` rule.

This is built on the work enabling collecting rule infrastructure in https://github.com/realm/SwiftLint/pull/2714.
2019-07-18 18:23:43 -07:00

36 lines
1.2 KiB
Swift

import Foundation
private var regexCache = [RegexCacheKey: NSRegularExpression]()
private let regexCacheLock = NSLock()
private struct RegexCacheKey: Hashable {
// Disable unused private declaration rule here because even though we don't use these properties
// directly, we rely on them for their hashable and equatable behavior.
// swiftlint:disable unused_declaration
let pattern: String
let options: NSRegularExpression.Options
// swiftlint:enable unused_declaration
}
extension NSRegularExpression.Options: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(rawValue)
}
}
extension NSRegularExpression {
internal static func cached(pattern: String, options: Options? = nil) throws -> NSRegularExpression {
let options = options ?? [.anchorsMatchLines, .dotMatchesLineSeparators]
let key = RegexCacheKey(pattern: pattern, options: options)
regexCacheLock.lock()
defer { regexCacheLock.unlock() }
if let result = regexCache[key] {
return result
}
let result = try NSRegularExpression(pattern: pattern, options: options)
regexCache[key] = result
return result
}
}