Sha256: 07914408d4e7d36cad0fa7bcbb986c2134ec4f6441041c84a86b75bad0005de7

Contents?: true

Size: 1.98 KB

Versions: 160

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `case_match` nodes. This will be used in place of
    # a plain node when the builder constructs the AST, making its methods
    # available to all `case_match` nodes within RuboCop.
    class CaseMatchNode < Node
      include ConditionalNode

      # Returns the keyword of the `case` statement as a string.
      #
      # @return [String] the keyword of the `case` statement
      def keyword
        'case'
      end

      # @deprecated Use `in_pattern_branches.each`
      def each_in_pattern(&block)
        return in_pattern_branches.to_enum(__method__) unless block

        in_pattern_branches.each(&block)

        self
      end

      # Returns an array of all the `in` pattern branches in the `case` statement.
      #
      # @return [Array<InPatternNode>] an array of `in_pattern` nodes
      def in_pattern_branches
        node_parts[1...-1]
      end

      # Returns an array of all the when branches in the `case` statement.
      #
      # @return [Array<Node, nil>] an array of the bodies of the `in` branches
      # and the `else` (if any). Note that these bodies could be nil.
      def branches
        bodies = in_pattern_branches.map(&:body)
        if else?
          # `empty-else` node sets nil because it has no body.
          else_branch.empty_else_type? ? bodies.push(nil) : bodies.push(else_branch)
        end
        bodies
      end

      # Returns the else branch of the `case` statement, if any.
      #
      # @return [Node] the else branch node of the `case` statement
      # @return [EmptyElse] the empty else branch node of the `case` statement
      # @return [nil] if the case statement does not have an else branch.
      def else_branch
        node_parts[-1]
      end

      # Checks whether this case statement has an `else` branch.
      #
      # @return [Boolean] whether the `case` statement has an `else` branch
      def else?
        !loc.else.nil?
      end
    end
  end
end

Version data entries

160 entries across 154 versions & 19 rubygems

Version Path
rubocop-ast-1.36.1 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.36.0 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.35.0 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.34.1 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.34.0 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.33.1 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.33.0 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.32.3 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.32.2 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.32.1 lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.32.0 lib/rubocop/ast/node/case_match_node.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/node/case_match_node.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.23.0/lib/rubocop/ast/node/case_match_node.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.23.0/lib/rubocop/ast/node/case_match_node.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.23.0/lib/rubocop/ast/node/case_match_node.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.31.3/lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.31.3 lib/rubocop/ast/node/case_match_node.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/case_match_node.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.31.2/lib/rubocop/ast/node/case_match_node.rb
rubocop-ast-1.31.2 lib/rubocop/ast/node/case_match_node.rb