Sha256: e309bbc4576fc26c5d7a694ed137871158477c0963a1d7278d0e3a4b0c45d34e

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `kwbegin` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `kwbegin` nodes within RuboCop.
    class KeywordBeginNode < Node
      # Returns the body of the `kwbegin` block. Returns `self` if the `kwbegin` contains
      # multiple nodes.
      #
      # @return [Node, nil] The body of the `kwbegin`.
      def body
        return unless node_parts.any?

        if rescue_node
          rescue_node.body
        elsif ensure_node
          ensure_node.node_parts[0]
        elsif node_parts.one?
          node_parts[0]
        else
          self
        end
      end

      # Returns the `rescue` node of the `kwbegin` block, if one is present.
      #
      # @return [Node, nil] The `rescue` node within `kwbegin`.
      def ensure_node
        node_parts[0] if node_parts[0]&.ensure_type?
      end

      # Returns the `rescue` node of the `kwbegin` block, if one is present.
      #
      # @return [Node, nil] The `rescue` node within `kwbegin`.
      def rescue_node
        return ensure_node&.rescue_node if ensure_node&.rescue_node

        node_parts[0] if node_parts[0]&.rescue_type?
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-ast-1.36.2 lib/rubocop/ast/node/keyword_begin_node.rb
rubocop-ast-1.36.1 lib/rubocop/ast/node/keyword_begin_node.rb
rubocop-ast-1.36.0 lib/rubocop/ast/node/keyword_begin_node.rb
rubocop-ast-1.35.0 lib/rubocop/ast/node/keyword_begin_node.rb