Sha256: 68b3d59f8d69690e868670a9553139fd55ce589355a6b626f139019698d0efc4

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 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

          node.each_descendant(:class, :module) do |inner_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

4 entries across 4 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/metrics/class_length.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/metrics/class_length.rb
rubocop-0.26.1 lib/rubocop/cop/metrics/class_length.rb
rubocop-0.26.0 lib/rubocop/cop/metrics/class_length.rb