Skip to content

Commit 8f96877

Browse files
committed
Provide abstract methods in Prism::Node
To make typechecking easier.
1 parent 63b1e7d commit 8f96877

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

rbi/prism_static.rbi

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ class Prism::ParseWarning
3232
end
3333

3434
class Prism::Node
35-
sig { returns(T::Array[T.nilable(Prism::Node)]) }
36-
def child_nodes; end
37-
3835
sig { returns(Prism::Location) }
3936
def location; end
4037

@@ -43,6 +40,21 @@ class Prism::Node
4340

4441
sig { returns(String) }
4542
def to_dot; end
43+
44+
sig { params(visitor: Prism::Visitor).void }
45+
def accept(visitor); end
46+
47+
sig { returns(T::Array[T.nilable(Prism::Node)]) }
48+
def child_nodes; end
49+
50+
sig { returns(T::Array[Prism::Node]) }
51+
def compact_child_nodes; end
52+
53+
sig { returns(T::Array[T.nilable(Prism::Node)]) }
54+
def deconstruct; end
55+
56+
sig { returns(Symbol) }
57+
def type; end
4658
end
4759

4860
class Prism::Comment

templates/lib/prism/node.rb.erb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,44 @@ module Prism
3636
def to_dot
3737
DotVisitor.new.tap { |visitor| accept(visitor) }.to_dot
3838
end
39+
40+
# --------------------------------------------------------------------------
41+
# :section: Node interface
42+
# These methods are effectively abstract methods that must be implemented by
43+
# the various subclasses of Node. They are here to make it easier to work
44+
# with typecheckers.
45+
# --------------------------------------------------------------------------
46+
47+
# Accepts a visitor and calls back into the specialized visit function.
48+
def accept(visitor)
49+
raise NoMethodError, "undefined method `accept' for #{inspect}"
50+
end
51+
52+
# Returns an array of child nodes, including `nil`s in the place of optional
53+
# nodes that were not present.
54+
def child_nodes
55+
raise NoMethodError, "undefined method `#{__method__}' for #{inspect}"
56+
end
57+
58+
alias deconstruct child_nodes
59+
60+
# Returns an array of child nodes, excluding any `nil`s in the place of
61+
# optional nodes that were not present.
62+
def compact_child_nodes
63+
raise NoMethodError, "undefined method `compact_child_nodes' for #{inspect}"
64+
end
65+
66+
# Returns an array of child nodes and locations that could potentially have
67+
# comments attached to them.
68+
def comment_targets
69+
raise NoMethodError, "undefined method `comment_targets' for #{inspect}"
70+
end
71+
72+
# Returns a symbol symbolizing the type of node that this represents. This
73+
# is particularly useful for case statements and array comparisons.
74+
def type
75+
raise NoMethodError, "undefined method `type' for #{inspect}"
76+
end
3977
end
4078
<%- nodes.each do |node| -%>
4179

0 commit comments

Comments
 (0)