Sha256: 7d0a411d222fffdb0e5d8aa3b934961545224e2f0d08bc927bd0432ab332bc94

Contents?: true

Size: 874 Bytes

Versions: 2

Compression:

Stored size: 874 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    class BlockNesting < Cop
      NESTING_BLOCKS = [:case, :if, :while, :until, :for, :resbody]

      def inspect(source, tokens, ast, comments)
        max = BlockNesting.config['Max']
        check_nesting_level(ast, max, 0)
      end

      private

      def check_nesting_level(node, max, current_level)
        if NESTING_BLOCKS.include?(node.type)
          current_level += 1
          if current_level == max + 1
            add_offence(:convention, node.location.line, 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.8.3 lib/rubocop/cop/block_nesting.rb
rubocop-0.8.2 lib/rubocop/cop/block_nesting.rb