Skip to content

Commit 0727d7f

Browse files
authored
Fix unexpected expansion (#7)
* Update MacroTesting example tests This brings in the latest versions of Apple's macro demos and tests from SwiftSyntax. * Fix unexpected expansion Fixes #6. * wip * wip
1 parent 3c9424a commit 0727d7f

File tree

10 files changed

+24
-48
lines changed

10 files changed

+24
-48
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let package = Package(
1818
],
1919
dependencies: [
2020
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
21-
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.14.0"),
21+
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.14.1"),
2222
],
2323
targets: [
2424
.target(

Sources/MacroTesting/AssertMacro.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ public func assertMacro(
297297
)
298298
}
299299

300-
if allDiagnostics.filter({ $0.diagMessage.severity == .error }).isEmpty
301-
|| expandedSource != nil
302-
{
300+
if allDiagnostics.filter({ $0.diagMessage.severity == .error }).isEmpty {
303301
offset += 1
304302
assertInlineSnapshot(
305303
of: expandedSourceFile.description.trimmingCharacters(in: .newlines),

Tests/MacroTestingTests/MacroExamples/AddAsyncMacro.swift

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,23 @@ public struct AddAsyncMacro: PeerMacro {
4242
}
4343

4444
// This only makes sense void functions
45-
if funcDecl.signature.returnClause?.type.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
46-
.description != "Void"
47-
{
45+
if funcDecl.signature.returnClause?.type.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description != "Void" {
4846
throw CustomError.message(
4947
"@addAsync requires an function that returns void"
5048
)
5149
}
5250

5351
// Requires a completion handler block as last parameter
54-
guard
55-
let completionHandlerParameterAttribute = funcDecl.signature.parameterClause.parameters.last?
56-
.type.as(AttributedTypeSyntax.self),
57-
let completionHandlerParameter = completionHandlerParameterAttribute.baseType.as(
58-
FunctionTypeSyntax.self)
52+
guard let completionHandlerParameterAttribute = funcDecl.signature.parameterClause.parameters.last?.type.as(AttributedTypeSyntax.self),
53+
let completionHandlerParameter = completionHandlerParameterAttribute.baseType.as(FunctionTypeSyntax.self)
5954
else {
6055
throw CustomError.message(
6156
"@addAsync requires an function that has a completion handler as last parameter"
6257
)
6358
}
6459

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

7669
let isResultReturn = returnType?.children(viewMode: .all).first?.description == "Result"
77-
let successReturnType =
78-
isResultReturn
79-
? returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument
80-
: returnType
70+
let successReturnType = isResultReturn ? returnType!.as(IdentifierTypeSyntax.self)!.genericArgumentClause?.arguments.first!.argument : returnType
8171

8272
// Remove completionHandler and comma from the previous parameter
8373
var newParameterList = funcDecl.signature.parameterClause.parameters
8474
newParameterList.removeLast()
8575
let newParameterListLastParameter = newParameterList.last!
8676
newParameterList.removeLast()
87-
newParameterList.append(
88-
newParameterListLastParameter.with(\.trailingTrivia, []).with(\.trailingComma, nil))
77+
newParameterList.append(newParameterListLastParameter.with(\.trailingTrivia, []).with(\.trailingComma, nil))
8978

9079
// Drop the @addAsync attribute from the new declaration.
9180
let newAttributeList = funcDecl.attributes.filter {
@@ -147,9 +136,7 @@ public struct AddAsyncMacro: PeerMacro {
147136
)
148137
.with(
149138
\.returnClause,
150-
successReturnType != nil
151-
? ReturnClauseSyntax(
152-
leadingTrivia: .space, type: successReturnType!.with(\.leadingTrivia, .space)) : nil
139+
successReturnType != nil ? ReturnClauseSyntax(leadingTrivia: .space, type: successReturnType!.with(\.leadingTrivia, .space)) : nil
153140
) // add result type
154141
.with(
155142
\.parameterClause,

Tests/MacroTestingTests/MacroExamples/AddCompletionHandlerMacro.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public struct AddCompletionHandlerMacro: PeerMacro {
7373
}
7474

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

7978
let completionHandlerParam =
8079
FunctionParameterSyntax(

Tests/MacroTestingTests/MacroExamples/CustomCodable.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,18 @@ public enum CustomCodable: MemberMacro {
2424
let cases = memberList.compactMap({ member -> String? in
2525
// is a property
2626
guard
27-
let propertyName = member.decl.as(VariableDeclSyntax.self)?.bindings.first?.pattern.as(
28-
IdentifierPatternSyntax.self)?.identifier.text
27+
let propertyName = member.decl.as(VariableDeclSyntax.self)?.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier.text
2928
else {
3029
return nil
3130
}
3231

3332
// if it has a CodableKey macro on it
34-
if let customKeyMacro = member.decl.as(VariableDeclSyntax.self)?.attributes.first(where: {
35-
element in
36-
element.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.description
37-
== "CodableKey"
33+
if let customKeyMacro = member.decl.as(VariableDeclSyntax.self)?.attributes.first(where: { element in
34+
element.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.description == "CodableKey"
3835
}) {
3936

4037
// Uses the value in the Macro
41-
let customKeyValue = customKeyMacro.as(AttributeSyntax.self)!.arguments!.as(
42-
LabeledExprListSyntax.self)!.first!.expression
38+
let customKeyValue = customKeyMacro.as(AttributeSyntax.self)!.arguments!.as(LabeledExprListSyntax.self)!.first!.expression
4339

4440
return "case \(propertyName) = \(customKeyValue)"
4541
} else {

Tests/MacroTestingTests/MacroExamples/MetaEnumMacro.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public struct MetaEnumMacro {
2121
let access: DeclModifierListSyntax.Element?
2222
let parentParamName: TokenSyntax
2323

24-
init(
25-
node: AttributeSyntax, declaration: some DeclGroupSyntax, context: some MacroExpansionContext
26-
) throws {
24+
init(node: AttributeSyntax, declaration: some DeclGroupSyntax, context: some MacroExpansionContext) throws {
2725
guard let enumDecl = declaration.as(EnumDeclSyntax.self) else {
2826
throw DiagnosticsError(diagnostics: [
2927
CaseMacroDiagnostic.notAnEnum(declaration).diagnose(at: Syntax(node))
@@ -96,7 +94,7 @@ extension EnumDeclSyntax {
9694
var caseElements: [EnumCaseElementSyntax] {
9795
memberBlock.members.flatMap { member in
9896
guard let caseDecl = member.decl.as(EnumCaseDeclSyntax.self) else {
99-
return [EnumCaseElementSyntax]()
97+
return Array<EnumCaseElementSyntax>()
10098
}
10199

102100
return Array(caseDecl.elements)
@@ -112,8 +110,7 @@ extension CaseMacroDiagnostic: DiagnosticMessage {
112110
var message: String {
113111
switch self {
114112
case .notAnEnum(let decl):
115-
return
116-
"'@MetaEnum' can only be attached to an enum, not \(decl.descriptiveDeclKind(withArticle: true))"
113+
return "'@MetaEnum' can only be attached to an enum, not \(decl.descriptiveDeclKind(withArticle: true))"
117114
}
118115
}
119116

Tests/MacroTestingTests/MacroExamples/NewTypeMacro.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ extension NewTypeMacro: MemberMacro {
3030
.expression.as(MemberAccessExprSyntax.self),
3131
let rawType = memberAccessExn.base?.as(DeclReferenceExprSyntax.self)
3232
else {
33-
throw CustomError.message(
34-
#"@NewType requires the raw type as an argument, in the form "RawType.self"."#)
33+
throw CustomError.message(#"@NewType requires the raw type as an argument, in the form "RawType.self"."#)
3534
}
3635

3736
guard let declaration = declaration.as(StructDeclSyntax.self) else {

Tests/MacroTestingTests/MacroExamples/OptionSetMacro.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ extension OptionSetMacro: ExtensionMacro {
155155

156156
// If there is an explicit conformance to OptionSet already, don't add one.
157157
if let inheritedTypes = structDecl.inheritanceClause?.inheritedTypes,
158-
inheritedTypes.contains(where: { inherited in inherited.type.trimmedDescription == "OptionSet"
159-
})
158+
inheritedTypes.contains(
159+
where: { inherited in inherited.type.trimmedDescription == "OptionSet" }
160+
)
160161
{
161162
return []
162163
}

Tests/MacroTestingTests/MacroExamples/WrapStoredPropertiesMacro.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public struct WrapStoredPropertiesMacro: MemberAttributeMacro {
4343
stringLiteral.segments.count == 1,
4444
case let .stringSegment(wrapperName)? = stringLiteral.segments.first
4545
else {
46-
throw CustomError.message(
47-
"macro requires a string literal containing the name of an attribute")
46+
throw CustomError.message("macro requires a string literal containing the name of an attribute")
4847
}
4948

5049
return [

0 commit comments

Comments
 (0)