Skip to content

Commit 6f03210

Browse files
committed
Add a property that returns a Syntax node as its concrete type
Otherwise it requires a lot of boilerplate to return the Syntax node's type in the lit-test-helper.
1 parent f67e771 commit 6f03210

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Sources/SwiftSyntax/SyntaxNodes.swift.gyb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,20 @@ public extension Syntax {
314314
% else:
315315
return .${node.swift_syntax_kind}(${node.name}(self)!)
316316
% end
317+
% end
318+
}
319+
}
320+
321+
/// Retrieve the concretely typed node that this Syntax node wraps.
322+
var asConcreteType: Any {
323+
switch self.asSyntaxEnum {
324+
case .token(let node):
325+
return node
326+
case .unknown(let node):
327+
return node
328+
% for node in SYNTAX_NODES:
329+
case .${node.swift_syntax_kind}(let node):
330+
return node
317331
% end
318332
}
319333
}

Sources/lit-test-helper/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,11 @@ func performRoundtrip(args: CommandLineArguments) throws {
399399
struct NodePrinter: SyntaxAnyVisitor {
400400
func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
401401
assert(!node.isUnknown)
402-
print("<\(type(of: node))>", terminator: "")
402+
print("<\(type(of: node.asConcreteType))>", terminator: "")
403403
return .visitChildren
404404
}
405405
func visitAnyPost(_ node: Syntax) {
406-
print("</\(type(of: node))>", terminator: "")
406+
print("</\(type(of: node.asConcreteType))>", terminator: "")
407407
}
408408
func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
409409
print("<\(type(of: token))>", terminator: "")

0 commit comments

Comments
 (0)