File: ensure_node.rb

package info (click to toggle)
ruby-rubocop-ast 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,768 kB
  • sloc: ruby: 17,017; yacc: 90; makefile: 9
file content (53 lines) | stat: -rw-r--r-- 1,713 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
50
51
52
53
# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `ensure` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `ensure` nodes within RuboCop.
    class EnsureNode < Node
      DEPRECATION_WARNING_LOCATION_CACHE = [] # rubocop:disable Style/MutableConstant
      private_constant :DEPRECATION_WARNING_LOCATION_CACHE

      # Returns the body of the `ensure` clause.
      #
      # @return [Node, nil] The body of the `ensure`.
      # @deprecated Use `EnsureNode#branch`
      def body
        first_caller = caller(1..1).first

        unless DEPRECATION_WARNING_LOCATION_CACHE.include?(first_caller)
          warn '`EnsureNode#body` is deprecated and will be changed in the next major version of ' \
               'rubocop-ast. Use `EnsureNode#branch` instead to get the body of the `ensure` branch.'
          warn "Called from:\n#{caller.join("\n")}\n\n"

          DEPRECATION_WARNING_LOCATION_CACHE << first_caller
        end

        branch
      end

      # Returns an the ensure branch in the exception handling statement.
      #
      # @return [Node, nil] the body of the ensure branch.
      def branch
        node_parts[1]
      end

      # Returns the `rescue` node of the `ensure`, if present.
      #
      # @return [Node, nil] The `rescue` node.
      def rescue_node
        node_parts[0] if node_parts[0].rescue_type?
      end

      # Checks whether this node body is a void context.
      # Always `true` for `ensure`.
      #
      # @return [true] whether the `ensure` node body is a void context
      def void_context?
        true
      end
    end
  end
end