Skip to content

Commit cbdaa25

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

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func evaluateMacro(
202202
return -1
203203
}
204204

205-
macroName = parentExpansion.macro.withoutTrivia().description
205+
macroName = parentExpansion.macro.text
206206
evaluatedSyntax = Syntax(try exprMacro.expansion(of: parentExpansion, in: &context))
207207

208208
// Handle expression macro. The resulting decls are wrapped in a `CodeBlockItemListSyntax`.
@@ -212,7 +212,7 @@ func evaluateMacro(
212212
return -1
213213
}
214214
let decls = try declMacro.expansion(of: parentExpansion, in: &context)
215-
macroName = parentExpansion.macro.withoutTrivia().description
215+
macroName = parentExpansion.macro.text
216216
evaluatedSyntax = Syntax(CodeBlockItemListSyntax(
217217
decls.map { CodeBlockItemSyntax(item: .decl($0)) }))
218218

@@ -241,7 +241,7 @@ func evaluateMacro(
241241
)
242242
}
243243

244-
var evaluatedSyntaxStr = evaluatedSyntax.withoutTrivia().description
244+
var evaluatedSyntaxStr = evaluatedSyntax.trimmedDescription
245245
evaluatedSyntaxStr.withUTF8 { utf8 in
246246
let evaluatedResultPtr = UnsafeMutablePointer<UInt8>.allocate(capacity: utf8.count + 1)
247247
if let baseAddress = utf8.baseAddress {
@@ -357,7 +357,7 @@ func expandAttachedMacro(
357357

358358
// Form a buffer of accessor declarations to return to the caller.
359359
evaluatedSyntaxStr = accessors.map {
360-
$0.withoutTrivia().description
360+
$0.trimmedDescription
361361
}.joined(separator: "\n\n")
362362

363363
case (let attachedMacro as MemberAttributeMacro.Type, .MemberAttribute):
@@ -380,7 +380,7 @@ func expandAttachedMacro(
380380

381381
// Form a buffer containing an attribute list to return to the caller.
382382
evaluatedSyntaxStr = attributes.map {
383-
$0.withoutTrivia().description
383+
$0.trimmedDescription
384384
}.joined(separator: " ")
385385

386386
case (let attachedMacro as MemberDeclarationMacro.Type, .SynthesizedMembers):
@@ -392,7 +392,7 @@ func expandAttachedMacro(
392392

393393
// Form a buffer of member declarations to return to the caller.
394394
evaluatedSyntaxStr = members.map {
395-
$0.withoutTrivia().description
395+
$0.trimmedDescription
396396
}.joined(separator: "\n\n")
397397

398398
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)