Skip to content

Commit

Permalink
ci: lint with swiftformat (#316)
Browse files Browse the repository at this point in the history
1. Adds a CI job that runs `swiftformat --lint` so that formatting is
enforced.
2. Updates the pre-commit hook to use
[git-format-staged](https://github.com/hallettj/git-format-staged) when
formatting.
3. Removes the SwiftPM dependency on SwiftFormat, preferring mise.
4. Formats everything.

Recommend reviewing by commit to see the changes that aren't formatting.

Fixes #33
  • Loading branch information
watt authored Nov 20, 2024
1 parent b90bd8f commit 2ce9f82
Show file tree
Hide file tree
Showing 194 changed files with 1,062 additions and 778 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/swift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,13 @@ jobs:
- name: Swiftdocs
run: |
.buildscript/build_swift_docs.sh ${{ runner.temp }}/swiftdocs
swiftformat:
runs-on: macos-latest

steps:
- uses: actions/checkout@v4
- uses: jdx/mise-action@v2

- name: Run swiftformat
run: swiftformat --lint .
16 changes: 13 additions & 3 deletions .hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
#!/bin/bash

swift_files=$(git diff --diff-filter=d --staged --name-only -- '*.swift')
# Do not run on merge
if [[ $(git rev-parse -q --verify MERGE_HEAD) ]]; then
exit 0
fi

echo $swift_files | xargs swift run swiftformat --quiet
ROOT="$(git rev-parse --show-toplevel)"
SWIFT_FORMAT="mise x -- swiftformat"
GIT_FORMAT_STAGED="$ROOT/Scripts/git-format-staged"
CONFIG="${ROOT}/.swiftformat"

echo $swift_files | xargs git add
$SWIFT_FORMAT --version 1>/dev/null 2>&1
if [ $? -eq 0 ]
then
$GIT_FORMAT_STAGED --formatter "$SWIFT_FORMAT stdin --config "$CONFIG" --stdinpath '{}'" "*.swift"
fi
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@
"version" : "600.0.1"
}
},
{
"identity" : "swiftformat",
"kind" : "remoteSourceControl",
"location" : "https://github.com/nicklockwood/SwiftFormat",
"state" : {
"revision" : "dd989a46d0c6f15c016484bab8afe5e7a67a4022",
"version" : "0.54.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
Expand Down
9 changes: 4 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/ReactiveCocoa/ReactiveSwift.git", from: "7.1.1"),
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.6.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat", exact: "0.54.0"),
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"),
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0" ..< "601.0.0-prerelease"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.1.0"),
.package(url: "https://github.com/pointfreeco/swift-identified-collections", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.4.0"),
Expand Down Expand Up @@ -278,7 +277,7 @@ extension PackageDescription.Product {
}

for target in package.targets {
var settings = target.swiftSettings ?? []
settings.append(.enableExperimentalFeature("StrictConcurrency=targeted"))
target.swiftSettings = settings
var settings = target.swiftSettings ?? []
settings.append(.enableExperimentalFeature("StrictConcurrency=targeted"))
target.swiftSettings = settings
}
2 changes: 1 addition & 1 deletion Samples/AlertContainer/Sources/AlertContainerScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct AlertContainerScreen<BaseScreen: Screen>: Screen {
}

public func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
return AlertContainerViewController.description(for: self, environment: environment)
AlertContainerViewController.description(for: self, environment: environment)
}
}

Expand Down
21 changes: 11 additions & 10 deletions Samples/AlertContainer/Sources/AlertContainerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import UIKit
import Workflow
import WorkflowUI

private struct AlertStyleConstants {
private enum AlertStyleConstants {
static let viewWidth: CGFloat = 343.0
static let buttonTitleColor = UIColor(red: 41 / 255, green: 150 / 255, blue: 204 / 255, alpha: 1.0)
static let titleFont = UIFont.systemFont(ofSize: 18, weight: .medium)
}

internal final class AlertContainerViewController<AlertScreen: Screen>: ScreenViewController<AlertContainerScreen<AlertScreen>> {
final class AlertContainerViewController<AlertScreen: Screen>: ScreenViewController<AlertContainerScreen<AlertScreen>> {
private var baseScreenViewController: DescribedViewController

private let dimmingView = UIView()
Expand Down Expand Up @@ -63,7 +63,7 @@ internal final class AlertContainerViewController<AlertScreen: Screen>: ScreenVi
baseScreenViewController.update(screen: screen.baseScreen, environment: environment)

if let alert = screen.alert {
if let alertView = alertView {
if let alertView {
alertView.alert = alert
} else {
let inAlertView = AlertView(alert: alert)
Expand Down Expand Up @@ -104,7 +104,7 @@ internal final class AlertContainerViewController<AlertScreen: Screen>: ScreenVi
)
}
} else {
if let alertView = alertView {
if let alertView {
UIView.animate(
withDuration: 0.1,
delay: 0,
Expand All @@ -126,23 +126,23 @@ internal final class AlertContainerViewController<AlertScreen: Screen>: ScreenVi
}

override var childForStatusBarStyle: UIViewController? {
return baseScreenViewController
baseScreenViewController
}

override var childForStatusBarHidden: UIViewController? {
return baseScreenViewController
baseScreenViewController
}

override var childForHomeIndicatorAutoHidden: UIViewController? {
return baseScreenViewController
baseScreenViewController
}

override var childForScreenEdgesDeferringSystemGestures: UIViewController? {
return baseScreenViewController
baseScreenViewController
}

override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return baseScreenViewController.supportedInterfaceOrientations
baseScreenViewController.supportedInterfaceOrientations
}
}

Expand Down Expand Up @@ -173,7 +173,7 @@ private final class AlertView: UIView {
}

private func commonInit() {
guard let alert = alert else {
guard let alert else {
return
}
title.text = alert.title
Expand Down Expand Up @@ -253,6 +253,7 @@ private final class AlertButton: UIButton {
addTarget(self, action: #selector(triggerActionHandler), for: .touchUpInside)
}

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand Down
2 changes: 1 addition & 1 deletion Samples/AsyncWorker/Sources/AsyncWorkerWorkflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension AsyncWorkerWorkflow {
}

func makeInitialState() -> AsyncWorkerWorkflow.State {
return State(model: Model(message: "Initial State"))
State(model: Model(message: "Initial State"))
}

func workflowDidChange(from previousWorkflow: AsyncWorkerWorkflow, state: inout State) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

class FakeNetworkManager {
static func makeFakeNetworkRequest() -> FakeRequest {
return FakeRequest()
FakeRequest()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Samples/AsyncWorker/Sources/FakeNetwork/Model.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// FakeModel.swift
// Model.swift
// AsyncWorker
//
// Created by Mark Johnson on 6/16/22.
Expand Down
10 changes: 4 additions & 6 deletions Samples/BackStackContainer/Sources/BackStackContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import WorkflowUI
import UIKit
import WorkflowUI

public final class BackStackContainer<Content: Screen>: ScreenViewController<BackStackScreen<Content>>, UINavigationControllerDelegate {
private let navController = UINavigationController()
Expand Down Expand Up @@ -69,14 +69,12 @@ public final class BackStackContainer<Content: Screen>: ScreenViewController<Bac
return
}

let hidden: Bool

switch topScreen.barVisibility {
let hidden = switch topScreen.barVisibility {
case .hidden:
hidden = true
true

case .visible:
hidden = false
false
}
navController.setNavigationBarHidden(hidden, animated: animated)
}
Expand Down
14 changes: 7 additions & 7 deletions Samples/BackStackContainer/Sources/BackStackScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import WorkflowUI
import UIKit
import WorkflowUI

public struct BackStackScreen<ScreenType: Screen>: Screen {
var items: [Item]
Expand All @@ -25,7 +25,7 @@ public struct BackStackScreen<ScreenType: Screen>: Screen {
}

public func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
return BackStackContainer.description(for: self, environment: environment)
BackStackContainer.description(for: self, environment: environment)
}
}

Expand All @@ -36,10 +36,10 @@ extension BackStackScreen {
public var screen: ScreenType
public var barVisibility: BarVisibility

public init<Key: Hashable>(key: Key?, screen: ScreenType, barVisibility: BarVisibility) {
public init(key: (some Hashable)?, screen: ScreenType, barVisibility: BarVisibility) {
self.screen = screen

if let key = key {
if let key {
self.key = AnyHashable(key)
} else {
self.key = AnyHashable(ObjectIdentifier(ScreenType.self))
Expand All @@ -52,7 +52,7 @@ extension BackStackScreen {
self.init(key: key, screen: screen, barVisibility: barVisibility)
}

public init<Key: Hashable>(key: Key?, screen: ScreenType, barContent: BackStackScreen.BarContent) {
public init(key: (some Hashable)?, screen: ScreenType, barContent: BackStackScreen.BarContent) {
self.init(key: key, screen: screen, barVisibility: .visible(barContent))
}

Expand All @@ -61,7 +61,7 @@ extension BackStackScreen {
self.init(key: key, screen: screen, barContent: barContent)
}

public init<Key: Hashable>(key: Key?, screen: ScreenType) {
public init(key: (some Hashable)?, screen: ScreenType) {
let barVisibility: BarVisibility = .visible(BarContent())
self.init(key: key, screen: screen, barVisibility: barVisibility)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ extension BackStackScreen.BarContent {

/// Convenience factory for a default back button.
public static func back(handler: @escaping () -> Void) -> Button {
return Button(content: .text("Back"), handler: handler)
Button(content: .text("Back"), handler: handler)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import WorkflowUI
import UIKit
import WorkflowUI

/**
Wrapper view controller for being hosted in a backstack. Handles updating the bar button items.
Expand Down Expand Up @@ -58,7 +58,7 @@ final class ScreenWrapperViewController<ScreenType: Screen>: UIViewController {
}

func matches(item: BackStackScreen<ScreenType>.Item) -> Bool {
return item.key == key
item.key == key
&& type(of: item.screen) == ScreenType.self
}

Expand Down Expand Up @@ -97,16 +97,16 @@ final class ScreenWrapperViewController<ScreenType: Screen>: UIViewController {
}
}

let title: String
switch barContent.title {
let title: String = switch barContent.title {
case .none:
title = ""
""
case .text(let text):
title = text
text
}
navigationItem.title = title
}

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand All @@ -127,6 +127,7 @@ extension ScreenWrapperViewController {
update(with: button)
}

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand Down
4 changes: 2 additions & 2 deletions Samples/ModalContainer/Sources/ModalContainerScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct ModalContainerScreen<BaseScreen: Screen>: Screen {
}

public func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
return ModalContainerViewController.description(for: self, environment: environment)
ModalContainerViewController.description(for: self, environment: environment)
}
}

Expand All @@ -55,7 +55,7 @@ public struct ModalContainerScreenModal {
/// A key used to differentiate modal screens during updates
public var key: AnyHashable

public init<Key: Hashable>(screen: AnyScreen, style: Style = .fullScreen, key: Key, animated: Bool = true) {
public init(screen: AnyScreen, style: Style = .fullScreen, key: some Hashable, animated: Bool = true) {
self.screen = screen
self.style = style
self.key = AnyHashable(key)
Expand Down
Loading

0 comments on commit 2ce9f82

Please sign in to comment.