Files
SwiftLint/Source/SwiftLintCore/Models/Region.swift
T
JP Simard 86d60400c1 Move core SwiftLint functionality to new SwiftLintCore module
Over the years, SwiftLintFramework had become a fairly massive monolith,
containing over 400 source files with both core infrastructure and
rules.

Architecturally, the rules should rely on the core infrastructure but
not the other way around. There are two exceptions to this:
`custom_rules` and `superfluous_disable_command` which need special
integration with the linter infrastructure.

Now the time has come to formalize this architecture and one way to do
that is to move the core SwiftLint functionality out of
SwiftLintFramework and into a new SwiftLintCore module that the rules
can depend on.

Beyond enforcing architectural patterns, this also has the advantage of
speeding up incremental compilation by skipping rebuilding the core
functionality when iterating on rules.

Because the core functionality is always useful when building rules, I'm
opting to import SwiftLintCore in SwiftLintFramework as `@_exported` so
that it's implicitly available to all files in SwiftLintFramework
without needing to import it directly.

In a follow-up I'll also split the built-in rules and the extra rules
into their own modules. More modularization is possible from there, but
not planned.

The bulk of this PR just moves files from `Source/SwiftLintFramework/*`
to `Source/SwiftLintCore/*`. There are some other changes that can't be
split up into their own PRs:

* Change jazzy to document the SwiftLintCore module instead of
  SwiftLintFramework.
* Change imports in unit tests to reflect where code was moved to.
* Update `sourcery` make rule to reflect where code was moved to.
* Create a new `coreRules` array and register those rules with the
  registry. This allows the `custom_rules` and
  `superfluous_disable_command` rule implementations to remain internal
  to the SwiftLintCore module, preventing more implementation details
  from leaking across architectural layers.
* Move `RuleRegistry.registerAllRulesOnce()` out of the type declaration
  and up one level so it can access rules defined downstream from
  SwiftLintCore.
2023-04-26 21:10:19 -04:00

86 lines
3.8 KiB
Swift

import SwiftSyntax
/// 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.
///
/// - returns: True if the specific location is contained in this region.
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.
///
/// - returns: True if the specified rule is enabled in this region.
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.
///
/// - returns: True if the specified rule is disabled in this region.
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.
///
/// - returns: Deprecated rule aliases.
public func deprecatedAliasesDisabling(rule: Rule) -> Set<String> {
let identifiers = type(of: rule).description.deprecatedAliases
return Set(disabledRuleIdentifiers.map { $0.stringRepresentation }).intersection(identifiers)
}
/// Converts this `Region` to a SwiftSyntax `SourceRange`.
///
/// - parameter locationConverter: The SwiftSyntax location converter to use.
///
/// - returns: The `SourceRange` if one was produced.
func toSourceRange(locationConverter: SourceLocationConverter) -> SourceRange? {
guard let startLine = start.line, let endLine = end.line else {
return nil
}
let startPosition = locationConverter.position(ofLine: startLine, column: min(1000, start.character ?? 1))
let endPosition = locationConverter.position(ofLine: endLine, column: min(1000, end.character ?? 1))
let startLocation = locationConverter.location(for: startPosition)
let endLocation = locationConverter.location(for: endPosition)
return SourceRange(start: startLocation, end: endLocation)
}
}