Class: RuboCop::Cop::Metrics::LineLength
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
#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
#def_node_matcher, #def_node_search, #node_search_body
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #support_autocorrect?
#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?, parenthesized_call?, range_with_surrounding_comma, range_with_surrounding_space, source_range, strip_quotes, ternary_op?, to_string_literal, to_symbol_literal, within_node?
Methods included from PathUtil
absolute?, hidden?, issue_deprecation_warning, match_path?, relative_path
Instance Method Details
#allow_heredoc? ⇒ Boolean
56
57
58
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 56
def allow_heredoc?
allowed_heredoc
end
|
#allow_uri? ⇒ Boolean
81
82
83
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 81
def allow_uri?
cop_config['AllowURI']
end
|
#allowed_heredoc ⇒ Object
60
61
62
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 60
def allowed_heredoc
cop_config['AllowHeredoc']
end
|
#allowed_uri_position?(line, uri_range) ⇒ Boolean
85
86
87
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 85
def allowed_uri_position?(line, uri_range)
uri_range.begin < max && uri_range.end == line.length
end
|
#check_line(line, index, heredocs) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 23
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) ⇒ Object
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 41
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
|
64
65
66
67
68
69
70
71
72
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 64
def (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
89
90
91
92
93
94
95
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 89
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
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 16
def investigate(processed_source)
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) ⇒ Boolean
74
75
76
77
78
79
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 74
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
97
98
99
100
101
102
103
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 97
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 ⇒ Object
52
53
54
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 52
def max
cop_config['Max']
end
|
#offense(loc, line) ⇒ Object
36
37
38
39
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 36
def offense(loc, line)
message = format(MSG, line.length, max)
add_offense(nil, loc, message) { self.max = line.length }
end
|
#uri_regexp ⇒ Object
112
113
114
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 112
def uri_regexp
@regexp ||= URI.regexp(cop_config['URISchemes'])
end
|
#valid_uri?(uri_ish_string) ⇒ Boolean
105
106
107
108
109
110
|
# File 'lib/rubocop/cop/metrics/line_length.rb', line 105
def valid_uri?(uri_ish_string)
URI.parse(uri_ish_string)
true
rescue
false
end
|