Skip to content

Commit

Permalink
feat: Extended format rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rakuyoMo committed Apr 16, 2024
1 parent f0cde78 commit 378a2e9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
15 changes: 3 additions & 12 deletions Plugins/FormatSwift/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ struct RakuyoSwiftFormatPlugin {
// MARK: CommandPlugin

extension RakuyoSwiftFormatPlugin: CommandPlugin {

// MARK: Internal

func performCommand(context: PluginContext, arguments: [String]) async throws {
var argumentExtractor = ArgumentExtractor(arguments)

Expand Down Expand Up @@ -106,8 +103,6 @@ extension RakuyoSwiftFormatPlugin: CommandPlugin {
)
}

// MARK: Private

/// Retrieves the list of paths that should be formatted / linted
///
/// By default this tool runs on all subdirectories of the package's root directory,
Expand All @@ -129,7 +124,6 @@ extension RakuyoSwiftFormatPlugin: CommandPlugin {
let rootSwiftFiles = packageDirectoryContents.filter { $0.pathExtension.hasSuffix("swift") }
return (subdirectories + rootSwiftFiles).map { $0.path }
}

}

#if canImport(XcodeProjectPlugin)
Expand Down Expand Up @@ -184,9 +178,6 @@ struct SwiftVersion: Comparable {
}

extension Package {

// MARK: Internal

/// The minimum Swift version supported by this package
var minimumSwiftVersion: SwiftVersion {
guard let version = supportedSwiftVersions.min() else {
Expand All @@ -195,8 +186,6 @@ extension Package {
return version
}

// MARK: Private

/// Swift versions supported by this package. Guaranteed to be non-empty.
/// - This includes the `swift-tools-version` from the `Package.swift`,
/// plus the Swift version of any additional version-specific Package manifest
Expand Down Expand Up @@ -241,7 +230,9 @@ extension Package {
let major = string.popFirst().flatMap({ Int(String($0)) }),
string.popFirst() == ".",
let minor = string.popFirst().flatMap({ Int(String($0)) })
else { return nil }
else {
return nil
}

return SwiftVersion(major: major, minor: minor)
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/RakuyoSwiftFormatTool/Command.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Foundation

// MARK: - Command

/// A single command line invocation
struct Command {

// MARK: Internal

/// This property can be overridden to provide a mock implementation in unit tests.
static var runCommand: (Self) throws -> Int32 = { try $0.executeShellCommand() }

Expand All @@ -17,9 +16,11 @@ struct Command {
func run() throws -> Int32 {
try Self.runCommand(self)
}

// MARK: Private
}

// MARK: - Private

extension Command {
/// Synchronously runs this command and returns its exit code
private func executeShellCommand() throws -> Int32 {
let process = Process()
Expand All @@ -45,5 +46,4 @@ struct Command {
// swiftlint:disable:next no_direct_standard_out_logs
print("[AibnbSwiftFormatTool]", string)
}

}
4 changes: 2 additions & 2 deletions Sources/RakuyoSwiftFormatTool/RakuyoSwiftFormatTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct RakuyoSwiftFormatTool: ParsableCommand {
var swiftVersion: String?
}

// MARK: Main
// MARK: - Main

extension RakuyoSwiftFormatTool {
func run() throws {
Expand Down Expand Up @@ -92,7 +92,7 @@ extension RakuyoSwiftFormatTool {
}
}

// MARK: Private
// MARK: - Private

extension RakuyoSwiftFormatTool {
/// Whether the command should autocorrect invalid code, or only emit lint errors
Expand Down
7 changes: 7 additions & 0 deletions Sources/RakuyoSwiftFormatTool/rakuyo.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
--shortoptionals always #typeSugar
--semicolons never #semicolons
--doccomments preserve #docComments
--modifierorder open,public,final,fileprivate,private,private(set),static,override,convenience,lazy,weak #modifierOrder

# We recommend a max width of 100 but _strictly enforce_ a max width of 130
--maxwidth 130 # wrap
Expand Down Expand Up @@ -99,3 +100,9 @@
--rules consistentSwitchStatementSpacing
--rules semicolons
--rules isEmpty
--rules acronyms
--rules modifierOrder
--rules spaceAroundOperators
--rules assertionFailures
--rules blankLinesAroundMark
--rules blankLinesBetweenChainedFunctions
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import XCTest
// MARK: - RakuyoSwiftFormatToolTest

final class RakuyoSwiftFormatToolTest: XCTestCase {

// MARK: Internal

func testFormatWithNoViolations() {
var ranSwiftFormat = false
var ranSwiftLint = false
Expand Down Expand Up @@ -244,9 +241,11 @@ final class RakuyoSwiftFormatToolTest: XCTestCase {
XCTAssertEqual(unexpectedSwiftFormatExitCode as? ExitCode, ExitCode(1234))
XCTAssertEqual(unexpectedSwiftLintExitCode as? ExitCode, ExitCode(42))
}

// MARK: Private
}

// MARK: - Private

extension RakuyoSwiftFormatToolTest {
/// Runs `RakuyoSwiftFormatTool` with the `Command` calls mocked using the given mocks
private func runFormatTool(arguments: [String]? = nil, with mocks: MockCommands) -> Error? {
let existingRunCommandImplementation = Command.runCommand
Expand Down

0 comments on commit 378a2e9

Please sign in to comment.