1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
# frozen_string_literal: true
module RuboCop
module AST
# A node extension for `rescue` nodes. This will be used in place of a
# plain node when the builder constructs the AST, making its methods
# available to all `rescue` nodes within RuboCop.
class RescueNode < Node
# Returns the body of the rescue node.
#
# @return [Node, nil] The body of the rescue node.
def body
node_parts[0]
end
# Returns an array of all the rescue branches in the exception handling statement.
#
# @return [Array<ResbodyNode>] an array of `resbody` nodes
def resbody_branches
node_parts[1...-1]
end
# Returns an array of all the rescue branches in the exception handling statement.
#
# @return [Array<Node, nil>] an array of the bodies of the rescue branches
# and the else (if any). Note that these bodies could be nil.
def branches
bodies = resbody_branches.map(&:body)
bodies.push(else_branch) if else?
bodies
end
# Returns the else branch of the exception handling statement, if any.
#
# @return [Node] the else branch node of the exception handling statement
# @return [nil] if the exception handling statement does not have an else branch.
def else_branch
node_parts[-1]
end
# Checks whether this exception handling statement has an `else` branch.
#
# @return [Boolean] whether the exception handling statement has an `else` branch
def else?
loc.else
end
end
end
end
|