Skip to content

Commit 16668dc

Browse files
authored
Add some new swift-format 5.9 rules (apple#356)
Add some new swift-format 5.9 rules ### Motivation I scanned the swift-format 5.9 release notes and found a few new rules that can make our code further consistent. ### Modifications Added those rules and reformatted. The only hand-made change is in `.swift-format`, the rest is the result of rerunning swift-format. ### Result More consistent code, fewer style discussions on PRs. ### Test Plan CI. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.10) - Build finished. ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (compatibility test) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. apple#356
1 parent cf15973 commit 16668dc

File tree

9 files changed

+22
-19
lines changed

9 files changed

+22
-19
lines changed

.swift-format

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"rules" : {
1919
"AllPublicDeclarationsHaveDocumentation" : true,
2020
"AlwaysUseLowerCamelCase" : false,
21+
"AlwaysUseLiteralForEmptyCollectionInit" : true,
2122
"AmbiguousTrailingClosureOverload" : true,
2223
"BeginDocumentationCommentWithOneLineSummary" : false,
2324
"DoNotUseSemicolons" : true,
@@ -38,10 +39,12 @@
3839
"NoLeadingUnderscores" : false,
3940
"NoParensAroundConditions" : true,
4041
"NoVoidReturnOnFunctionSignature" : true,
42+
"OmitExplicitReturns" : true,
4143
"OneCasePerLine" : true,
4244
"OneVariableDeclarationPerLine" : true,
4345
"OnlyOneTrailingClosureArgument" : true,
4446
"OrderedImports" : false,
47+
"ReplaceForEachWithForLoop" : true,
4548
"ReturnVoidInsteadOfEmptyTuple" : true,
4649
"UseEarlyExits" : false,
4750
"UseLetInEveryBoundCaseVariable" : false,

Plugins/OpenAPIGenerator/plugin.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import XcodeProjectPlugin
6262

6363
extension SwiftOpenAPIGeneratorPlugin: XcodeBuildToolPlugin {
6464
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
65-
return try createBuildCommands(
65+
try createBuildCommands(
6666
pluginWorkDirectory: context.pluginWorkDirectory,
6767
tool: context.tool,
6868
sourceFiles: target.inputFiles,

Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ extension Declaration {
12511251

12521252
extension FunctionKind {
12531253
/// Returns a non-failable initializer, for example `init()`.
1254-
static var initializer: Self { return .initializer(failable: false) }
1254+
static var initializer: Self { .initializer(failable: false) }
12551255

12561256
/// Returns a non-static function kind.
12571257
static func function(name: String) -> Self { .function(name: name, isStatic: false) }
@@ -1262,12 +1262,12 @@ extension CodeBlock {
12621262
/// Returns a new declaration code block wrapping the provided declaration.
12631263
/// - Parameter declaration: The declaration to wrap.
12641264
/// - Returns: A new `CodeBlock` instance containing the provided declaration.
1265-
static func declaration(_ declaration: Declaration) -> Self { return CodeBlock(item: .declaration(declaration)) }
1265+
static func declaration(_ declaration: Declaration) -> Self { CodeBlock(item: .declaration(declaration)) }
12661266

12671267
/// Returns a new expression code block wrapping the provided expression.
12681268
/// - Parameter expression: The expression to wrap.
12691269
/// - Returns: A new `CodeBlock` instance containing the provided declaration.
1270-
static func expression(_ expression: Expression) -> Self { return CodeBlock(item: .expression(expression)) }
1270+
static func expression(_ expression: Expression) -> Self { CodeBlock(item: .expression(expression)) }
12711271
}
12721272

12731273
extension Expression {
@@ -1290,7 +1290,7 @@ extension Expression {
12901290
/// expression.
12911291
/// - Parameter member: The name of the member to access on the expression.
12921292
/// - Returns: A new expression representing member access.
1293-
func dot(_ member: String) -> Expression { return .memberAccess(.init(left: self, right: member)) }
1293+
func dot(_ member: String) -> Expression { .memberAccess(.init(left: self, right: member)) }
12941294

12951295
/// Returns a new expression that calls the current expression as a function
12961296
/// with the specified arguments.
@@ -1306,7 +1306,7 @@ extension Expression {
13061306
/// For example: `.foo`, where `member` is `foo`.
13071307
/// - Parameter member: The name of the member to access.
13081308
/// - Returns: A new expression representing member access with a dot prefix.
1309-
static func dot(_ member: String) -> Self { return Self.memberAccess(.init(right: member)) }
1309+
static func dot(_ member: String) -> Self { Self.memberAccess(.init(right: member)) }
13101310

13111311
/// Returns a new identifier expression for the provided pattern, such
13121312
/// as a variable or function name.

Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct YamsParser: ParserProtocol {
2727
/// - Returns: An array of top-level keys as strings.
2828
/// - Throws: An error if there are any issues with parsing the YAML string.
2929
public static func extractTopLevelKeys(fromYAMLString yamlString: String) throws -> [String] {
30-
var yamlKeys = [String]()
30+
var yamlKeys: [String] = []
3131
let parser = try Parser(yaml: yamlString)
3232

3333
if let rootNode = try parser.singleRoot(), case let .mapping(mapping) = rootNode {
@@ -126,7 +126,7 @@ extension Diagnostic {
126126
/// - location: Describes the input file being worked on when the error occurred.
127127
/// - Returns: An error diagnostic.
128128
static func openAPIVersionError(versionString: String, location: Location) -> Diagnostic {
129-
return error(
129+
error(
130130
message:
131131
"Unsupported document version: \(versionString). Please provide a document with OpenAPI versions in the 3.0.x or 3.1.x sets.",
132132
location: location
@@ -137,7 +137,7 @@ extension Diagnostic {
137137
/// - Parameter location: Describes the input file being worked on when the error occurred
138138
/// - Returns: An error diagnostic.
139139
static func openAPIMissingVersionError(location: Location) -> Diagnostic {
140-
return error(
140+
error(
141141
message:
142142
"No openapi key found, please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x or 3.1.x sets.",
143143
location: location

Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extension OperationDescription {
102102

103103
/// Returns the type name of the namespace unique to the operation.
104104
var operationNamespace: TypeName {
105-
return .init(
105+
.init(
106106
components: [.root, .init(swift: Constants.Operations.namespace, json: "paths")]
107107
+ path.components.map { .init(swift: nil, json: $0) } + [
108108
.init(swift: methodName, json: httpMethod.rawValue)

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseHeader.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extension ServerFileTranslator {
126126
func translateResponseHeaderInServer(_ header: TypedResponseHeader, responseVariableName: String) throws
127127
-> Expression
128128
{
129-
return .try(
129+
.try(
130130
.identifierPattern("converter").dot("setHeaderFieldAs\(header.codingStrategy.runtimeName)")
131131
.call([
132132
.init(

Sources/swift-openapi-generator/GenerateOptions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ extension _GenerateOptions {
9898
do {
9999
let data = try Data(contentsOf: config)
100100
let configAsString = String(decoding: data, as: UTF8.self)
101-
var yamlKeys = [String]()
101+
var yamlKeys: [String] = []
102102

103103
do { yamlKeys = try YamsParser.extractTopLevelKeys(fromYAMLString: configAsString) } catch {
104104
throw ValidationError("The config isn't valid. \(error)")

Tests/PetstoreConsumerTests/Test_Client.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ final class Test_Client: XCTestCase {
676676

677677
func testUploadAvatarForPet_500() async throws {
678678
transport = .init { request, requestBody, baseURL, operationID in
679-
return try HTTPResponse(status: .internalServerError, headerFields: [.contentType: "text/plain"])
679+
try HTTPResponse(status: .internalServerError, headerFields: [.contentType: "text/plain"])
680680
.withEncodedBody(Data.efghString)
681681
}
682682
let response = try await client.uploadAvatarForPet(.init(path: .init(petId: 1), body: .binary(.init(.abcd))))

Tests/PetstoreConsumerTests/Test_Server.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ final class Test_Server: XCTestCase {
7979

8080
func testListPets_default() async throws {
8181
client = .init(listPetsBlock: { input in
82-
return .default(statusCode: 400, .init(body: .json(.init(code: 1, me_dollar_sage: "Oh no!"))))
82+
.default(statusCode: 400, .init(body: .json(.init(code: 1, me_dollar_sage: "Oh no!"))))
8383
})
8484
let (response, responseBody) = try await server.listPets(
8585
.init(soar_path: "/api/pets", method: .get),
@@ -377,7 +377,7 @@ final class Test_Server: XCTestCase {
377377
}
378378

379379
func testGetStats_200_json() async throws {
380-
client = .init(getStatsBlock: { input in return .ok(.init(body: .json(.init(count: 1)))) })
380+
client = .init(getStatsBlock: { input in .ok(.init(body: .json(.init(count: 1)))) })
381381
let (response, responseBody) = try await server.getStats(
382382
.init(
383383
soar_path: "/api/pets/stats",
@@ -400,7 +400,7 @@ final class Test_Server: XCTestCase {
400400
}
401401

402402
func testGetStats_200_unexpectedAccept() async throws {
403-
client = .init(getStatsBlock: { input in return .ok(.init(body: .json(.init(count: 1)))) })
403+
client = .init(getStatsBlock: { input in .ok(.init(body: .json(.init(count: 1)))) })
404404
do {
405405
_ = try await server.getStats(
406406
.init(soar_path: "/api/pets/stats", method: .patch, headerFields: [.accept: "foo/bar"]),
@@ -412,7 +412,7 @@ final class Test_Server: XCTestCase {
412412
}
413413

414414
func testGetStats_200_text() async throws {
415-
client = .init(getStatsBlock: { input in return .ok(.init(body: .plainText("count is 1"))) })
415+
client = .init(getStatsBlock: { input in .ok(.init(body: .plainText("count is 1"))) })
416416
let (response, responseBody) = try await server.getStats(
417417
.init(
418418
soar_path: "/api/pets/stats",
@@ -521,7 +521,7 @@ final class Test_Server: XCTestCase {
521521
}
522522

523523
func testGetStats_200_binary() async throws {
524-
client = .init(getStatsBlock: { input in return .ok(.init(body: .binary("count_is_1"))) })
524+
client = .init(getStatsBlock: { input in .ok(.init(body: .binary("count_is_1"))) })
525525
let (response, responseBody) = try await server.getStats(
526526
.init(
527527
soar_path: "/api/pets/stats",
@@ -634,7 +634,7 @@ final class Test_Server: XCTestCase {
634634
}
635635

636636
func testProbe_204() async throws {
637-
client = .init(probeBlock: { input in return .noContent(.init()) })
637+
client = .init(probeBlock: { input in .noContent(.init()) })
638638
let (response, responseBody) = try await server.probe(
639639
.init(soar_path: "/api/probe/", method: .post),
640640
nil,

0 commit comments

Comments
 (0)