Sha256: c647a133dffaaf29f9c1ba2880b6cc54c7c729a4669209b7cf390d05750ed0ca

Contents?: true

Size: 1.49 KB

Versions: 32

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for checking length of code segments.
    module CodeLength
      include ConfigurableMax

      MSG = '%<label>s has too many lines. [%<length>d/%<max>d]'

      private

      def message(length, max_length)
        format(MSG, label: cop_label, length: length, max: max_length)
      end

      def max_length
        cop_config['Max']
      end

      def count_comments?
        cop_config['CountComments']
      end

      def count_as_one
        Array(cop_config['CountAsOne']).map(&:to_sym)
      end

      def check_code_length(node)
        # Skip costly calculation when definitely not needed
        return if node.line_count <= max_length

        calculator = build_code_length_calculator(node)
        length = calculator.calculate
        return if length <= max_length

        location = node.casgn_type? ? node.loc.name : node.loc.expression

        add_offense(location, message: message(length, max_length)) do
          self.max = length
        end
      end

      # Returns true for lines that shall not be included in the count.
      def irrelevant_line(source_line)
        source_line.blank? || !count_comments? && comment_line?(source_line)
      end

      def build_code_length_calculator(node)
        Metrics::Utils::CodeLengthCalculator.new(
          node,
          processed_source,
          count_comments: count_comments?,
          foldable_types: count_as_one
        )
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/code_length.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/code_length.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/code_length.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/code_length.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/code_length.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/code_length.rb
rubocop-1.9.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.8.1 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.8.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.7.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.6.1 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.6.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.5.2 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.5.1 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.5.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.4.2 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.4.1 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.4.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.3.1 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.3.0 lib/rubocop/cop/mixin/code_length.rb