SwiftLint/Source/SwiftLintFramework/Models/Region.swift

59 lines
2.6 KiB
Swift

/// A contiguous region of Swift source code.
public struct Region: Equatable {
/// The location describing the start of the region. All locations that are less than this value
/// (earlier in the source file) are not contained in this region.
public let start: Location
/// The location describing the end of the region. All locations that are greater than this value
/// (later in the source file) are not contained in this region.
public let end: Location
/// All SwiftLint rule identifiers that are disabled in this region.
public let disabledRuleIdentifiers: Set<RuleIdentifier>
/// Creates a Region by setting explicit values for all its properties.
///
/// - parameter start: The region's starting location.
/// - parameter end: The region's ending location.
/// - parameter disabledRuleIdentifiers: All SwiftLint rule identifiers that are disabled in this region.
public init(start: Location, end: Location, disabledRuleIdentifiers: Set<RuleIdentifier>) {
self.start = start
self.end = end
self.disabledRuleIdentifiers = disabledRuleIdentifiers
}
/// Whether the specific location is contained in this region.
///
/// - parameter location: The location to check for containment.
public func contains(_ location: Location) -> Bool {
return start <= location && end >= location
}
/// Whether the specified rule is enabled in this region.
///
/// - parameter rule: The rule whose status should be determined.
public func isRuleEnabled(_ rule: Rule) -> Bool {
return !isRuleDisabled(rule)
}
/// Whether the specified rule is disabled in this region.
///
/// - parameter rule: The rule whose status should be determined.
public func isRuleDisabled(_ rule: Rule) -> Bool {
guard !disabledRuleIdentifiers.contains(.all) else {
return true
}
let identifiersToCheck = type(of: rule).description.allIdentifiers
let regionIdentifiers = Set(disabledRuleIdentifiers.map { $0.stringRepresentation })
return !regionIdentifiers.isDisjoint(with: identifiersToCheck)
}
/// Returns the deprecated rule aliases that are disabling the specified rule in this region.
/// Returns the empty set if the rule isn't disabled in this region.
///
/// - parameter rule: The rule to check.
public func deprecatedAliasesDisabling(rule: Rule) -> Set<String> {
let identifiers = type(of: rule).description.deprecatedAliases
return Set(disabledRuleIdentifiers.map { $0.stringRepresentation }).intersection(identifiers)
}
}