diff options
author | Kevin Newton <[email protected]> | 2024-07-03 08:50:22 -0400 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2024-07-11 14:25:54 -0400 |
commit | 39dcfe26ee55919c3277a59884592a0ebe0aee6a (patch) | |
tree | 89e51a06d5c852deb27966335a7adde760347cb4 | |
parent | 32090e2b8d4bd1c0ddfd6b4ee82927e935534eed (diff) |
[ruby/prism] Move Node#type and Node::type documentation
https://github.com/ruby/prism/commit/08a71f6259
-rw-r--r-- | lib/prism/prism.gemspec | 1 | ||||
-rw-r--r-- | prism/templates/lib/prism/node.rb.erb | 49 | ||||
-rw-r--r-- | test/prism/newline_test.rb | 1 |
3 files changed, 24 insertions, 27 deletions
diff --git a/lib/prism/prism.gemspec b/lib/prism/prism.gemspec index b39ba706dc..750af194a8 100644 --- a/lib/prism/prism.gemspec +++ b/lib/prism/prism.gemspec @@ -82,6 +82,7 @@ Gem::Specification.new do |spec| "lib/prism/pack.rb", "lib/prism/parse_result.rb", "lib/prism/parse_result/comments.rb", + "lib/prism/parse_result/errors.rb", "lib/prism/parse_result/newlines.rb", "lib/prism/pattern.rb", "lib/prism/polyfill/byteindex.rb", diff --git a/prism/templates/lib/prism/node.rb.erb b/prism/templates/lib/prism/node.rb.erb index 66cbce767e..8b6c54ef00 100644 --- a/prism/templates/lib/prism/node.rb.erb +++ b/prism/templates/lib/prism/node.rb.erb @@ -176,18 +176,31 @@ module Prism raise NoMethodError, "undefined method `comment_targets' for #{inspect}" 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. def inspect raise NoMethodError, "undefined method `inspect' for #{inspect}" 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 raise NoMethodError, "undefined method `type' for #{inspect}" end @@ -340,30 +353,12 @@ module Prism InspectVisitor.compose(self) end - # 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: () -> Symbol + # Return a symbol representation of this node type. See `Node#type`. def type :<%= node.human %> 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: () -> Symbol + # Return a symbol representation of this node type. See `Node::type`. def self.type :<%= node.human %> end diff --git a/test/prism/newline_test.rb b/test/prism/newline_test.rb index a244b5428a..fefe9def91 100644 --- a/test/prism/newline_test.rb +++ b/test/prism/newline_test.rb @@ -14,6 +14,7 @@ module Prism unescape_test.rb encoding/regular_expression_encoding_test.rb encoding/string_encoding_test.rb + result/breadth_first_search_test.rb result/static_literals_test.rb result/warnings_test.rb ruby/parser_test.rb |