Sha256: b93ae97180d8a3450f6773b128886af021252dd8e2ca1325bf5986434bda2100

Contents?: true

Size: 1.42 KB

Versions: 16

Compression:

Stored size: 1.42 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Metrics
      # This cop checks for excessive nesting of conditional and looping
      # constructs. Despite the cop's name, blocks are not considered as an
      # extra level of nesting.
      #
      # The maximum level of nesting allowed is configurable.
      class BlockNesting < Cop
        include ConfigurableMax
        include IfNode

        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)
            current_level += 1 unless elsif?(node)
            if current_level > max
              self.max = current_level
              unless part_of_ignored_node?(node)
                add_offense(node, :expression, message(max)) do
                  ignore_node(node)
                end
              end
            end
          end

          node.each_child_node do |child_node|
            check_nesting_level(child_node, max, current_level)
          end
        end

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.35.0 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.34.2 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.34.1 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.34.0 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.33.0 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.32.1 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.32.0 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.31.0 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.30.1 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.30.0 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.29.1 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.29.0 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.28.0 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.27.1 lib/rubocop/cop/metrics/block_nesting.rb
rubocop-0.27.0 lib/rubocop/cop/metrics/block_nesting.rb