Files
SwiftLint/Source/SwiftLintCore/Extensions/Array+SwiftLint.swift
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

124 lines
4.7 KiB
Swift

import Dispatch
public extension Array where Element: Equatable {
/// The elements in this array, discarding duplicates after the first one.
/// Order-preserving.
var unique: [Element] {
var uniqueValues = [Element]()
for item in self where !uniqueValues.contains(item) {
uniqueValues.append(item)
}
return uniqueValues
}
}
public extension Array where Element: Hashable {
/// Produces an array containing the passed `obj` value.
/// If `obj` is an array already, return it.
/// If `obj` is a set, copy its elements to a new array.
/// If `obj` is a value of type `Element`, return a single-item array containing it.
///
/// - parameter obj: The input.
///
/// - returns: The produced array.
static func array(of obj: Any?) -> [Element]? {
if let array = obj as? [Element] {
return array
} else if let set = obj as? Set<Element> {
return Array(set)
} else if let obj = obj as? Element {
return [obj]
}
return nil
}
}
public extension Array {
/// Produces an array containing the passed `obj` value.
/// If `obj` is an array already, return it.
/// If `obj` is a value of type `Element`, return a single-item array containing it.
///
/// - parameter obj: The input.
///
/// - returns: The produced array.
static func array(of obj: Any?) -> [Element]? {
if let array = obj as? [Element] {
return array
} else if let obj = obj as? Element {
return [obj]
}
return nil
}
/// Group the elements in this array into a dictionary, keyed by applying the specified `transform`.
///
/// - parameter transform: The transformation function to extract an element to its group key.
///
/// - returns: The elements grouped by applying the specified transformation.
func group<U: Hashable>(by transform: (Element) -> U) -> [U: [Element]] {
return Dictionary(grouping: self, by: { transform($0) })
}
/// Returns the elements failing the `belongsInSecondPartition` test, followed by the elements passing the
/// `belongsInSecondPartition` test.
///
/// - parameter belongsInSecondPartition: The test function to determine if the element should be in the second
/// partition.
///
/// - returns: The elements failing the `belongsInSecondPartition` test, followed by the elements passing the
/// `belongsInSecondPartition` test.
func partitioned(by belongsInSecondPartition: (Element) throws -> Bool) rethrows ->
(first: ArraySlice<Element>, second: ArraySlice<Element>) {
var copy = self
let pivot = try copy.partition(by: belongsInSecondPartition)
return (copy[0..<pivot], copy[pivot..<count])
}
/// Same as `flatMap` but spreads the work in the `transform` block in parallel using GCD's `concurrentPerform`.
///
/// - parameter transform: The transformation to apply to each element.
///
/// - returns: The result of applying `transform` on every element and flattening the results.
func parallelFlatMap<T>(transform: (Element) -> [T]) -> [T] {
return parallelMap(transform: transform).flatMap { $0 }
}
/// Same as `compactMap` but spreads the work in the `transform` block in parallel using GCD's `concurrentPerform`.
///
/// - parameter transform: The transformation to apply to each element.
///
/// - returns: The result of applying `transform` on every element and discarding the `nil` ones.
func parallelCompactMap<T>(transform: (Element) -> T?) -> [T] {
return parallelMap(transform: transform).compactMap { $0 }
}
/// Same as `map` but spreads the work in the `transform` block in parallel using GCD's `concurrentPerform`.
///
/// - parameter transform: The transformation to apply to each element.
///
/// - returns: The result of applying `transform` on every element.
func parallelMap<T>(transform: (Element) -> T) -> [T] {
var result = ContiguousArray<T?>(repeating: nil, count: count)
return result.withUnsafeMutableBufferPointer { buffer in
DispatchQueue.concurrentPerform(iterations: buffer.count) { idx in
buffer[idx] = transform(self[idx])
}
return buffer.map { $0! }
}
}
}
public extension Collection {
/// Whether this collection has one or more element.
var isNotEmpty: Bool {
return !isEmpty
}
/// Get the only element in the collection.
///
/// If the collection is empty or contains more than one element the result will be `nil`.
var onlyElement: Element? {
count == 1 ? first : nil
}
}