8000 [ruby/prism] Provide abstract methods in Prism::Node · mikhailnov/ruby@7b6731b · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b6731b

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Provide abstract methods in Prism::Node
To make typechecking easier. ruby/prism@8f96877d7a
1 parent 8cbba87 commit 7b6731b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

prism/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)
0