Skip to content

Commit

Permalink
DefNode: Optimize by avoiding intermediate array
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Jul 18, 2020
1 parent 99e488f commit 55ef778
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions lib/rubocop/ast/node/def_node.rb
Expand Up @@ -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<Node>] the arguments of the method definition
def arguments
node_parts[1]
children[-2]
end

# The body of the method definition.
Expand All @@ -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
Expand Down

0 comments on commit 55ef778

Please sign in to comment.