Skip to content

Commit

Permalink
Run swift-format
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis authored and github-actions[bot] committed Oct 3, 2023
1 parent 2754f76 commit 3c9424a
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Tests/MacroTestingTests/CustomCodableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class CustomCodableMacroTests: BaseTestCase {
struct Person {
let name: String
@CodableKey("user_age") let age: Int
func randomFunction() {}
}
"""
Expand Down
2 changes: 1 addition & 1 deletion Tests/MacroTestingTests/FuncUniqueMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest
final class FuncUniqueMacroTests: BaseTestCase {
override func invokeTest() {
withMacroTesting(
macros: [FuncUniqueMacro.self,]
macros: [FuncUniqueMacro.self]
) {
super.invokeTest()
}
Expand Down
27 changes: 20 additions & 7 deletions Tests/MacroTestingTests/MacroExamples/AddAsyncMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,30 @@ public struct AddAsyncMacro: PeerMacro {
}

// This only makes sense void functions
if funcDecl.signature.returnClause?.type.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description != "Void" {
if funcDecl.signature.returnClause?.type.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
.description != "Void"
{
throw CustomError.message(
"@addAsync requires an function that returns void"
)
}

// Requires a completion handler block as last parameter
guard let completionHandlerParameterAttribute = funcDecl.signature.parameterClause.parameters.last?.type.as(AttributedTypeSyntax.self),
let completionHandlerParameter = completionHandlerParameterAttribute.baseType.as(FunctionTypeSyntax.self)
guard
let completionHandlerParameterAttribute = funcDecl.signature.parameterClause.parameters.last?
.type.as(AttributedTypeSyntax.self),
let completionHandlerParameter = completionHandlerParameterAttribute.baseType.as(
FunctionTypeSyntax.self)
else {
throw CustomError.message(
"@addAsync requires an function that has a completion handler as last parameter"
)
}

// Completion handler needs to return Void
if completionHandlerParameter.returnClause.type.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description != "Void" {
if completionHandlerParameter.returnClause.type.with(\.leadingTrivia, []).with(
\.trailingTrivia, []
).description != "Void" {
throw CustomError.message(
"@addAsync requires an function that has a completion handler that returns Void"
)
Expand All @@ -67,14 +74,18 @@ public struct AddAsyncMacro: PeerMacro {
let returnType = completionHandlerParameter.parameters.first?.type

let isResultReturn = returnType?.children(viewMode: .all).first?.description == "Result"
let successReturnType = isResultReturn ? returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument : returnType
let successReturnType =
isResultReturn
? returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument
: returnType

// Remove completionHandler and comma from the previous parameter
var newParameterList = funcDecl.signature.parameterClause.parameters
newParameterList.removeLast()
let newParameterListLastParameter = newParameterList.last!
newParameterList.removeLast()
newParameterList.append(newParameterListLastParameter.with(\.trailingTrivia, []).with(\.trailingComma, nil))
newParameterList.append(
newParameterListLastParameter.with(\.trailingTrivia, []).with(\.trailingComma, nil))

// Drop the @addAsync attribute from the new declaration.
let newAttributeList = funcDecl.attributes.filter {
Expand Down Expand Up @@ -136,7 +147,9 @@ public struct AddAsyncMacro: PeerMacro {
)
.with(
\.returnClause,
successReturnType != nil ? ReturnClauseSyntax(leadingTrivia: .space, type: successReturnType!.with(\.leadingTrivia, .space)) : nil
successReturnType != nil
? ReturnClauseSyntax(
leadingTrivia: .space, type: successReturnType!.with(\.leadingTrivia, .space)) : nil
) // add result type
.with(
\.parameterClause,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public struct AddCompletionHandlerMacro: PeerMacro {
}

// Form the completion handler parameter.
let resultType: TypeSyntax? = funcDecl.signature.returnClause?.type.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
let resultType: TypeSyntax? = funcDecl.signature.returnClause?.type.with(\.leadingTrivia, [])
.with(\.trailingTrivia, [])

let completionHandlerParam =
FunctionParameterSyntax(
Expand Down
12 changes: 8 additions & 4 deletions Tests/MacroTestingTests/MacroExamples/CustomCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ public enum CustomCodable: MemberMacro {
let cases = memberList.compactMap({ member -> String? in
// is a property
guard
let propertyName = member.decl.as(VariableDeclSyntax.self)?.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier.text
let propertyName = member.decl.as(VariableDeclSyntax.self)?.bindings.first?.pattern.as(
IdentifierPatternSyntax.self)?.identifier.text
else {
return nil
}

// if it has a CodableKey macro on it
if let customKeyMacro = member.decl.as(VariableDeclSyntax.self)?.attributes.first(where: { element in
element.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.description == "CodableKey"
if let customKeyMacro = member.decl.as(VariableDeclSyntax.self)?.attributes.first(where: {
element in
element.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.description
== "CodableKey"
}) {

// Uses the value in the Macro
let customKeyValue = customKeyMacro.as(AttributeSyntax.self)!.arguments!.as(LabeledExprListSyntax.self)!.first!.expression
let customKeyValue = customKeyMacro.as(AttributeSyntax.self)!.arguments!.as(
LabeledExprListSyntax.self)!.first!.expression

return "case \(propertyName) = \(customKeyValue)"
} else {
Expand Down
9 changes: 6 additions & 3 deletions Tests/MacroTestingTests/MacroExamples/MetaEnumMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public struct MetaEnumMacro {
let access: DeclModifierListSyntax.Element?
let parentParamName: TokenSyntax

init(node: AttributeSyntax, declaration: some DeclGroupSyntax, context: some MacroExpansionContext) throws {
init(
node: AttributeSyntax, declaration: some DeclGroupSyntax, context: some MacroExpansionContext
) throws {
guard let enumDecl = declaration.as(EnumDeclSyntax.self) else {
throw DiagnosticsError(diagnostics: [
CaseMacroDiagnostic.notAnEnum(declaration).diagnose(at: Syntax(node))
Expand Down Expand Up @@ -94,7 +96,7 @@ extension EnumDeclSyntax {
var caseElements: [EnumCaseElementSyntax] {
memberBlock.members.flatMap { member in
guard let caseDecl = member.decl.as(EnumCaseDeclSyntax.self) else {
return Array<EnumCaseElementSyntax>()
return [EnumCaseElementSyntax]()
}

return Array(caseDecl.elements)
Expand All @@ -110,7 +112,8 @@ extension CaseMacroDiagnostic: DiagnosticMessage {
var message: String {
switch self {
case .notAnEnum(let decl):
return "'@MetaEnum' can only be attached to an enum, not \(decl.descriptiveDeclKind(withArticle: true))"
return
"'@MetaEnum' can only be attached to an enum, not \(decl.descriptiveDeclKind(withArticle: true))"
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/MacroTestingTests/MacroExamples/NewTypeMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ extension NewTypeMacro: MemberMacro {
.expression.as(MemberAccessExprSyntax.self),
let rawType = memberAccessExn.base?.as(DeclReferenceExprSyntax.self)
else {
throw CustomError.message(#"@NewType requires the raw type as an argument, in the form "RawType.self"."#)
throw CustomError.message(
#"@NewType requires the raw type as an argument, in the form "RawType.self"."#)
}

guard let declaration = declaration.as(StructDeclSyntax.self) else {
Expand Down
23 changes: 16 additions & 7 deletions Tests/MacroTestingTests/MacroExamples/OptionSetMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public struct OptionSetMacro {
stringLiteral.segments.count == 1,
case let .stringSegment(optionsEnumNameString)? = stringLiteral.segments.first
else {
context.diagnose(OptionSetMacroDiagnostic.requiresStringLiteral(optionsEnumNameArgumentLabel).diagnose(at: optionEnumNameArg.expression))
context.diagnose(
OptionSetMacroDiagnostic.requiresStringLiteral(optionsEnumNameArgumentLabel).diagnose(
at: optionEnumNameArg.expression))
return nil
}

Expand All @@ -118,12 +120,15 @@ public struct OptionSetMacro {
return nil
}).first
else {
context.diagnose(OptionSetMacroDiagnostic.requiresOptionsEnum(optionsEnumName).diagnose(at: decl))
context.diagnose(
OptionSetMacroDiagnostic.requiresOptionsEnum(optionsEnumName).diagnose(at: decl))
return nil
}

// Retrieve the raw type from the attribute.
guard let genericArgs = attribute.attributeName.as(IdentifierTypeSyntax.self)?.genericArgumentClause,
guard
let genericArgs = attribute.attributeName.as(IdentifierTypeSyntax.self)?
.genericArgumentClause,
let rawType = genericArgs.arguments.first?.argument
else {
context.diagnose(OptionSetMacroDiagnostic.requiresOptionsEnumRawType.diagnose(at: attribute))
Expand All @@ -143,13 +148,15 @@ extension OptionSetMacro: ExtensionMacro {
in context: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax] {
// Decode the expansion arguments.
guard let (structDecl, _, _) = decodeExpansion(of: node, attachedTo: declaration, in: context) else {
guard let (structDecl, _, _) = decodeExpansion(of: node, attachedTo: declaration, in: context)
else {
return []
}

// If there is an explicit conformance to OptionSet already, don't add one.
if let inheritedTypes = structDecl.inheritanceClause?.inheritedTypes,
inheritedTypes.contains(where: { inherited in inherited.type.trimmedDescription == "OptionSet" })
inheritedTypes.contains(where: { inherited in inherited.type.trimmedDescription == "OptionSet"
})
{
return []
}
Expand All @@ -165,14 +172,16 @@ extension OptionSetMacro: MemberMacro {
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
// Decode the expansion arguments.
guard let (_, optionsEnum, rawType) = decodeExpansion(of: attribute, attachedTo: decl, in: context) else {
guard
let (_, optionsEnum, rawType) = decodeExpansion(of: attribute, attachedTo: decl, in: context)
else {
return []
}

// Find all of the case elements.
let caseElements = optionsEnum.memberBlock.members.flatMap { member in
guard let caseDecl = member.decl.as(EnumCaseDeclSyntax.self) else {
return Array<EnumCaseElementSyntax>()
return [EnumCaseElementSyntax]()
}

return Array(caseDecl.elements)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public struct WrapStoredPropertiesMacro: MemberAttributeMacro {
stringLiteral.segments.count == 1,
case let .stringSegment(wrapperName)? = stringLiteral.segments.first
else {
throw CustomError.message("macro requires a string literal containing the name of an attribute")
throw CustomError.message(
"macro requires a string literal containing the name of an attribute")
}

return [
Expand Down
4 changes: 2 additions & 2 deletions Tests/MacroTestingTests/MemberDeprecatedMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ final class MemberDepreacatedMacroTests: XCTestCase {
@memberDeprecated
public struct SomeStruct {
typealias MacroName = String
public var oldProperty: Int = 420
func oldMethod() {
print("This is an old method.")
}
Expand Down

0 comments on commit 3c9424a

Please sign in to comment.