Class: RuboCop::Cop::Metrics::LineLength

Inherits:
Cop
  • Object
show all
Includes:
ConfigurableMax
Defined in:
lib/rubocop/cop/metrics/line_length.rb

Overview

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

Constant Summary collapse

MSG =
'Line is too long. [%d/%d]'.freeze

Constants included from Util

Util::ASGN_NODES, Util::BYTE_ORDER_MARK, Util::EQUALS_ASGN_NODES, Util::LITERAL_REGEX, Util::OPERATOR_METHODS, Util::SHORTHAND_ASGN_NODES, Util::STRING_ESCAPES, Util::STRING_ESCAPE_REGEX

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offenses, #processed_source

Instance Method Summary collapse

Methods included from ConfigurableMax

#max=, #parameter_name

Methods inherited from Cop

#add_offense, all, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, cop_type, #correct, #debug?, #details, #display_cop_names?, #display_style_guide?, #excluded_file?, #extra_details?, inherited, #initialize, #join_force?, lint?, match?, #message, non_rails, #parse, qualified_cop_name, #reference_url, #relevant_file?, #style_guide_url, #target_ruby_version

Methods included from Sexp

#s

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search, #node_search_body

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

begins_its_line?, block_length, comment_line?, directions, double_quotes_acceptable?, double_quotes_required?, effective_column, ends_its_line?, first_part_of_call_chain, interpret_string_escapes, line_range, move_pos, numeric_range_size, on_node, operator?, parentheses?, range_with_surrounding_comma, range_with_surrounding_space, source_range, strip_quotes, to_string_literal, to_symbol_literal, within_node?

Methods included from PathUtil

absolute?, hidden?, issue_deprecation_warning, match_path?, relative_path

Constructor Details

This class inherits a constructor from RuboCop::Cop::Cop

Instance Method Details

#allow_heredoc?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rubocop/cop/metrics/line_length.rb', line 49

def allow_heredoc?
  allowed_heredoc
end

#allow_uri?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/rubocop/cop/metrics/line_length.rb', line 74

def allow_uri?
  cop_config['AllowURI']
end

#allowed_heredocObject



53
54
55
# File 'lib/rubocop/cop/metrics/line_length.rb', line 53

def allowed_heredoc
  cop_config['AllowHeredoc']
end

#allowed_uri_position?(line, uri_range) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rubocop/cop/metrics/line_length.rb', line 78

def allowed_uri_position?(line, uri_range)
  uri_range.begin < max && uri_range.end == line.length
end

#extract_heredocs(ast) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/rubocop/cop/metrics/line_length.rb', line 57

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) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/rubocop/cop/metrics/line_length.rb', line 82

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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/metrics/line_length.rb', line 16

def investigate(processed_source)
  heredocs = extract_heredocs(processed_source.ast) if allow_heredoc?
  processed_source.lines.each_with_index do |line, index|
    next unless line.length > max

    if allow_heredoc?
      next if line_in_whitelisted_heredoc?(heredocs, index.succ)
    end

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

    message = format(MSG, line.length, max)

    excessive_position = if uri_range && uri_range.begin < max
                           uri_range.end
                         else
                           max
                         end

    range = source_range(processed_source.buffer, index + 1,
                         excessive_position...(line.length))

    add_offense(nil, range, message) { self.max = line.length }
  end
end

#line_in_whitelisted_heredoc?(heredocs, line_number) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/rubocop/cop/metrics/line_length.rb', line 67

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) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/rubocop/cop/metrics/line_length.rb', line 90

def match_uris(string)
  matches = []
  string.scan(uri_regexp) do
    matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0])
  end
  matches
end

#maxObject



45
46
47
# File 'lib/rubocop/cop/metrics/line_length.rb', line 45

def max
  cop_config['Max']
end

#uri_regexpObject



105
106
107
# File 'lib/rubocop/cop/metrics/line_length.rb', line 105

def uri_regexp
  @regexp ||= URI.regexp(cop_config['URISchemes'])
end

#valid_uri?(uri_ish_string) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
# File 'lib/rubocop/cop/metrics/line_length.rb', line 98

def valid_uri?(uri_ish_string)
  URI.parse(uri_ish_string)
  true
rescue
  false
end