Sha256: eb579aa651ef0f21b6759abf8e6ec9fb03cc8cfd025ba631490f8c9f95a0bf1e

Contents?: true

Size: 1.7 KB

Versions: 21

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `case` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `case` nodes within RuboCop.
    class CaseNode < 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 `when_branches.each`
      def each_when(&block)
        return when_branches.to_enum(__method__) unless block_given?

        when_branches.each(&block)

        self
      end

      # Returns an array of all the when branches in the `case` statement.
      #
      # @return [Array<WhenNode>] an array of `when` nodes
      def when_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 when branches
      # and the else (if any). Note that these bodies could be nil.
      def branches
        bodies = when_branches.map(&:body)
        bodies.push(else_branch) if else?
        bodies
      end

      # Returns the else branch of the `case` statement, if any.
      #
      # @return [Node] the 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
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/case_node.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/case_node.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/case_node.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/case_node.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/case_node.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/case_node.rb
rubocop-ast-1.1.1 lib/rubocop/ast/node/case_node.rb
rubocop-ast-1.1.0 lib/rubocop/ast/node/case_node.rb
rubocop-ast-1.0.1 lib/rubocop/ast/node/case_node.rb
rubocop-ast-1.0.0 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.8.0 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.7.1 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.7.0 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.6.0 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.5.1 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.5.0 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.4.2 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.4.1 lib/rubocop/ast/node/case_node.rb
rubocop-ast-0.4.0 lib/rubocop/ast/node/case_node.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.3.0/lib/rubocop/ast/node/case_node.rb