class RuboCop::Cop::Metrics::LineLength

This cop checks the length of lines in the source code. The maximum length is configurable.

Constants

MSG

Public Instance Methods

allow_heredoc?() click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 55
def allow_heredoc?
  allowed_heredoc
end
allow_uri?() click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 80
def allow_uri?
  cop_config['AllowURI']
end
allowed_heredoc() click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 59
def allowed_heredoc
  cop_config['AllowHeredoc']
end
allowed_uri_position?(line, uri_range) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 84
def allowed_uri_position?(line, uri_range)
  uri_range.begin < max && uri_range.end == line.length
end
check_line(line, index, heredocs) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 22
def check_line(line, index, heredocs)
  return unless line.length > max
  return if heredocs &&
            line_in_whitelisted_heredoc?(heredocs, index.succ)

  if allow_uri?
    uri_range = find_excessive_uri_range(line)
    return if uri_range && allowed_uri_position?(line, uri_range)
  end

  offense(excess_range(uri_range, line, index), line)
end
excess_range(uri_range, line, index) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 40
def excess_range(uri_range, line, index)
  excessive_position = if uri_range && uri_range.begin < max
                         uri_range.end
                       else
                         max
                       end

  source_range(processed_source.buffer, index + 1,
               excessive_position...(line.length))
end
extract_heredocs(ast) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 63
def extract_heredocs(ast)
  return [] unless ast
  ast.each_node.with_object([]) do |node, heredocs|
    next unless node.location.is_a?(Parser::Source::Map::Heredoc)
    body = node.location.heredoc_body
    delimiter = node.location.heredoc_end.source.strip
    heredocs << [body.first_line...body.last_line, delimiter]
  end
end
find_excessive_uri_range(line) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 88
def find_excessive_uri_range(line)
  last_uri_match = match_uris(line).last
  return nil unless last_uri_match
  begin_position, end_position = last_uri_match.offset(0)
  return nil if begin_position < max && end_position < max
  begin_position...end_position
end
investigate(processed_source) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 15
def investigate(processed_source)
  heredocs = extract_heredocs(processed_source.ast) if allow_heredoc?
  processed_source.lines.each_with_index do |line, index|
    check_line(line, index, heredocs)
  end
end
line_in_whitelisted_heredoc?(heredocs, line_number) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 73
def line_in_whitelisted_heredoc?(heredocs, line_number)
  heredocs.any? do |range, delimiter|
    range.cover?(line_number) &&
      (allowed_heredoc == true || allowed_heredoc.include?(delimiter))
  end
end
match_uris(string) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 96
def match_uris(string)
  matches = []
  string.scan(uri_regexp) do
    matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0])
  end
  matches
end
max() click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 51
def max
  cop_config['Max']
end
offense(loc, line) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 35
def offense(loc, line)
  message = format(MSG, line.length, max)
  add_offense(nil, loc, message) { self.max = line.length }
end
uri_regexp() click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 111
def uri_regexp
  @regexp ||= URI.regexp(cop_config['URISchemes'])
end
valid_uri?(uri_ish_string) click to toggle source
# File lib/rubocop/cop/metrics/line_length.rb, line 104
def valid_uri?(uri_ish_string)
  URI.parse(uri_ish_string)
  true
rescue
  false
end