[ruby/prism] Move Node#type and Node::type documentation

https://github.com/ruby/prism/commit/08a71f6259
This commit is contained in:
Kevin Newton 2024-07-03 08:50:22 -04:00
Родитель 32090e2b8d
Коммит 39dcfe26ee
3 изменённых файлов: 24 добавлений и 27 удалений

Просмотреть файл

@ -82,6 +82,7 @@ Gem::Specification.new do |spec|
"lib/prism/pack.rb", "lib/prism/pack.rb",
"lib/prism/parse_result.rb", "lib/prism/parse_result.rb",
"lib/prism/parse_result/comments.rb", "lib/prism/parse_result/comments.rb",
"lib/prism/parse_result/errors.rb",
"lib/prism/parse_result/newlines.rb", "lib/prism/parse_result/newlines.rb",
"lib/prism/pattern.rb", "lib/prism/pattern.rb",
"lib/prism/polyfill/byteindex.rb", "lib/prism/polyfill/byteindex.rb",

Просмотреть файл

@ -176,18 +176,31 @@ module Prism
raise NoMethodError, "undefined method `comment_targets' for #{inspect}" raise NoMethodError, "undefined method `comment_targets' for #{inspect}"
end end
# Returns a symbol symbolizing the type of node that this represents. This
# is particularly useful for case statements and array comparisons.
def type
raise NoMethodError, "undefined method `type' for #{inspect}"
end
# Returns a string representation of the node. # Returns a string representation of the node.
def inspect def inspect
raise NoMethodError, "undefined method `inspect' for #{inspect}" raise NoMethodError, "undefined method `inspect' for #{inspect}"
end end
# Returns the type of the node as a symbol. # Sometimes you want to check an instance of a node against a list of
# classes to see what kind of behavior to perform. Usually this is done by
# calling `[cls1, cls2].include?(node.class)` or putting the node into a
# case statement and doing `case node; when cls1; when cls2; end`. Both of
# these approaches are relatively slow because of the constant lookups,
# method calls, and/or array allocations.
#
# Instead, you can call #type, which will return to you a symbol that you
# can use for comparison. This is faster than the other approaches because
# it uses a single integer comparison, but also because if you're on CRuby
# you can take advantage of the fact that case statements with all symbol
# keys will use a jump table.
def type
raise NoMethodError, "undefined method `type' for #{inspect}"
end
# Similar to #type, this method returns a symbol that you can use for
# splitting on the type of the node without having to do a long === chain.
# Note that like #type, it will still be slower than using == for a single
# class, but should be faster in a case statement or an array comparison.
def self.type def self.type
raise NoMethodError, "undefined method `type' for #{inspect}" raise NoMethodError, "undefined method `type' for #{inspect}"
end end
@ -340,30 +353,12 @@ module Prism
InspectVisitor.compose(self) InspectVisitor.compose(self)
end end
# Sometimes you want to check an instance of a node against a list of # Return a symbol representation of this node type. See `Node#type`.
# classes to see what kind of behavior to perform. Usually this is done by
# calling `[cls1, cls2].include?(node.class)` or putting the node into a
# case statement and doing `case node; when cls1; when cls2; end`. Both of
# these approaches are relatively slow because of the constant lookups,
# method calls, and/or array allocations.
#
# Instead, you can call #type, which will return to you a symbol that you
# can use for comparison. This is faster than the other approaches because
# it uses a single integer comparison, but also because if you're on CRuby
# you can take advantage of the fact that case statements with all symbol
# keys will use a jump table.
#
# def type: () -> Symbol
def type def type
:<%= node.human %> :<%= node.human %>
end end
# Similar to #type, this method returns a symbol that you can use for # Return a symbol representation of this node type. See `Node::type`.
# splitting on the type of the node without having to do a long === chain.
# Note that like #type, it will still be slower than using == for a single
# class, but should be faster in a case statement or an array comparison.
#
# def self.type: () -> Symbol
def self.type def self.type
:<%= node.human %> :<%= node.human %>
end end

Просмотреть файл

@ -14,6 +14,7 @@ module Prism
unescape_test.rb unescape_test.rb
encoding/regular_expression_encoding_test.rb encoding/regular_expression_encoding_test.rb
encoding/string_encoding_test.rb encoding/string_encoding_test.rb
result/breadth_first_search_test.rb
result/static_literals_test.rb result/static_literals_test.rb
result/warnings_test.rb result/warnings_test.rb
ruby/parser_test.rb ruby/parser_test.rb