From 378a2e99e6da72b46f971cbf5af0a608a8e763c2 Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Tue, 16 Apr 2024 17:25:37 +0800 Subject: [PATCH] feat: Extended format rules --- Plugins/FormatSwift/Plugin.swift | 15 +++------------ Sources/RakuyoSwiftFormatTool/Command.swift | 12 ++++++------ .../RakuyoSwiftFormatTool.swift | 4 ++-- Sources/RakuyoSwiftFormatTool/rakuyo.swiftformat | 7 +++++++ .../RakuyoSwiftFormatToolTests.swift | 9 ++++----- 5 files changed, 22 insertions(+), 25 deletions(-) diff --git a/Plugins/FormatSwift/Plugin.swift b/Plugins/FormatSwift/Plugin.swift index e93f884..ba8e9bc 100644 --- a/Plugins/FormatSwift/Plugin.swift +++ b/Plugins/FormatSwift/Plugin.swift @@ -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) @@ -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, @@ -129,7 +124,6 @@ extension RakuyoSwiftFormatPlugin: CommandPlugin { let rootSwiftFiles = packageDirectoryContents.filter { $0.pathExtension.hasSuffix("swift") } return (subdirectories + rootSwiftFiles).map { $0.path } } - } #if canImport(XcodeProjectPlugin) @@ -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 { @@ -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 @@ -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) } diff --git a/Sources/RakuyoSwiftFormatTool/Command.swift b/Sources/RakuyoSwiftFormatTool/Command.swift index d2eca26..b1e7775 100644 --- a/Sources/RakuyoSwiftFormatTool/Command.swift +++ b/Sources/RakuyoSwiftFormatTool/Command.swift @@ -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() } @@ -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() @@ -45,5 +46,4 @@ struct Command { // swiftlint:disable:next no_direct_standard_out_logs print("[AibnbSwiftFormatTool]", string) } - } diff --git a/Sources/RakuyoSwiftFormatTool/RakuyoSwiftFormatTool.swift b/Sources/RakuyoSwiftFormatTool/RakuyoSwiftFormatTool.swift index 348088a..f901e52 100644 --- a/Sources/RakuyoSwiftFormatTool/RakuyoSwiftFormatTool.swift +++ b/Sources/RakuyoSwiftFormatTool/RakuyoSwiftFormatTool.swift @@ -40,7 +40,7 @@ struct RakuyoSwiftFormatTool: ParsableCommand { var swiftVersion: String? } -// MARK: Main +// MARK: - Main extension RakuyoSwiftFormatTool { func run() throws { @@ -92,7 +92,7 @@ extension RakuyoSwiftFormatTool { } } -// MARK: Private +// MARK: - Private extension RakuyoSwiftFormatTool { /// Whether the command should autocorrect invalid code, or only emit lint errors diff --git a/Sources/RakuyoSwiftFormatTool/rakuyo.swiftformat b/Sources/RakuyoSwiftFormatTool/rakuyo.swiftformat index 8f8bc91..0623d76 100644 --- a/Sources/RakuyoSwiftFormatTool/rakuyo.swiftformat +++ b/Sources/RakuyoSwiftFormatTool/rakuyo.swiftformat @@ -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 @@ -99,3 +100,9 @@ --rules consistentSwitchStatementSpacing --rules semicolons --rules isEmpty +--rules acronyms +--rules modifierOrder +--rules spaceAroundOperators +--rules assertionFailures +--rules blankLinesAroundMark +--rules blankLinesBetweenChainedFunctions diff --git a/Tests/RakuyoSwiftFormatToolTests/RakuyoSwiftFormatToolTests.swift b/Tests/RakuyoSwiftFormatToolTests/RakuyoSwiftFormatToolTests.swift index 95d48ea..df71338 100644 --- a/Tests/RakuyoSwiftFormatToolTests/RakuyoSwiftFormatToolTests.swift +++ b/Tests/RakuyoSwiftFormatToolTests/RakuyoSwiftFormatToolTests.swift @@ -6,9 +6,6 @@ import XCTest // MARK: - RakuyoSwiftFormatToolTest final class RakuyoSwiftFormatToolTest: XCTestCase { - - // MARK: Internal - func testFormatWithNoViolations() { var ranSwiftFormat = false var ranSwiftLint = false @@ -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