Skip to content

Commit 36e5608

Browse files
committed
Return single ResultType rather than an Array of transformed children RestultTypes.
Rather than the default implemenation returning all children flatted together into an Array, the default implementation now just hits a fatalError.
1 parent 1a3599c commit 36e5608

File tree

3 files changed

+843
-1893
lines changed

3 files changed

+843
-1893
lines changed

Sources/SwiftSyntax/SyntaxTransform.swift.gyb

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,35 @@
2020
public protocol SyntaxTransformVisitor {
2121
associatedtype ResultType = Void
2222

23-
func visit(_ token: TokenSyntax) -> [ResultType]
24-
func visit(_ node: UnknownSyntax) -> [ResultType]
23+
func visit(_ token: TokenSyntax) -> ResultType
24+
func visit(_ node: UnknownSyntax) -> ResultType
2525

2626
% for node in SYNTAX_NODES:
2727
% if is_visitable(node):
2828
/// Visiting `${node.name}` specifically.
2929
/// - Parameter node: the node we are visiting.
3030
/// - Returns: the sum of whatever the child visitors return.
31-
func visit(_ node: ${node.name}) -> [ResultType]
31+
func visit(_ node: ${node.name}) -> ResultType
3232
% end
3333
% end
3434
}
3535

3636
extension SyntaxTransformVisitor {
37-
public func visit(_ token: TokenSyntax) -> [ResultType] { [] }
38-
public func visit(_ node: UnknownSyntax) -> [ResultType] { [] }
37+
public func visit(_ token: TokenSyntax) -> ResultType { fatalError("Not implemented.") }
38+
public func visit(_ node: UnknownSyntax) -> ResultType { fatalError("Not implemented.") }
3939

4040
% for node in SYNTAX_NODES:
4141
% if is_visitable(node):
4242
/// Visiting `${node.name}` specifically.
4343
/// - Parameter node: the node we are visiting.
4444
/// - Returns: nil by default.
45-
public func visit(_ node: ${node.name}) -> [ResultType] {
46-
% if node.is_base():
47-
// Avoid calling into visitChildren if possible.
48-
if !node.raw.layoutView!.children.isEmpty {
49-
return visitChildren(node)
50-
}
51-
return []
52-
% else:
53-
// Avoid calling into visitChildren if possible.
54-
if !node.raw.layoutView!.children.isEmpty {
55-
return visitChildren(node)
56-
}
57-
return []
58-
% end
45+
public func visit(_ node: ${node.name}) -> ResultType {
46+
fatalError("Not implemented.")
5947
}
6048
% end
6149
% end
6250

63-
public func visit(_ data: Syntax) -> [ResultType] {
51+
public func visit(_ data: Syntax) -> ResultType {
6452
switch data.raw.kind {
6553
case .token:
6654
let node = TokenSyntax(data)!
@@ -80,7 +68,7 @@ extension SyntaxTransformVisitor {
8068
}
8169
}
8270

83-
public func visit(_ data: ExprSyntax) -> [ResultType] {
71+
public func visit(_ data: ExprSyntax) -> ResultType {
8472
switch data.raw.kind {
8573
% for node in NON_BASE_SYNTAX_NODES:
8674
% if node.base_kind == "Expr":
@@ -94,7 +82,7 @@ extension SyntaxTransformVisitor {
9482
}
9583
}
9684

97-
public func visit(_ data: PatternSyntax) -> [ResultType] {
85+
public func visit(_ data: PatternSyntax) -> ResultType {
9886
switch data.raw.kind {
9987
% for node in NON_BASE_SYNTAX_NODES:
10088
% if node.base_kind == "Pattern":
@@ -108,7 +96,7 @@ extension SyntaxTransformVisitor {
10896
}
10997
}
11098

111-
public func visit(_ data: TypeSyntax) -> [ResultType] {
99+
public func visit(_ data: TypeSyntax) -> ResultType {
112100
switch data.raw.kind {
113101
% for node in NON_BASE_SYNTAX_NODES:
114102
% if node.base_kind == "Type":
@@ -124,7 +112,7 @@ extension SyntaxTransformVisitor {
124112

125113
public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] {
126114
let syntaxNode = Syntax(node)
127-
return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).flatMap { rawChild in
115+
return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).map { rawChild in
128116
let child = Syntax(SyntaxData(rawChild, parent: syntaxNode))
129117
return visit(child)
130118
}

0 commit comments

Comments
 (0)