-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SwiftSyntax] Add SyntaxRewriter.visitAny(_:) #15212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@swift-ci please smoke test |
hmm, this will make |
Ah, yes you're right - this would make it so Something like |
sounds good! |
09db2e5
to
e8b0bdb
Compare
@swift-ci please smoke test |
|
||
// Visit this node using the global visitor first | ||
let newNode = visitAny(node) | ||
switch newNode.raw.kind { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this two-phase transformation may complicate clients' codebase. Can we force users to choose either using visitAny
or visit(_ node: SpecializedSyntax)
? meaning if visitAny
changed a node, we don't call visit(_ node: SpecializedSyntax)
any more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, I'll do that!
dccecde
to
91a27ea
Compare
// If the global visitor returned non-nil, visit the rewritten node's | ||
// children and skip specialized dispatch. | ||
if let newNode = visitAny(node) { | ||
return visitChildren(newNode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, sorry about being a little nit-picky here. Whether visitingChildren
should be up to the overriding function, right? like other specialized visit functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes you're right. Thanks for catching that!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The more I think about it, though, the more I wonder if we should always call visitChildren after the specialized visitor has been called.
For example, if I'm writing a transform that, say, lays out function call arguments a certain way, I shouldn't have to manually iterate over all subexpressions and visit them, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, i think it is a valid assumption that in most cases users will visit a node's children even though the node itself has been updated. @rintaro what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, we should separate Visitor
and Walker
like ASTVisitor
/ ASTWalker
.
91a27ea
to
1dcf5f4
Compare
@swift-ci please smoke test and merge |
@swift-ci please smoke test |
42d96ae
to
48ce47f
Compare
@swift-ci please smoke test |
@swift-ci please smoke test |
1 similar comment
@swift-ci please smoke test |
@swift-ci please smoke test |
* [SwiftSyntax] Add SyntaxRewriter.visitAny(_:) This function, when overridden, allows Syntax rewriters to perform custom dynamic visitation behavior. If a user wanted to, say, store a series of transformations accessible by metatype, they can override visitAny and do their own runtime dispatch. If a non-nil result is returned from visitAny, the original specialized visitors are skipped.
This patch makes it so clients can override
visit(_ node: Syntax) -> Syntax
inSyntaxRewriter
andSyntaxVisitor
subclasses, if they choose to override the global visitation behavior.