Skip to content

Commit dadb48b

Browse files
committed
Adjustment for removed with<childName> functions in SwiftSyntax
1 parent c2ad27c commit dadb48b

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension ASTGenVisitor {
2020
let tupleElement = TupleExprElementSyntax(
2121
label: nil, colon: nil, expression: ExprSyntax(trailingClosure), trailingComma: nil)
2222

23-
return visit(node.addArgument(tupleElement).withTrailingClosure(nil))
23+
return visit(node.addArgument(tupleElement).with(\.trailingClosure, nil))
2424
}
2525

2626
let args = visit(node.argumentList).rawValue

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func evaluateMacro(
162162
print("not on a macro expansion node: \(token.recursiveDescription)")
163163
return -1
164164
}
165-
macroName = parentExpansion.macro.withoutTrivia().description
165+
macroName = parentExpansion.macro.text
166166
evaluatedSyntax = Syntax(try exprMacro.expansion(of: parentExpansion, in: &context))
167167

168168
// Handle expression macro. The resulting decls are wrapped in a `CodeBlockItemListSyntax`.
@@ -172,7 +172,7 @@ func evaluateMacro(
172172
return -1
173173
}
174174
let decls = try declMacro.expansion(of: parentExpansion, in: &context)
175-
macroName = parentExpansion.macro.withoutTrivia().description
175+
macroName = parentExpansion.macro.text
176176
evaluatedSyntax = Syntax(CodeBlockItemListSyntax(
177177
decls.map { CodeBlockItemSyntax(item: .decl($0)) }))
178178

@@ -201,7 +201,7 @@ func evaluateMacro(
201201
)
202202
}
203203

204-
var evaluatedSyntaxStr = evaluatedSyntax.withoutTrivia().description
204+
var evaluatedSyntaxStr = evaluatedSyntax.trimmedDescription
205205
evaluatedSyntaxStr.withUTF8 { utf8 in
206206
let evaluatedResultPtr = UnsafeMutablePointer<UInt8>.allocate(capacity: utf8.count + 1)
207207
if let baseAddress = utf8.baseAddress {
@@ -315,7 +315,7 @@ func expandAttachedMacro(
315315

316316
// Form a buffer of accessor declarations to return to the caller.
317317
evaluatedSyntaxStr = accessors.map {
318-
$0.withoutTrivia().description
318+
$0.trimmedDescription
319319
}.joined(separator: "\n\n")
320320

321321
case let attachedMacro as MemberAttributeMacro.Type:
@@ -338,7 +338,7 @@ func expandAttachedMacro(
338338

339339
// Form a buffer containing an attribute list to return to the caller.
340340
evaluatedSyntaxStr = attributes.map {
341-
$0.withoutTrivia().description
341+
$0.trimmedDescription
342342
}.joined(separator: " ")
343343

344344
default:

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private func replaceFirstLabel(
1414
}
1515

1616
return tuple.replacing(
17-
childAt: 0, with: firstElement.withLabel(.identifier(newLabel)))
17+
childAt: 0, with: firstElement.with(\.label, .identifier(newLabel)))
1818
}
1919

2020
public struct ColorLiteralMacro: ExpressionMacro {
@@ -26,7 +26,7 @@ public struct ColorLiteralMacro: ExpressionMacro {
2626
)
2727
let initSyntax: ExprSyntax = ".init(\(argList))"
2828
if let leadingTrivia = macro.leadingTrivia {
29-
return initSyntax.withLeadingTrivia(leadingTrivia)
29+
return initSyntax.with(\.leadingTrivia, leadingTrivia)
3030
}
3131
return initSyntax
3232
}
@@ -38,7 +38,7 @@ public struct FileIDMacro: ExpressionMacro {
3838
) -> ExprSyntax {
3939
let fileLiteral: ExprSyntax = #""\#(raw: context.moduleName)/\#(raw: context.fileName)""#
4040
if let leadingTrivia = macro.leadingTrivia {
41-
return fileLiteral.withLeadingTrivia(leadingTrivia)
41+
return fileLiteral.with(\.leadingTrivia, leadingTrivia)
4242
}
4343
return fileLiteral
4444
}
@@ -82,7 +82,7 @@ public enum AddBlocker: ExpressionMacro {
8282
}
8383

8484
// Link the folded argument back into the tree.
85-
let node = node.withArgumentList(node.argumentList.replacing(childAt: 0, with: node.argumentList.first!.withExpression(foldedArgument.as(ExprSyntax.self)!)))
85+
let node = node.with(\.argumentList, node.argumentList.replacing(childAt: 0, with: node.argumentList.first!.with(\.expression, foldedArgument.as(ExprSyntax.self)!)))
8686

8787
class AddVisitor: SyntaxRewriter {
8888
var diagnostics: [Diagnostic] = []
@@ -102,8 +102,8 @@ public enum AddBlocker: ExpressionMacro {
102102
severity: .error
103103
),
104104
highlights: [
105-
Syntax(node.leftOperand.withoutTrivia()),
106-
Syntax(node.rightOperand.withoutTrivia())
105+
Syntax(node.leftOperand.with(\.leadingTrivia, []).with(\.trailingTrivia, [])),
106+
Syntax(node.rightOperand.with(\.leadingTrivia, []).with(\.trailingTrivia, []))
107107
],
108108
fixIts: [
109109
FixIt(
@@ -114,7 +114,7 @@ public enum AddBlocker: ExpressionMacro {
114114
),
115115
changes: [
116116
FixIt.Change.replace(
117-
oldNode: Syntax(binOp.operatorToken.withoutTrivia()),
117+
oldNode: Syntax(binOp.operatorToken.with(\.leadingTrivia, []).with(\.trailingTrivia, [])),
118118
newNode: Syntax(
119119
TokenSyntax(
120120
.binaryOperator("-"),
@@ -129,9 +129,11 @@ public enum AddBlocker: ExpressionMacro {
129129
)
130130

131131
return ExprSyntax(
132-
node.withOperatorOperand(
132+
node.with(
133+
\.operatorOperand,
133134
ExprSyntax(
134-
binOp.withOperatorToken(
135+
binOp.with(
136+
\.operatorToken,
135137
binOp.operatorToken.withKind(.binaryOperator("-"))
136138
)
137139
)

0 commit comments

Comments
 (0)