From 55ef7789f5630925b47beb87fb2d18e06261d33a Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Fri, 17 Jul 2020 23:07:29 -0400 Subject: [PATCH] DefNode: Optimize by avoiding intermediate array --- lib/rubocop/ast/node/def_node.rb | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/lib/rubocop/ast/node/def_node.rb b/lib/rubocop/ast/node/def_node.rb index 7e37047d7..4769acc6d 100644 --- a/lib/rubocop/ast/node/def_node.rb +++ b/lib/rubocop/ast/node/def_node.rb @@ -31,14 +31,14 @@ def argument_forwarding? # # @return [Symbol] the name of the defined method def method_name - node_parts[2] + children[-3] end # An array containing the arguments of the method definition. # # @return [Array] the arguments of the method definition def arguments - node_parts[1] + children[-2] end # The body of the method definition. @@ -49,33 +49,14 @@ def arguments # # @return [Node] the body of the method definition def body - node_parts[0] + children[-1] end # The receiver of the method definition, if any. # # @return [Node, nil] the receiver of the method definition, or `nil`. def receiver - node_parts[3] - end - - # Custom destructuring method. This can be used to normalize - # destructuring for different variations of the node. - # - # In this case, the `def` node destructures into: - # - # `method_name, arguments, body` - # - # while the `defs` node destructures into: - # - # `receiver, method_name, arguments, body` - # - # so we reverse the destructured array to get the optional receiver - # at the end, where it can be discarded. - # - # @return [Array] the different parts of the `def` or `defs` node - def node_parts - to_a.reverse + children[-4] end end end