Skip to content

Commit dac918f

Browse files
authored
SwiftSyntax: add pre and post visit function so that client can override a general visiting logic. SR-6902 (#14365)
1 parent 54355ae commit dac918f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tools/SwiftSyntax/SyntaxRewriter.swift.gyb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ open class SyntaxRewriter {
4040
open func visit(_ token: TokenSyntax) -> Syntax {
4141
return token
4242
}
43+
44+
/// The function called before visiting the node and its descendents.
45+
/// - node: the node we are about to visit.
46+
open func visitPre(_ node: Syntax) {}
47+
48+
/// The function called after visting the node and its descendents.
49+
/// - node: the node we just finished visiting.
50+
open func visitPost(_ node: Syntax) {}
51+
4352
public func visit(_ node: Syntax) -> Syntax {
53+
visitPre(node)
54+
defer { visitPost(node) }
4455
switch node.raw.kind {
4556
case .token: return visit(node as! TokenSyntax)
4657
% for node in SYNTAX_NODES:
@@ -70,7 +81,17 @@ open class SyntaxVisitor {
7081

7182
open func visit(_ token: TokenSyntax) {}
7283

84+
/// The function called before visiting the node and its descendents.
85+
/// - node: the node we are about to visit.
86+
open func visitPre(_ node: Syntax) {}
87+
88+
/// The function called after visting the node and its descendents.
89+
/// - node: the node we just finished visiting.
90+
open func visitPost(_ node: Syntax) {}
91+
7392
public func visit(_ node: Syntax) {
93+
visitPre(node)
94+
defer { visitPost(node) }
7495
switch node.raw.kind {
7596
case .token: visit(node as! TokenSyntax)
7697
% for node in SYNTAX_NODES:

0 commit comments

Comments
 (0)