Skip to content

Commit 10d8028

Browse files
authored
Enable swift-format checking (apple#711)
Adopts the common swift-mmio and swift-argument-parser format and enables CI checking. Fixes: apple#702
1 parent a01856b commit 10d8028

File tree

140 files changed

+7463
-5243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+7463
-5243
lines changed

.github/workflows/lint.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,25 @@ on:
55
types: [opened, reopened, synchronize]
66

77
jobs:
8+
validate_format_config:
9+
name: Validate Format Config
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repo
13+
uses: actions/checkout@v4
14+
15+
- name: Install apt dependencies
16+
run: sudo apt-get -qq update && sudo apt-get -qq -y install curl
17+
18+
- name: Compare against swift-mmio swift-format config
19+
run: |
20+
curl -sL https://raw.githubusercontent.com/apple/swift-mmio/refs/heads/main/.swift-format -o .swift-format-mmio
21+
diff .swift-format .swift-format-mmio
22+
823
soundness:
924
name: Soundness
1025
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
1126
with:
12-
format_check_enabled: false # bug: https://github.com/apple/swift-argument-parser/issues/702
1327
license_header_check_enabled: false # feature: https://github.com/swiftlang/github-workflows/issues/78
1428
license_header_check_project_name: "Swift Argument Parser" # bug: https://github.com/swiftlang/github-workflows/issues/76
1529
shell_check_enabled: false # bug: https://github.com/apple/swift-argument-parser/issues/703

.swift-format

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"fileScopedDeclarationPrivacy" : {
3+
"accessLevel" : "private"
4+
},
5+
"indentation" : {
6+
"spaces" : 2
7+
},
8+
"indentConditionalCompilationBlocks" : false,
9+
"indentSwitchCaseLabels" : false,
10+
"lineBreakAroundMultilineExpressionChainComponents" : false,
11+
"lineBreakBeforeControlFlowKeywords" : false,
12+
"lineBreakBeforeEachArgument" : false,
13+
"lineBreakBeforeEachGenericRequirement" : false,
14+
"lineLength" : 80,
15+
"maximumBlankLines" : 1,
16+
"multiElementCollectionTrailingCommas" : true,
17+
"noAssignmentInExpressions" : {
18+
"allowedFunctions" : [
19+
"XCTAssertNoThrow"
20+
]
21+
},
22+
"prioritizeKeepingFunctionOutputTogether" : false,
23+
"respectsExistingLineBreaks" : true,
24+
"rules" : {
25+
"AllPublicDeclarationsHaveDocumentation" : false,
26+
"AlwaysUseLiteralForEmptyCollectionInit" : true,
27+
"AlwaysUseLowerCamelCase" : true,
28+
"AmbiguousTrailingClosureOverload" : false,
29+
"BeginDocumentationCommentWithOneLineSummary" : true,
30+
"DoNotUseSemicolons" : true,
31+
"DontRepeatTypeInStaticProperties" : true,
32+
"FileScopedDeclarationPrivacy" : true,
33+
"FullyIndirectEnum" : true,
34+
"GroupNumericLiterals" : true,
35+
"IdentifiersMustBeASCII" : true,
36+
"NeverForceUnwrap" : true,
37+
"NeverUseForceTry" : true,
38+
"NeverUseImplicitlyUnwrappedOptionals" : true,
39+
"NoAccessLevelOnExtensionDeclaration" : true,
40+
"NoAssignmentInExpressions" : true,
41+
"NoBlockComments" : true,
42+
"NoCasesWithOnlyFallthrough" : true,
43+
"NoEmptyTrailingClosureParentheses" : true,
44+
"NoLabelsInCasePatterns" : true,
45+
"NoLeadingUnderscores" : false,
46+
"NoParensAroundConditions" : true,
47+
"NoPlaygroundLiterals" : true,
48+
"NoVoidReturnOnFunctionSignature" : true,
49+
"OmitExplicitReturns" : true,
50+
"OneCasePerLine" : true,
51+
"OneVariableDeclarationPerLine" : true,
52+
"OnlyOneTrailingClosureArgument" : true,
53+
"OrderedImports" : true,
54+
"ReplaceForEachWithForLoop" : true,
55+
"ReturnVoidInsteadOfEmptyTuple" : true,
56+
"TypeNamesShouldBeCapitalized" : true,
57+
"UseEarlyExits" : false,
58+
"UseExplicitNilCheckInConditions" : true,
59+
"UseLetInEveryBoundCaseVariable" : true,
60+
"UseShorthandTypeNames" : true,
61+
"UseSingleLinePropertyGetter" : true,
62+
"UseSynthesizedInitializer" : true,
63+
"UseTripleSlashForDocumentationComments" : true,
64+
"UseWhereClausesInForLoops" : false,
65+
"ValidateDocumentationComments" : true
66+
},
67+
"spacesBeforeEndOfLineComments": 2,
68+
"spacesAroundRangeFormationOperators" : false,
69+
"tabWidth" : 2,
70+
"version" : 1
71+
}

Examples/color/Color.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ struct Color: ParsableCommand {
1717
@Option(help: "Your favorite color.")
1818
var fav: ColorOptions
1919

20-
@Option(help: .init("Your second favorite color.", discussion: "This is optional."))
20+
@Option(
21+
help: .init("Your second favorite color.", discussion: "This is optional."))
2122
var second: ColorOptions?
2223

2324
func run() {

Examples/count-lines/CountLines.swift

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,57 @@ import Foundation
1515
@main
1616
@available(macOS 12, *)
1717
struct CountLines: AsyncParsableCommand {
18-
@Argument(
19-
help: "A file to count lines in. If omitted, counts the lines of stdin.",
20-
completion: .file(), transform: URL.init(fileURLWithPath:))
21-
var inputFile: URL? = nil
22-
23-
@Option(help: "Only count lines with this prefix.")
24-
var prefix: String? = nil
25-
26-
@Flag(help: "Include extra information in the output.")
27-
var verbose = false
18+
@Argument(
19+
help: "A file to count lines in. If omitted, counts the lines of stdin.",
20+
completion: .file(), transform: URL.init(fileURLWithPath:))
21+
var inputFile: URL? = nil
22+
23+
@Option(help: "Only count lines with this prefix.")
24+
var prefix: String? = nil
25+
26+
@Flag(help: "Include extra information in the output.")
27+
var verbose = false
2828
}
2929

3030
@available(macOS 12, *)
3131
extension CountLines {
32-
var fileHandle: FileHandle {
33-
get throws {
34-
guard let inputFile else {
35-
return .standardInput
36-
}
37-
return try FileHandle(forReadingFrom: inputFile)
38-
}
32+
var fileHandle: FileHandle {
33+
get throws {
34+
guard let inputFile else {
35+
return .standardInput
36+
}
37+
return try FileHandle(forReadingFrom: inputFile)
3938
}
40-
41-
func printCount(_ count: Int) {
42-
guard verbose else {
43-
print(count)
44-
return
45-
}
46-
47-
if let filename = inputFile?.lastPathComponent {
48-
print("Lines in '\(filename)'", terminator: "")
49-
} else {
50-
print("Lines from stdin", terminator: "")
51-
}
52-
53-
if let prefix {
54-
print(", prefixed by '\(prefix)'", terminator: "")
55-
}
56-
57-
print(": \(count)")
39+
}
40+
41+
func printCount(_ count: Int) {
42+
guard verbose else {
43+
print(count)
44+
return
45+
}
46+
47+
if let filename = inputFile?.lastPathComponent {
48+
print("Lines in '\(filename)'", terminator: "")
49+
} else {
50+
print("Lines from stdin", terminator: "")
5851
}
59-
60-
mutating func run() async throws {
61-
let countAllLines = prefix == nil
62-
let lineCount = try await fileHandle.bytes.lines.reduce(0) { count, line in
63-
if countAllLines || line.starts(with: prefix!) {
64-
return count + 1
65-
} else {
66-
return count
67-
}
68-
}
69-
70-
printCount(lineCount)
52+
53+
if let prefix {
54+
print(", prefixed by '\(prefix)'", terminator: "")
55+
}
56+
57+
print(": \(count)")
58+
}
59+
60+
mutating func run() async throws {
61+
var lineCount = 0
62+
for try await line in try fileHandle.bytes.lines {
63+
if let prefix {
64+
lineCount += line.starts(with: prefix) ? 1 : 0
65+
} else {
66+
lineCount += 1
67+
}
7168
}
69+
printCount(lineCount)
70+
}
7271
}

0 commit comments

Comments
 (0)