123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::CodeLength

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: lib/rubocop/cop/mixin/code_length.rb

Overview

Common functionality for checking length of code segments.

Constant Summary

Class Method Summary

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#count_comments?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/code_length.rb', line 23

def count_comments?
  cop_config['CountComments']
end

Instance Method Details

#build_code_length_calculator(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/code_length.rb', line 49

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

#check_code_length(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/code_length.rb', line 31

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

#count_as_one (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/code_length.rb', line 27

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

#irrelevant_line(source_line) (private)

Returns true for lines that shall not be included in the count.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/code_length.rb', line 45

def irrelevant_line(source_line)
  source_line.blank? || (!count_comments? && comment_line?(source_line))
end

#location(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/code_length.rb', line 58

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

#max_length (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/code_length.rb', line 19

def max_length
  cop_config['Max']
end

#message(length, max_length) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/code_length.rb', line 15

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