class RuboCop::Cop::Infl::SoftLineLength

Checks forgivingly for line lengths

Constants

HARD_MSG
SOFT_MSG

Public Instance Methods

investigate(processed_source) click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 11
def investigate(processed_source)
  stats = calculate_stats(processed_source)
  soft_credit =
    (stats.size * soft_allowance_percent_of_lines / 100.0).round

  stats.each do |check, index, length|
    soft_credit = report(soft_credit, check, index, length)
  end
end

Private Instance Methods

calculate_stats(processed_source) click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 23
def calculate_stats(processed_source)
  processed_source
    .lines
    .each_with_index
    .map { |line, index| check_line(line, index) }
    .compact
end
check_line(line, index) click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 31
def check_line(line, index)
  case line.length
  when 0                         then nil
  when ->(l) { l <= soft_limit } then [:ok, nil, nil]
  when ->(l) { l <= hard_limit } then [:soft, index, line.length]
  else                                [:hard, index, line.length]
  end
end
hard_limit() click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 70
def hard_limit
  cop_config['HardLimit']
end
hard_offense(processed_source, index, length) click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 56
def hard_offense(processed_source, index, length)
  offense(processed_source, HARD_MSG, index, length, hard_limit)
end
offense(processed_source, template, index, length, limit) click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 60
def offense(processed_source, template, index, length, limit)
  loc = source_range(processed_source.buffer, index + 1, 0...length)
  msg = format(template, length, limit)
  add_offense(nil, loc, msg)
end
report(soft_credit, check, index, length) click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 40
def report(soft_credit, check, index, length)
  case check
  when :soft
    soft_credit -= 1
    soft_offense(processed_source, index, length) if soft_credit < 0
  when :hard
    hard_offense(processed_source, index, length)
  end

  soft_credit
end
soft_allowance_percent_of_lines() click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 74
def soft_allowance_percent_of_lines
  cop_config['AllowedLongLinePercentage']
end
soft_limit() click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 66
def soft_limit
  cop_config['SoftLimit']
end
soft_offense(processed_source, index, length) click to toggle source
# File lib/rubocop/cop/infl/soft_line_length.rb, line 52
def soft_offense(processed_source, index, length)
  offense(processed_source, SOFT_MSG, index, length, soft_limit)
end