mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
86d60400c1
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.
57 lines
2.5 KiB
Swift
57 lines
2.5 KiB
Swift
import Foundation
|
|
|
|
/// An interface for enumerating files that can be linted by SwiftLint.
|
|
public protocol LintableFileManager {
|
|
/// Returns all files that can be linted in the specified path. If the path is relative, it will be appended to the
|
|
/// specified root path, or currentt working directory if no root directory is specified.
|
|
///
|
|
/// - parameter path: The path in which lintable files should be found.
|
|
/// - parameter rootDirectory: The parent directory for the specified path. If none is provided, the current working
|
|
/// directory will be used.
|
|
///
|
|
/// - returns: Files to lint.
|
|
func filesToLint(inPath path: String, rootDirectory: String?) -> [String]
|
|
|
|
/// Returns the date when the file at the specified path was last modified. Returns `nil` if the file cannot be
|
|
/// found or its last modification date cannot be determined.
|
|
///
|
|
/// - parameter path: The file whose modification date should be determined.
|
|
///
|
|
/// - returns: A date, if one was determined.
|
|
func modificationDate(forFileAtPath path: String) -> Date?
|
|
|
|
/// Returns true if a file (but not a directory) exists at the specified path.
|
|
///
|
|
/// - parameter path: The path that should be checked to see if it is a file.
|
|
///
|
|
/// - returns: true if the specified path is a file.
|
|
func isFile(atPath path: String) -> Bool
|
|
}
|
|
|
|
extension FileManager: LintableFileManager {
|
|
public func filesToLint(inPath path: String, rootDirectory: String? = nil) -> [String] {
|
|
let absolutePath = path.bridge()
|
|
.absolutePathRepresentation(rootDirectory: rootDirectory ?? currentDirectoryPath).bridge()
|
|
.standardizingPath
|
|
|
|
// if path is a file, it won't be returned in `enumerator(atPath:)`
|
|
if absolutePath.bridge().isSwiftFile() && absolutePath.isFile {
|
|
return [absolutePath]
|
|
}
|
|
|
|
return subpaths(atPath: absolutePath)?.parallelCompactMap { element -> String? in
|
|
guard element.bridge().isSwiftFile() else { return nil }
|
|
let absoluteElementPath = absolutePath.bridge().appendingPathComponent(element)
|
|
return absoluteElementPath.isFile ? absoluteElementPath : nil
|
|
} ?? []
|
|
}
|
|
|
|
public func modificationDate(forFileAtPath path: String) -> Date? {
|
|
return (try? attributesOfItem(atPath: path))?[.modificationDate] as? Date
|
|
}
|
|
|
|
public func isFile(atPath path: String) -> Bool {
|
|
path.isFile
|
|
}
|
|
}
|