Skip to content

Commit 681eacd

Browse files
committed
Return vititor results for all children (rather than the first child).
1 parent e6bb9cf commit 681eacd

File tree

3 files changed

+1352
-1349
lines changed

3 files changed

+1352
-1349
lines changed

Sources/SwiftSyntax/SyntaxVisitor.swift.gyb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,47 +145,47 @@ open class SyntaxVisitor {
145145
public protocol SyntaxTransformVisitor {
146146
associatedtype ResultType = Void
147147

148-
func visit(_ token: TokenSyntax) -> ResultType?
149-
func visit(_ node: UnknownSyntax) -> ResultType?
148+
func visit(_ token: TokenSyntax) -> [ResultType]
149+
func visit(_ node: UnknownSyntax) -> [ResultType]
150150

151151
% for node in SYNTAX_NODES:
152152
% if is_visitable(node):
153153
/// Visiting `${node.name}` specifically.
154154
/// - Parameter node: the node we are visiting.
155-
/// - Returns: nil by default.
156-
func visit(_ node: ${node.name}) -> ResultType?
155+
/// - Returns: the sum of whatever the child visitors return.
156+
func visit(_ node: ${node.name}) -> [ResultType]
157157
% end
158158
% end
159159
}
160160

161161
extension SyntaxTransformVisitor {
162-
public func visit(_ token: TokenSyntax) -> ResultType? { nil }
163-
public func visit(_ node: UnknownSyntax) -> ResultType? { nil }
162+
public func visit(_ token: TokenSyntax) -> [ResultType] { [] }
163+
public func visit(_ node: UnknownSyntax) -> [ResultType] { [] }
164164

165165
% for node in SYNTAX_NODES:
166166
% if is_visitable(node):
167167
/// Visiting `${node.name}` specifically.
168168
/// - Parameter node: the node we are visiting.
169169
/// - Returns: nil by default.
170-
public func visit(_ node: ${node.name}) -> ResultType? {
170+
public func visit(_ node: ${node.name}) -> [ResultType] {
171171
% if node.is_base():
172172
// Avoid calling into visitChildren if possible.
173173
if !node.raw.layoutView!.children.isEmpty {
174-
return visitChildren(node).first
174+
return visitChildren(node)
175175
}
176-
return nil
176+
return []
177177
% else:
178178
// Avoid calling into visitChildren if possible.
179179
if !node.raw.layoutView!.children.isEmpty {
180-
return visitChildren(node).first
180+
return visitChildren(node)
181181
}
182-
return nil
182+
return []
183183
% end
184184
}
185185
% end
186186
% end
187187

188-
public func visit(_ data: SyntaxData) -> ResultType? {
188+
public func visit(_ data: SyntaxData) -> [ResultType] {
189189
switch data.raw.kind {
190190
case .token:
191191
let node = TokenSyntax(data)
@@ -207,7 +207,7 @@ extension SyntaxTransformVisitor {
207207

208208
public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] {
209209
let syntaxNode = Syntax(node)
210-
return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).compactMap { childRaw in
210+
return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).flatMap { childRaw in
211211
let childData = SyntaxData(childRaw, parent: syntaxNode)
212212
return visit(childData)
213213
}

0 commit comments

Comments
 (0)