Sha256: 0467c758fc32a59931e0ec20d15bd3b550bbe5999654b78bd97a3806496dd039

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for excessive nesting of conditional and looping
      # constructs. Despite the cop's name, blocks are not considered as a
      # extra level of nesting.
      #
      # The maximum level of nesting allowed is configurable.
      class BlockNesting < Cop
        NESTING_BLOCKS = [:case, :if, :while, :while_post, :until, :until_post,
                          :for, :resbody]

        def investigate(processed_source)
          return unless processed_source.ast
          max = cop_config['Max']
          check_nesting_level(processed_source.ast, max, 0)
        end

        private

        def check_nesting_level(node, max, current_level)
          if NESTING_BLOCKS.include?(node.type)
            unless node.loc.respond_to?(:keyword) &&
                node.loc.keyword.is?('elsif')
              current_level += 1
            end
            if current_level == max + 1
              convention(node, :expression, message(max))
              return
            end
          end
          node.children.each do |child|
            if child.is_a?(Parser::AST::Node)
              check_nesting_level(child, max, current_level)
            end
          end
        end

        def message(max)
          "Avoid more than #{max} levels of block nesting."
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.15.0 lib/rubocop/cop/style/block_nesting.rb
rubocop-0.14.1 lib/rubocop/cop/style/block_nesting.rb
rubocop-0.14.0 lib/rubocop/cop/style/block_nesting.rb
rubocop-0.13.1 lib/rubocop/cop/style/block_nesting.rb
rubocop-0.13.0 lib/rubocop/cop/style/block_nesting.rb