Class: RuboCop::Cop::Style::LineLength
- Includes:
- ConfigurableMax
- Defined in:
- lib/rubocop/cop/style/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
- #allow_uri? ⇒ Boolean
- #allowed_uri_position?(line, uri_range) ⇒ Boolean
- #find_excessive_uri_range(line) ⇒ Object
- #investigate(processed_source) ⇒ Object
- #match_uris(string) ⇒ Object
- #max ⇒ Object
- #valid_uri?(uri_ish_string) ⇒ Boolean
Methods included from ConfigurableMax
Methods inherited from Cop
#add_offense, all, #autocorrect?, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, cop_type, #debug?, #display_cop_names?, #exclude_file?, #include_file?, 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
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
Methods included from PathUtil
Constructor Details
This class inherits a constructor from RuboCop::Cop::Cop
Instance Method Details
#allow_uri? ⇒ Boolean
46 47 48 |
# File 'lib/rubocop/cop/style/line_length.rb', line 46 def allow_uri? cop_config['AllowURI'] end |
#allowed_uri_position?(line, uri_range) ⇒ Boolean
50 51 52 |
# File 'lib/rubocop/cop/style/line_length.rb', line 50 def allowed_uri_position?(line, uri_range) uri_range.begin < max && uri_range.end == line.length end |
#find_excessive_uri_range(line) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/rubocop/cop/style/line_length.rb', line 54 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 38 39 40 |
# File 'lib/rubocop/cop/style/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 = 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, ) do self.max = line.length end end end |
#match_uris(string) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rubocop/cop/style/line_length.rb', line 62 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 |
#max ⇒ Object
42 43 44 |
# File 'lib/rubocop/cop/style/line_length.rb', line 42 def max cop_config['Max'] end |
#valid_uri?(uri_ish_string) ⇒ Boolean
79 80 81 82 83 84 |
# File 'lib/rubocop/cop/style/line_length.rb', line 79 def valid_uri?(uri_ish_string) URI.parse(uri_ish_string) true rescue false end |