From 919ae855cc51133411f2f103003fc6904ee13e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Ba=CC=A8k?= Date: Fri, 8 Sep 2023 18:46:03 +0200 Subject: [PATCH] Standardize indentation and code formatting in macro examples --- .../DictionaryIndirectionMacro.swift | 14 ++--- .../Implementation/Member/CustomCodable.swift | 8 +-- .../Implementation/Member/MetaEnumMacro.swift | 39 ++++++------ .../Implementation/Peer/AddAsyncMacro.swift | 15 +++-- .../ExpressionMacrosPlayground.swift | 2 +- .../DictionaryIndirectionMacroTests.swift | 16 ++--- .../ComplexMacros/OptionSetMacroTests.swift | 12 ++-- .../Member/CustomCodableTests.swift | 12 ++-- .../Member/MetaEnumMacroTests.swift | 60 +++++++++---------- .../Peer/AddAsyncMacroTests.swift | 14 ++--- 10 files changed, 93 insertions(+), 99 deletions(-) diff --git a/Examples/Sources/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacro.swift b/Examples/Sources/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacro.swift index 9297cb5bde3..23d7ce421bc 100644 --- a/Examples/Sources/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacro.swift +++ b/Examples/Sources/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacro.swift @@ -81,16 +81,14 @@ public struct DictionaryStoragePropertyMacro: AccessorMacro { return [ """ - - get { - _storage[\(literal: identifier.text), default: \(defaultValue)] as! \(type) - } + get { + _storage[\(literal: identifier.text), default: \(defaultValue)] as! \(type) + } """, """ - - set { - _storage[\(literal: identifier.text)] = newValue - } + set { + _storage[\(literal: identifier.text)] = newValue + } """, ] } diff --git a/Examples/Sources/MacroExamples/Implementation/Member/CustomCodable.swift b/Examples/Sources/MacroExamples/Implementation/Member/CustomCodable.swift index fc5463f94cc..1b4fffeb0b2 100644 --- a/Examples/Sources/MacroExamples/Implementation/Member/CustomCodable.swift +++ b/Examples/Sources/MacroExamples/Implementation/Member/CustomCodable.swift @@ -44,11 +44,9 @@ public enum CustomCodable: MemberMacro { }) let codingKeys: DeclSyntax = """ - - enum CodingKeys: String, CodingKey { - \(raw: cases.joined(separator: "\n")) - } - + enum CodingKeys: String, CodingKey { + \(raw: cases.joined(separator: "\n")) + } """ return [codingKeys] diff --git a/Examples/Sources/MacroExamples/Implementation/Member/MetaEnumMacro.swift b/Examples/Sources/MacroExamples/Implementation/Member/MetaEnumMacro.swift index 780a1f2cb37..4120d050b07 100644 --- a/Examples/Sources/MacroExamples/Implementation/Member/MetaEnumMacro.swift +++ b/Examples/Sources/MacroExamples/Implementation/Member/MetaEnumMacro.swift @@ -41,36 +41,39 @@ public struct MetaEnumMacro { func makeMetaEnum() -> DeclSyntax { // FIXME: Why does this need to be a string to make trailing trivia work properly? - let caseDecls = childCases.map { childCase in - " case \(childCase.name)" - }.joined(separator: "\n") + let caseDecls = + childCases + .map { childCase in + " case \(childCase.name)" + } + .joined(separator: "\n") return """ - - \(access)enum Meta { + \(access)enum Meta { \(raw: caseDecls) \(makeMetaInit()) - } - + } """ } func makeMetaInit() -> DeclSyntax { // FIXME: Why does this need to be a string to make trailing trivia work properly? - let caseStatements = childCases.map { childCase in - """ - case .\(childCase.name): - self = .\(childCase.name) - """ - }.joined(separator: "\n") + let caseStatements = + childCases + .map { childCase in + """ + case .\(childCase.name): + self = .\(childCase.name) + """ + } + .joined(separator: "\n") return """ - - \(access)init(_ \(parentParamName): \(parentTypeName)) { - switch \(parentParamName) { + \(access)init(_ \(parentParamName): \(parentTypeName)) { + switch \(parentParamName) { \(raw: caseStatements) - } - } + } + } """ } } diff --git a/Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift b/Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift index 2a62b999668..da9faa690ba 100644 --- a/Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift +++ b/Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift @@ -101,12 +101,12 @@ public struct AddAsyncMacro: PeerMacro { let switchBody: ExprSyntax = """ - switch returnValue { - case .success(let value): - continuation.resume(returning: value) - case .failure(let error): - continuation.resume(throwing: error) - } + switch returnValue { + case .success(let value): + continuation.resume(returning: value) + case .failure(let error): + continuation.resume(throwing: error) + } """ let newBody: ExprSyntax = @@ -115,8 +115,7 @@ public struct AddAsyncMacro: PeerMacro { \(raw: isResultReturn ? "try await withCheckedThrowingContinuation { continuation in" : "await withCheckedContinuation { continuation in") \(raw: funcDecl.name)(\(raw: callArguments.joined(separator: ", "))) { \(raw: returnType != nil ? "returnValue in" : "") - \(raw: isResultReturn ? switchBody : "continuation.resume(returning: \(raw: returnType != nil ? "returnValue" : "()"))") - + \(raw: isResultReturn ? switchBody : "continuation.resume(returning: \(raw: returnType != nil ? "returnValue" : "()"))") } } diff --git a/Examples/Sources/MacroExamples/Playground/ExpressionMacrosPlayground.swift b/Examples/Sources/MacroExamples/Playground/ExpressionMacrosPlayground.swift index 1fea4704133..17d9fcf897a 100644 --- a/Examples/Sources/MacroExamples/Playground/ExpressionMacrosPlayground.swift +++ b/Examples/Sources/MacroExamples/Playground/ExpressionMacrosPlayground.swift @@ -45,7 +45,7 @@ func runExpressionMacrosPlayground() { // malformed an error is emitted. Otherwise a non-optional URL is expanded. print(#URL("https://swift.org/")) - let domain = "domain.com" + // let domain = "domain.com" //print(#URL("https://\(domain)/api/path")) // error: #URL requires a static string literal //print(#URL("https://not a url.com")) // error: Malformed url diff --git a/Examples/Tests/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacroTests.swift b/Examples/Tests/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacroTests.swift index 0b2b69dee94..03191b51563 100644 --- a/Examples/Tests/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacroTests.swift +++ b/Examples/Tests/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacroTests.swift @@ -34,19 +34,19 @@ final class DictionaryStorageMacroTests: XCTestCase { struct Point { var x: Int = 1 { get { - _storage["x", default: 1] as! Int - } + _storage["x", default: 1] as! Int + } set { - _storage["x"] = newValue - } + _storage["x"] = newValue + } } var y: Int = 2 { get { - _storage["y", default: 2] as! Int - } + _storage["y", default: 2] as! Int + } set { - _storage["y"] = newValue - } + _storage["y"] = newValue + } } var _storage: [String: Any] = [:] diff --git a/Examples/Tests/MacroExamples/Implementation/ComplexMacros/OptionSetMacroTests.swift b/Examples/Tests/MacroExamples/Implementation/ComplexMacros/OptionSetMacroTests.swift index 0f09a656253..7ed8db52f60 100644 --- a/Examples/Tests/MacroExamples/Implementation/ComplexMacros/OptionSetMacroTests.swift +++ b/Examples/Tests/MacroExamples/Implementation/ComplexMacros/OptionSetMacroTests.swift @@ -59,16 +59,16 @@ final class OptionSetMacroTests: XCTestCase { } static let nextDay: Self = - Self (rawValue: 1 << Options.nextDay.rawValue) + Self(rawValue: 1 << Options.nextDay.rawValue) static let secondDay: Self = - Self (rawValue: 1 << Options.secondDay.rawValue) + Self(rawValue: 1 << Options.secondDay.rawValue) static let priority: Self = - Self (rawValue: 1 << Options.priority.rawValue) + Self(rawValue: 1 << Options.priority.rawValue) static let standard: Self = - Self (rawValue: 1 << Options.standard.rawValue) + Self(rawValue: 1 << Options.standard.rawValue) } extension ShippingOptions: OptionSet { @@ -110,10 +110,10 @@ final class OptionSetMacroTests: XCTestCase { } public static let nextDay: Self = - Self (rawValue: 1 << Options.nextDay.rawValue) + Self(rawValue: 1 << Options.nextDay.rawValue) public static let standard: Self = - Self (rawValue: 1 << Options.standard.rawValue) + Self(rawValue: 1 << Options.standard.rawValue) } """, macros: macros, diff --git a/Examples/Tests/MacroExamples/Implementation/Member/CustomCodableTests.swift b/Examples/Tests/MacroExamples/Implementation/Member/CustomCodableTests.swift index 1ad4d8b9b49..ea0f14f5b37 100644 --- a/Examples/Tests/MacroExamples/Implementation/Member/CustomCodableTests.swift +++ b/Examples/Tests/MacroExamples/Implementation/Member/CustomCodableTests.swift @@ -33,9 +33,9 @@ final class CustomCodableTests: XCTestCase { let age: Int enum CodingKeys: String, CodingKey { - case name - case age - } + case name + case age + } } """, macros: macros, @@ -62,9 +62,9 @@ final class CustomCodableTests: XCTestCase { func randomFunction() {} enum CodingKeys: String, CodingKey { - case name - case age = "user_age" - } + case name + case age = "user_age" + } } """, macros: macros, diff --git a/Examples/Tests/MacroExamples/Implementation/Member/MetaEnumMacroTests.swift b/Examples/Tests/MacroExamples/Implementation/Member/MetaEnumMacroTests.swift index 305bdbb6428..e7035fb9880 100644 --- a/Examples/Tests/MacroExamples/Implementation/Member/MetaEnumMacroTests.swift +++ b/Examples/Tests/MacroExamples/Implementation/Member/MetaEnumMacroTests.swift @@ -36,24 +36,23 @@ final class MetaEnumMacroTests: XCTestCase { case null enum Meta { - case integer - case text - case boolean - case null - - init(_ __macro_local_6parentfMu_: Cell) { - switch __macro_local_6parentfMu_ { - case .integer: - self = .integer - case .text: - self = .text - case .boolean: - self = .boolean - case .null: - self = .null - } - } + case integer + case text + case boolean + case null + init(_ __macro_local_6parentfMu_: Cell) { + switch __macro_local_6parentfMu_ { + case .integer: + self = .integer + case .text: + self = .text + case .boolean: + self = .boolean + case .null: + self = .null + } } + } } """, macros: macros, @@ -77,21 +76,20 @@ final class MetaEnumMacroTests: XCTestCase { case boolean(Bool) public enum Meta { - case integer - case text - case boolean - - public init(_ __macro_local_6parentfMu_: Cell) { - switch __macro_local_6parentfMu_ { - case .integer: - self = .integer - case .text: - self = .text - case .boolean: - self = .boolean - } - } + case integer + case text + case boolean + public init(_ __macro_local_6parentfMu_: Cell) { + switch __macro_local_6parentfMu_ { + case .integer: + self = .integer + case .text: + self = .text + case .boolean: + self = .boolean + } } + } } """, macros: macros, diff --git a/Examples/Tests/MacroExamples/Implementation/Peer/AddAsyncMacroTests.swift b/Examples/Tests/MacroExamples/Implementation/Peer/AddAsyncMacroTests.swift index 16893bdb095..89227d3bda8 100644 --- a/Examples/Tests/MacroExamples/Implementation/Peer/AddAsyncMacroTests.swift +++ b/Examples/Tests/MacroExamples/Implementation/Peer/AddAsyncMacroTests.swift @@ -36,12 +36,11 @@ final class AddAsyncMacroTests: XCTestCase { c(a: a, for: b, value) { returnValue in switch returnValue { - case .success(let value): - continuation.resume(returning: value) - case .failure(let error): - continuation.resume(throwing: error) - } - + case .success(let value): + continuation.resume(returning: value) + case .failure(let error): + continuation.resume(throwing: error) + } } } } @@ -68,8 +67,7 @@ final class AddAsyncMacroTests: XCTestCase { await withCheckedContinuation { continuation in d(a: a, for: b, value) { returnValue in - continuation.resume(returning: returnValue) - + continuation.resume(returning: returnValue) } } }