Sha256: b1c1f77c5ddac33af1be3cb3e4c35a21df4f23625d6d3e56576238bab43aead1

Contents?: true

Size: 1.75 KB

Versions: 15

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for checking length of code segments.
    module CodeLength
      extend ExcludeLimit

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

      exclude_limit 'Max'

      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 = location(node)

        add_offense(location, message: message(length, max_length)) { self.max = length }
      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

      def location(node)
        return node.loc.name if node.casgn_type?

        if LSP.enabled?
          end_range = node.loc.respond_to?(:name) ? node.loc.name : node.loc.begin
          node.source_range.begin.join(end_range)
        else
          node.source_range
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
rubocop-1.69.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.68.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.67.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.66.1 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.66.0 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.65.1 lib/rubocop/cop/mixin/code_length.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/code_length.rb
rubocop-1.65.0 lib/rubocop/cop/mixin/code_length.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/code_length.rb
rubocop-1.64.1 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.63.4 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.63.3 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.63.2 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.63.1 lib/rubocop/cop/mixin/code_length.rb
rubocop-1.63.0 lib/rubocop/cop/mixin/code_length.rb