Skip to content

Commit bb99500

Browse files
authored
Merge pull request #683 from rintaro/syntaxnodetype-simple
2 parents 1901568 + 07d0200 commit bb99500

15 files changed

+288
-1099
lines changed

Sources/SwiftSyntax/Misc.swift.gyb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,15 @@ extension Syntax {
6161
}
6262
}
6363
}
64+
65+
extension SyntaxKind {
66+
var syntaxNodeType: SyntaxProtocol.Type {
67+
switch self {
68+
case .token: return TokenSyntax.self
69+
case .unknown: return UnknownSyntax.self
70+
% for node in NON_BASE_SYNTAX_NODES:
71+
case .${node.swift_syntax_kind}: return ${node.name}.self
72+
% end
73+
}
74+
}
75+
}

Sources/SwiftSyntax/Syntax.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ extension Syntax {
6767
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
6868
return S.init(self)
6969
}
70-
71-
public var syntaxNodeType: SyntaxProtocol.Type {
72-
return Swift.type(of: self.asProtocol(SyntaxProtocol.self))
73-
}
7470
}
7571

7672
extension Syntax: CustomReflectable {
@@ -113,9 +109,6 @@ public protocol SyntaxProtocol: CustomStringConvertible,
113109
/// Converts the given `Syntax` node to this type. Returns `nil` if the
114110
/// conversion is not possible.
115111
init?(_ syntaxNode: Syntax)
116-
117-
/// Returns the underlying syntax node type.
118-
var syntaxNodeType: SyntaxProtocol.Type { get }
119112
}
120113

121114
extension SyntaxProtocol {
@@ -128,6 +121,10 @@ extension SyntaxProtocol {
128121
public var raw: RawSyntax {
129122
return _syntaxNode.data.raw
130123
}
124+
125+
public var syntaxNodeType: SyntaxProtocol.Type {
126+
return self.raw.kind.syntaxNodeType
127+
}
131128
}
132129

133130
public extension SyntaxProtocol {

Sources/SwiftSyntax/SyntaxBaseNodes.swift.gyb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ public struct ${node.name}: ${node.name}Protocol, SyntaxHashable {
9999
self._syntaxNode = Syntax(data)
100100
}
101101

102-
public var syntaxNodeType: SyntaxProtocol.Type {
103-
return _syntaxNode.syntaxNodeType
104-
}
105-
106102
public func `is`<S: ${node.name}Protocol>(_ syntaxType: S.Type) -> Bool {
107103
return self.as(syntaxType) != nil
108104
}

Sources/SwiftSyntax/SyntaxCollections.swift.gyb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
6666
self.init(data)
6767
}
6868

69-
public var syntaxNodeType: SyntaxProtocol.Type {
70-
return Swift.type(of: self)
71-
}
72-
7369
/// The number of elements, `present` or `missing`, in this collection.
7470
public var count: Int { return raw.layoutView!.children.count }
7571

Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
9595
let data = SyntaxData.forRoot(raw)
9696
self.init(data)
9797
}
98-
99-
public var syntaxNodeType: SyntaxProtocol.Type {
100-
return Swift.type(of: self)
101-
}
10298
% for (idx, child) in enumerate(node.children):
10399
% child_node = NODE_MAP.get(child.syntax_kind)
104100
%

Sources/SwiftSyntax/SyntaxOtherNodes.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ public struct UnknownSyntax: SyntaxProtocol, SyntaxHashable {
2525
self._syntaxNode = syntax
2626
}
2727

28-
public var syntaxNodeType: SyntaxProtocol.Type {
29-
return Swift.type(of: self)
30-
}
31-
3228
/// Creates an `UnknownSyntax` node from the given `SyntaxData`. This assumes
3329
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
3430
/// is undefined.
@@ -93,10 +89,6 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
9389
self.init(data)
9490
}
9591

96-
public var syntaxNodeType: SyntaxProtocol.Type {
97-
return Swift.type(of: self)
98-
}
99-
10092
public var presence: SourcePresence {
10193
return tokenView.presence
10294
}

Sources/SwiftSyntax/gyb_generated/Misc.swift

Lines changed: 272 additions & 0 deletions
Large diffs are not rendered by default.

Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
8080
self._syntaxNode = Syntax(data)
8181
}
8282

83-
public var syntaxNodeType: SyntaxProtocol.Type {
84-
return _syntaxNode.syntaxNodeType
85-
}
86-
8783
public func `is`<S: DeclSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
8884
return self.as(syntaxType) != nil
8985
}
@@ -183,10 +179,6 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
183179
self._syntaxNode = Syntax(data)
184180
}
185181

186-
public var syntaxNodeType: SyntaxProtocol.Type {
187-
return _syntaxNode.syntaxNodeType
188-
}
189-
190182
public func `is`<S: ExprSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
191183
return self.as(syntaxType) != nil
192184
}
@@ -286,10 +278,6 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
286278
self._syntaxNode = Syntax(data)
287279
}
288280

289-
public var syntaxNodeType: SyntaxProtocol.Type {
290-
return _syntaxNode.syntaxNodeType
291-
}
292-
293281
public func `is`<S: StmtSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
294282
return self.as(syntaxType) != nil
295283
}
@@ -389,10 +377,6 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
389377
self._syntaxNode = Syntax(data)
390378
}
391379

392-
public var syntaxNodeType: SyntaxProtocol.Type {
393-
return _syntaxNode.syntaxNodeType
394-
}
395-
396380
public func `is`<S: TypeSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
397381
return self.as(syntaxType) != nil
398382
}
@@ -492,10 +476,6 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
492476
self._syntaxNode = Syntax(data)
493477
}
494478

495-
public var syntaxNodeType: SyntaxProtocol.Type {
496-
return _syntaxNode.syntaxNodeType
497-
}
498-
499479
public func `is`<S: PatternSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
500480
return self.as(syntaxType) != nil
501481
}

0 commit comments

Comments
 (0)