Skip to content

Commit 4ea7f87

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 f6770f6 commit 4ea7f87

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

Sources/SwiftSyntax/SyntaxNodes.swift.gyb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ 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+
/// This property is exposed for testing purposes only.
323+
var _asConcreteType: Any {
324+
switch self.asSyntaxEnum {
325+
case .token(let node):
326+
return node
327+
case .unknown(let node):
328+
return node
329+
% for node in SYNTAX_NODES:
330+
case .${node.swift_syntax_kind}(let node):
331+
return node
317332
% end
318333
}
319334
}
@@ -323,7 +338,7 @@ extension Syntax: CustomReflectable {
323338
/// Reconstructs the real syntax type for this type from the node's kind and
324339
/// provides a mirror that reflects this type.
325340
public var customMirror: Mirror {
326-
return Mirror(reflecting: self.asConcreteType)
341+
return Mirror(reflecting: self._asConcreteType)
327342
}
328343
}
329344

@@ -333,7 +348,7 @@ extension ${node.name}: CustomReflectable {
333348
/// Reconstructs the real syntax type for this type from the node's kind and
334349
/// provides a mirror that reflects this type.
335350
public var customMirror: Mirror {
336-
return Mirror(reflecting: Syntax(self).asConcreteType)
351+
return Mirror(reflecting: Syntax(self)._asConcreteType)
337352
}
338353
}
339354
% elif node.is_syntax_collection():
@@ -344,9 +359,9 @@ extension ${node.name}: CustomReflectable {
344359
return Mirror(self, children: [
345360
% for child in node.children:
346361
% if child.is_optional:
347-
"${child.swift_name}": ${child.swift_name}.map(Syntax.init)?.asConcreteType as Any,
362+
"${child.swift_name}": ${child.swift_name}.map(Syntax.init)?._asConcreteType as Any,
348363
% else:
349-
"${child.swift_name}": Syntax(${child.swift_name}).asConcreteType,
364+
"${child.swift_name}": Syntax(${child.swift_name})._asConcreteType,
350365
% end
351366
% end
352367
])

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)