File: rescue_node.rb

package info (click to toggle)
ruby-rubocop-ast 1.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,256 kB
  • sloc: ruby: 15,071; yacc: 90; makefile: 9
file content (49 lines) | stat: -rw-r--r-- 1,598 bytes parent folder | download
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