Skip to content

Commit 144b9ff

Browse files
committed
Refactor syntax node manipulation to use variable setters
This commit replaces the use of 'with' method for syntax node manipulation in SwiftSyntax examples with direct variable setters for better clarity and readability.
1 parent fc58fc8 commit 144b9ff

File tree

9 files changed

+114
-120
lines changed

9 files changed

+114
-120
lines changed

Examples/Sources/AddOneToIntegerLiterals/AddOneToIntegerLiterals.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ private class AddOneToIntegerLiterals: SyntaxRewriter {
3131
let int = Int(integerText)!
3232

3333
// Create a new integer literal token with `int + 1` as its text.
34-
let newIntegerLiteralToken = token.with(\.tokenKind, .integerLiteral("\(int + 1)"))
34+
var newIntegerLiteralToken = token
35+
newIntegerLiteralToken.tokenKind = .integerLiteral("\(int + 1)")
3536

3637
// Return the new integer literal.
3738
return newIntegerLiteralToken

Examples/Sources/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacro.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ extension DictionaryStorageMacro: MemberMacro {
2121
providingMembersOf declaration: some DeclGroupSyntax,
2222
in context: some MacroExpansionContext
2323
) throws -> [DeclSyntax] {
24-
let storage: DeclSyntax = "var _storage: [String: Any] = [:]"
25-
return [
26-
storage.with(\.leadingTrivia, [.newlines(1), .spaces(2)])
27-
]
24+
var storage: DeclSyntax = "var _storage: [String: Any] = [:]"
25+
storage.leadingTrivia = [.newlines(1), .spaces(2)]
26+
return [storage]
2827
}
2928
}
3029

@@ -41,14 +40,14 @@ extension DictionaryStorageMacro: MemberAttributeMacro {
4140
return []
4241
}
4342

44-
return [
45-
AttributeSyntax(
46-
attributeName: IdentifierTypeSyntax(
47-
name: .identifier("DictionaryStorageProperty")
48-
)
43+
var attribute = AttributeSyntax(
44+
attributeName: IdentifierTypeSyntax(
45+
name: .identifier("DictionaryStorageProperty")
4946
)
50-
.with(\.leadingTrivia, [.newlines(1), .spaces(2)])
51-
]
47+
)
48+
attribute.leadingTrivia = [.newlines(1), .spaces(2)]
49+
50+
return [attribute]
5251
}
5352
}
5453

Examples/Sources/MacroExamples/Implementation/Expression/AddBlocker.swift

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct AddBlocker: ExpressionMacro {
2727
_ node: InfixOperatorExprSyntax
2828
) -> ExprSyntax {
2929
// Identify any infix operator + in the tree.
30-
if let binOp = node.operator.as(BinaryOperatorExprSyntax.self) {
30+
if var binOp = node.operator.as(BinaryOperatorExprSyntax.self) {
3131
if binOp.operator.text == "+" {
3232
// Form the warning
3333
let messageID = MessageID(domain: "silly", id: "addblock")
@@ -72,17 +72,12 @@ public struct AddBlocker: ExpressionMacro {
7272
)
7373
)
7474

75-
return ExprSyntax(
76-
node.with(
77-
\.operator,
78-
ExprSyntax(
79-
binOp.with(
80-
\.operator,
81-
binOp.operator.with(\.tokenKind, .binaryOperator("-"))
82-
)
83-
)
84-
)
85-
)
75+
binOp.operator.tokenKind = .binaryOperator("-")
76+
77+
var node = node
78+
node.operator = ExprSyntax(binOp)
79+
80+
return ExprSyntax(node)
8681
}
8782
}
8883

Examples/Sources/MacroExamples/Implementation/Expression/FontLiteralMacro.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ private func replaceFirstLabel(
3535
of tuple: LabeledExprListSyntax,
3636
with newLabel: String
3737
) -> LabeledExprListSyntax {
38-
guard let firstElement = tuple.first else {
38+
guard var firstElement = tuple.first else {
3939
return tuple
4040
}
4141

42+
firstElement.label = .identifier(newLabel)
43+
firstElement.colon = .colonToken()
44+
4245
var tuple = tuple
43-
tuple[tuple.startIndex] =
44-
firstElement
45-
.with(\.label, .identifier(newLabel))
46-
.with(\.colon, .colonToken())
46+
tuple[tuple.startIndex] = firstElement
4747
return tuple
4848
}

Examples/Sources/MacroExamples/Implementation/Member/MetaEnumMacro.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ public struct MetaEnumMacro {
2222
let parentParamName: TokenSyntax
2323

2424
init(node: AttributeSyntax, declaration: some DeclGroupSyntax, context: some MacroExpansionContext) throws {
25-
guard let enumDecl = declaration.as(EnumDeclSyntax.self) else {
25+
guard var enumDecl = declaration.as(EnumDeclSyntax.self) else {
2626
throw DiagnosticsError(diagnostics: [
2727
CaseMacroDiagnostic.notAnEnum(declaration).diagnose(at: Syntax(node))
2828
])
2929
}
3030

31-
parentTypeName = enumDecl.name.with(\.trailingTrivia, [])
31+
enumDecl.name.trailingTrivia = []
32+
33+
parentTypeName = enumDecl.name
3234

3335
access = enumDecl.modifiers.first(where: \.isNeededAccessLevelModifier)
3436

3537
childCases = enumDecl.caseElements.map { parentCase in
36-
parentCase.with(\.parameterClause, nil)
38+
var parentCase = parentCase
39+
parentCase.parameterClause = nil
40+
return parentCase
3741
}
3842

3943
parentParamName = context.makeUniqueName("parent")

Examples/Sources/MacroExamples/Implementation/MemberAttribute/WrapStoredPropertiesMacro.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,13 @@ public struct WrapStoredPropertiesMacro: MemberAttributeMacro {
4646
throw CustomError.message("macro requires a string literal containing the name of an attribute")
4747
}
4848

49-
return [
50-
AttributeSyntax(
51-
attributeName: IdentifierTypeSyntax(
52-
name: .identifier(wrapperName.content.text)
53-
)
49+
var attribute = AttributeSyntax(
50+
attributeName: IdentifierTypeSyntax(
51+
name: .identifier(wrapperName.content.text)
5452
)
55-
.with(\.leadingTrivia, [.newlines(1), .spaces(2)])
56-
]
53+
)
54+
attribute.leadingTrivia = [.newlines(1), .spaces(2)]
55+
return [attribute]
5756
}
5857
}
5958

Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public struct AddAsyncMacro: PeerMacro {
3030
) throws -> [DeclSyntax] {
3131

3232
// Only on functions at the moment.
33-
guard let funcDecl = declaration.as(FunctionDeclSyntax.self) else {
33+
guard var funcDecl = declaration.as(FunctionDeclSyntax.self) else {
3434
throw CustomError.message("@addAsync only works on functions")
3535
}
3636

@@ -42,7 +42,9 @@ public struct AddAsyncMacro: PeerMacro {
4242
}
4343

4444
// This only makes sense void functions
45-
if funcDecl.signature.returnClause?.type.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description != "Void" {
45+
guard let returnIdentifier = funcDecl.signature.returnClause?.type.as(IdentifierTypeSyntax.self),
46+
returnIdentifier.name.text == "Void"
47+
else {
4648
throw CustomError.message(
4749
"@addAsync requires an function that returns void"
4850
)
@@ -58,7 +60,9 @@ public struct AddAsyncMacro: PeerMacro {
5860
}
5961

6062
// Completion handler needs to return Void
61-
if completionHandlerParameter.returnClause.type.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description != "Void" {
63+
guard let returnIdentifier = completionHandlerParameter.returnClause.type.as(IdentifierTypeSyntax.self),
64+
returnIdentifier.name.text == "Void"
65+
else {
6266
throw CustomError.message(
6367
"@addAsync requires an function that has a completion handler that returns Void"
6468
)
@@ -72,9 +76,11 @@ public struct AddAsyncMacro: PeerMacro {
7276
// Remove completionHandler and comma from the previous parameter
7377
var newParameterList = funcDecl.signature.parameterClause.parameters
7478
newParameterList.removeLast()
75-
let newParameterListLastParameter = newParameterList.last!
79+
var newParameterListLastParameter = newParameterList.last!
7680
newParameterList.removeLast()
77-
newParameterList.append(newParameterListLastParameter.with(\.trailingTrivia, []).with(\.trailingComma, nil))
81+
newParameterListLastParameter.trailingTrivia = []
82+
newParameterListLastParameter.trailingComma = nil
83+
newParameterList.append(newParameterListLastParameter)
7884

7985
// Drop the @addAsync attribute from the new declaration.
8086
let newAttributeList = funcDecl.attributes.filter {
@@ -121,42 +127,37 @@ public struct AddAsyncMacro: PeerMacro {
121127
122128
"""
123129

124-
let newFunc =
125-
funcDecl
126-
.with(
127-
\.signature,
128-
funcDecl.signature
129-
.with(
130-
\.effectSpecifiers,
131-
FunctionEffectSpecifiersSyntax(
132-
leadingTrivia: .space,
133-
asyncSpecifier: .keyword(.async),
134-
throwsSpecifier: isResultReturn ? .keyword(.throws) : nil
135-
) // add async
136-
)
137-
.with(
138-
\.returnClause,
139-
successReturnType != nil ? ReturnClauseSyntax(leadingTrivia: .space, type: successReturnType!.with(\.leadingTrivia, .space)) : nil
140-
) // add result type
141-
.with(
142-
\.parameterClause,
143-
funcDecl.signature.parameterClause.with(\.parameters, newParameterList) // drop completion handler
144-
.with(\.trailingTrivia, [])
145-
)
146-
)
147-
.with(
148-
\.body,
149-
CodeBlockSyntax(
150-
leftBrace: .leftBraceToken(leadingTrivia: .space),
151-
statements: CodeBlockItemListSyntax(
152-
[CodeBlockItemSyntax(item: .expr(newBody))]
153-
),
154-
rightBrace: .rightBraceToken(leadingTrivia: .newline)
155-
)
156-
)
157-
.with(\.attributes, newAttributeList)
158-
.with(\.leadingTrivia, .newlines(2))
130+
// add async
131+
funcDecl.signature.effectSpecifiers = FunctionEffectSpecifiersSyntax(
132+
leadingTrivia: .space,
133+
asyncSpecifier: .keyword(.async),
134+
throwsSpecifier: isResultReturn ? .keyword(.throws) : nil
135+
)
136+
137+
// add result type
138+
if var successReturnType {
139+
successReturnType.leadingTrivia = .space
140+
funcDecl.signature.returnClause = ReturnClauseSyntax(leadingTrivia: .space, type: successReturnType)
141+
} else {
142+
funcDecl.signature.returnClause = nil
143+
}
144+
145+
// drop completion handler
146+
funcDecl.signature.parameterClause.parameters = newParameterList
147+
funcDecl.signature.parameterClause.trailingTrivia = []
148+
149+
funcDecl.body = CodeBlockSyntax(
150+
leftBrace: .leftBraceToken(leadingTrivia: .space),
151+
statements: CodeBlockItemListSyntax(
152+
[CodeBlockItemSyntax(item: .expr(newBody))]
153+
),
154+
rightBrace: .rightBraceToken(leadingTrivia: .newline)
155+
)
156+
157+
funcDecl.attributes = newAttributeList
158+
159+
funcDecl.leadingTrivia = .newlines(2)
159160

160-
return [DeclSyntax(newFunc)]
161+
return [DeclSyntax(funcDecl)]
161162
}
162163
}

Examples/Sources/MacroExamples/Implementation/Peer/AddCompletionHandlerMacro.swift

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,22 @@ public struct AddCompletionHandlerMacro: PeerMacro {
2525
) throws -> [DeclSyntax] {
2626
// Only on functions at the moment. We could handle initializers as well
2727
// with a bit of work.
28-
guard let funcDecl = declaration.as(FunctionDeclSyntax.self) else {
28+
guard var funcDecl = declaration.as(FunctionDeclSyntax.self) else {
2929
throw CustomError.message("@addCompletionHandler only works on functions")
3030
}
3131

3232
// This only makes sense for async functions.
3333
if funcDecl.signature.effectSpecifiers?.asyncSpecifier == nil {
3434
let newEffects: FunctionEffectSpecifiersSyntax
35-
if let existingEffects = funcDecl.signature.effectSpecifiers {
36-
newEffects = existingEffects.with(\.asyncSpecifier, .keyword(.async))
35+
if var existingEffects = funcDecl.signature.effectSpecifiers {
36+
existingEffects.asyncSpecifier = .keyword(.async)
37+
newEffects = existingEffects
3738
} else {
3839
newEffects = FunctionEffectSpecifiersSyntax(asyncSpecifier: .keyword(.async))
3940
}
4041

41-
let newSignature = funcDecl.signature.with(\.effectSpecifiers, newEffects)
42+
var newSignature = funcDecl.signature
43+
newSignature.effectSpecifiers = newEffects
4244
let messageID = MessageID(domain: "MacroExamples", id: "MissingAsync")
4345

4446
let diag = Diagnostic(
@@ -73,7 +75,9 @@ public struct AddCompletionHandlerMacro: PeerMacro {
7375
}
7476

7577
// Form the completion handler parameter.
76-
let resultType: TypeSyntax? = funcDecl.signature.returnClause?.type.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
78+
var resultType = funcDecl.signature.returnClause?.type
79+
resultType?.leadingTrivia = []
80+
resultType?.trailingTrivia = []
7781

7882
let completionHandlerParam =
7983
FunctionParameterSyntax(
@@ -85,14 +89,12 @@ public struct AddCompletionHandlerMacro: PeerMacro {
8589
// Add the completion handler parameter to the parameter list.
8690
let parameterList = funcDecl.signature.parameterClause.parameters
8791
var newParameterList = parameterList
88-
if let lastParam = parameterList.last {
92+
if var lastParam = parameterList.last {
8993
// We need to add a trailing comma to the preceding list.
9094
newParameterList.removeLast()
95+
lastParam.trailingComma = .commaToken(trailingTrivia: .space)
9196
newParameterList += [
92-
lastParam.with(
93-
\.trailingComma,
94-
.commaToken(trailingTrivia: .space)
95-
),
97+
lastParam,
9698
completionHandlerParam,
9799
]
98100
} else {
@@ -136,35 +138,28 @@ public struct AddCompletionHandlerMacro: PeerMacro {
136138
return attributeType.name.text != nodeType.name.text
137139
}
138140

139-
let newFunc =
140-
funcDecl
141-
.with(
142-
\.signature,
143-
funcDecl.signature
144-
.with(
145-
\.effectSpecifiers,
146-
funcDecl.signature.effectSpecifiers?.with(\.asyncSpecifier, nil) // drop async
147-
)
148-
.with(\.returnClause, nil) // drop result type
149-
.with(
150-
\.parameterClause, // add completion handler parameter
151-
funcDecl.signature.parameterClause.with(\.parameters, newParameterList)
152-
.with(\.trailingTrivia, [])
153-
)
154-
)
155-
.with(
156-
\.body,
157-
CodeBlockSyntax(
158-
leftBrace: .leftBraceToken(leadingTrivia: .space),
159-
statements: CodeBlockItemListSyntax(
160-
[CodeBlockItemSyntax(item: .expr(newBody))]
161-
),
162-
rightBrace: .rightBraceToken(leadingTrivia: .newline)
163-
)
164-
)
165-
.with(\.attributes, newAttributeList)
166-
.with(\.leadingTrivia, .newlines(2))
141+
// drop async
142+
funcDecl.signature.effectSpecifiers?.asyncSpecifier = nil
143+
144+
// drop result type
145+
funcDecl.signature.returnClause = nil
146+
147+
// add completion handler parameter
148+
funcDecl.signature.parameterClause.parameters = newParameterList
149+
funcDecl.signature.parameterClause.trailingTrivia = []
150+
151+
funcDecl.body = CodeBlockSyntax(
152+
leftBrace: .leftBraceToken(leadingTrivia: .space),
153+
statements: CodeBlockItemListSyntax(
154+
[CodeBlockItemSyntax(item: .expr(newBody))]
155+
),
156+
rightBrace: .rightBraceToken(leadingTrivia: .newline)
157+
)
158+
159+
funcDecl.attributes = newAttributeList
160+
161+
funcDecl.leadingTrivia = .newlines(2)
167162

168-
return [DeclSyntax(newFunc)]
163+
return [DeclSyntax(funcDecl)]
169164
}
170165
}

Examples/Sources/MacroExamples/Playground/PeerMacrosPlayground.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func runPeerMacrosPlayground() {
5454
var value = 0
5555
}
5656

57-
let counter = Counter()
57+
_ = Counter()
5858

5959
// print("Peer value with suffix name for \(Counter.self): \(String(describing: Counter_peer))")
6060
}

0 commit comments

Comments
 (0)