Skip to content

Remove SyntaxProtocol.syntaxNodeType requirement #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Sources/SwiftSyntax/Misc.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ extension Syntax {
}
}
}

extension SyntaxKind {
var syntaxNodeType: SyntaxProtocol.Type {
switch self {
case .token: return TokenSyntax.self
case .unknown: return UnknownSyntax.self
% for node in NON_BASE_SYNTAX_NODES:
case .${node.swift_syntax_kind}: return ${node.name}.self
% end
}
}
}
11 changes: 4 additions & 7 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ extension Syntax {
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
return S.init(self)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self.asProtocol(SyntaxProtocol.self))
}
}

extension Syntax: CustomReflectable {
Expand Down Expand Up @@ -113,9 +109,6 @@ public protocol SyntaxProtocol: CustomStringConvertible,
/// Converts the given `Syntax` node to this type. Returns `nil` if the
/// conversion is not possible.
init?(_ syntaxNode: Syntax)

/// Returns the underlying syntax node type.
var syntaxNodeType: SyntaxProtocol.Type { get }
}

extension SyntaxProtocol {
Expand All @@ -128,6 +121,10 @@ extension SyntaxProtocol {
public var raw: RawSyntax {
return _syntaxNode.data.raw
}

public var syntaxNodeType: SyntaxProtocol.Type {
return self.raw.kind.syntaxNodeType
}
}

public extension SyntaxProtocol {
Expand Down
4 changes: 0 additions & 4 deletions Sources/SwiftSyntax/SyntaxBaseNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ public struct ${node.name}: ${node.name}Protocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func `is`<S: ${node.name}Protocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
Expand Down
4 changes: 0 additions & 4 deletions Sources/SwiftSyntax/SyntaxCollections.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
self.init(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}

/// The number of elements, `present` or `missing`, in this collection.
public var count: Int { return raw.layoutView!.children.count }

Expand Down
4 changes: 0 additions & 4 deletions Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
let data = SyntaxData.forRoot(raw)
self.init(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}
% for (idx, child) in enumerate(node.children):
% child_node = NODE_MAP.get(child.syntax_kind)
%
Expand Down
8 changes: 0 additions & 8 deletions Sources/SwiftSyntax/SyntaxOtherNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public struct UnknownSyntax: SyntaxProtocol, SyntaxHashable {
self._syntaxNode = syntax
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}

/// Creates an `UnknownSyntax` node from the given `SyntaxData`. This assumes
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
/// is undefined.
Expand Down Expand Up @@ -93,10 +89,6 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
self.init(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}

public var presence: SourcePresence {
return tokenView.presence
}
Expand Down
272 changes: 272 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/Misc.swift

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func `is`<S: DeclSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
Expand Down Expand Up @@ -183,10 +179,6 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func `is`<S: ExprSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
Expand Down Expand Up @@ -286,10 +278,6 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func `is`<S: StmtSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
Expand Down Expand Up @@ -389,10 +377,6 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func `is`<S: TypeSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
Expand Down Expand Up @@ -492,10 +476,6 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func `is`<S: PatternSyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
Expand Down
Loading