Skip to content

Commit

Permalink
Standardize indentation and code formatting in macro examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Matejkob committed Sep 19, 2023
1 parent 96acf68 commit 919ae85
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
""",
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
"""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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" : "()"))")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 919ae85

Please sign in to comment.