Skip to content

Commit b67e899

Browse files
committed
[SyntaxRewriter] Iterate RawSyntaxChildren using pattern matching
Simpify the iteration code. Also, stop counting 'childIndex' as it can be retrieved from the absolute info.
1 parent e2d4423 commit b67e899

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxRewriterFile.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
318318
// with 'Syntax'
319319
var rewrittens: ContiguousArray<RetainedSyntaxArena> = []
320320
321-
// Incrementing i manually is faster than using .enumerated()
322-
var childIndex = 0
323-
for (raw, info) in RawSyntaxChildren(node) {
324-
defer { childIndex += 1 }
325-
326-
guard let child = raw, viewMode.shouldTraverse(node: child) else {
327-
// Node does not exist or should not be visited.
328-
continue
329-
}
321+
for case let (child?, info) in RawSyntaxChildren(node) where viewMode.shouldTraverse(node: child) {
330322
331323
// Build the Syntax node to rewrite
332324
var childNode = nodeFactory.create(parent: node, raw: child, absoluteInfo: info)
@@ -343,7 +335,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
343335
}
344336
345337
// Update the rewritten child.
346-
newLayout[childIndex] = childNode.raw
338+
newLayout[Int(info.indexInParent)] = childNode.raw
347339
// Retain the syntax arena of the new node until it's wrapped with Syntax node.
348340
rewrittens.append(childNode.raw.arenaReference.retained)
349341
}

Sources/SwiftSyntax/generated/SyntaxRewriter.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3915,17 +3915,7 @@ open class SyntaxRewriter {
39153915
// with 'Syntax'
39163916
var rewrittens: ContiguousArray<RetainedSyntaxArena> = []
39173917

3918-
// Incrementing i manually is faster than using .enumerated()
3919-
var childIndex = 0
3920-
for (raw, info) in RawSyntaxChildren(node) {
3921-
defer {
3922-
childIndex += 1
3923-
}
3924-
3925-
guard let child = raw, viewMode.shouldTraverse(node: child) else {
3926-
// Node does not exist or should not be visited.
3927-
continue
3928-
}
3918+
for case let (child?, info) in RawSyntaxChildren(node) where viewMode.shouldTraverse(node: child) {
39293919

39303920
// Build the Syntax node to rewrite
39313921
var childNode = nodeFactory.create(parent: node, raw: child, absoluteInfo: info)
@@ -3942,7 +3932,7 @@ open class SyntaxRewriter {
39423932
}
39433933

39443934
// Update the rewritten child.
3945-
newLayout[childIndex] = childNode.raw
3935+
newLayout[Int(info.indexInParent)] = childNode.raw
39463936
// Retain the syntax arena of the new node until it's wrapped with Syntax node.
39473937
rewrittens.append(childNode.raw.arenaReference.retained)
39483938
}

0 commit comments

Comments
 (0)