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]'

Constants included from Util

Util::ASGN_NODES, Util::EQUALS_ASGN_NODES, Util::OPERATOR_METHODS, Util::PROC_NEW_NODE, Util::SHORTHAND_ASGN_NODES

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, #autocorrect?, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, cop_name_with_namespace, cop_type, #debug?, #display_cop_names?, inherited, #initialize, #join_force?, lint?, #message, non_rails, qualified_cop_name, rails?, #relevant_file?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

begins_its_line?, block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, numeric_range_size, on_node, operator?, parentheses?, proc?, range_with_surrounding_space, source_range, strip_quotes, within_node?

Methods included from PathUtil

issue_deprecation_warning, match_path?, relative_path

Constructor Details

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

Instance Method Details

#allow_uri?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rubocop/cop/metrics/line_length.rb', line 43

def allow_uri?
  cop_config['AllowURI']
end

#allowed_uri_position?(line, uri_range) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rubocop/cop/metrics/line_length.rb', line 47

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

#find_excessive_uri_range(line) ⇒ Object



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

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



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

def investigate(processed_source)
  processed_source.lines.each_with_index do |line, index|
    next unless line.length > max

    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

#match_uris(string) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubocop/cop/metrics/line_length.rb', line 59

def match_uris(string)
  matches = []
  unscanned_position = 0

  loop do
    match_data = string.match(uri_regexp, unscanned_position)
    break unless match_data

    uri_ish_string = match_data[0]
    matches << match_data if valid_uri?(uri_ish_string)

    _, unscanned_position = match_data.offset(0)
  end

  matches
end

#maxObject



39
40
41
# File 'lib/rubocop/cop/metrics/line_length.rb', line 39

def max
  cop_config['Max']
end

#uri_regexpObject



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

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

#valid_uri?(uri_ish_string) ⇒ Boolean

Returns:

  • (Boolean)


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

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