Sha256: 5f04da50653347874e7b5e78def980222b6cf49d70172d7aa1207441c3114db4

Contents?: true

Size: 1.24 KB

Versions: 21

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `when` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `when` nodes within RuboCop.
    class WhenNode < Node
      # Returns an array of all the conditions in the `when` branch.
      #
      # @return [Array<Node>] an array of condition nodes
      def conditions
        node_parts[0...-1]
      end

      # @deprecated Use `conditions.each`
      def each_condition(&block)
        return conditions.to_enum(__method__) unless block_given?

        conditions.each(&block)

        self
      end

      # Returns the index of the `when` branch within the `case` statement.
      #
      # @return [Integer] the index of the `when` branch
      def branch_index
        parent.when_branches.index(self)
      end

      # Checks whether the `when` node has a `then` keyword.
      #
      # @return [Boolean] whether the `when` node has a `then` keyword
      def then?
        loc.begin&.is?('then')
      end

      # Returns the body of the `when` node.
      #
      # @return [Node, nil] the body of the `when` node
      def body
        node_parts[-1]
      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/when_node.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/when_node.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/when_node.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/when_node.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/when_node.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/when_node.rb
rubocop-ast-1.1.1 lib/rubocop/ast/node/when_node.rb
rubocop-ast-1.1.0 lib/rubocop/ast/node/when_node.rb
rubocop-ast-1.0.1 lib/rubocop/ast/node/when_node.rb
rubocop-ast-1.0.0 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.8.0 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.7.1 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.7.0 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.6.0 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.5.1 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.5.0 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.4.2 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.4.1 lib/rubocop/ast/node/when_node.rb
rubocop-ast-0.4.0 lib/rubocop/ast/node/when_node.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.3.0/lib/rubocop/ast/node/when_node.rb