Sha256: 3a7606937b8174c7a30581ff44c3f2781840e04e9843247e6901931ec16465cd

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Metrics
      # This cop checks if the length a class exceeds some maximum value.
      # Comment lines can optionally be ignored.
      # The maximum allowed length is configurable.
      class ClassLength < Cop
        include CodeLength

        def on_class(node)
          check_code_length(node)
        end

        private

        def message(length, max_length)
          format('Class definition is too long. [%d/%d]', length, max_length)
        end

        def code_length(node)
          class_body_line_numbers = line_range(node).to_a[1...-1]

          target_line_numbers = class_body_line_numbers -
                                  line_numbers_of_inner_classes(node)

          target_line_numbers.reduce(0) do |length, line_number|
            source_line = processed_source[line_number]
            next length if irrelevant_line(source_line)
            length + 1
          end
        end

        def line_numbers_of_inner_classes(node)
          line_numbers = Set.new

          on_node([:class, :module], node) do |inner_node|
            next if inner_node.eql?(node)
            line_range = line_range(inner_node)
            line_numbers.merge(line_range)
          end

          line_numbers.to_a
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.25.0 lib/rubocop/cop/metrics/class_length.rb