class RuboCop::Cop::Metrics::LineLength
of the ‘Layout/Tab` cop.
The tab size is configured in the `IndentationWidth`
The maximum length is configurable.
This cop checks the length of lines in the source code.
def allow_heredoc?
def allow_heredoc? allowed_heredoc end
def allow_uri?
def allow_uri? cop_config['AllowURI'] end
def allowed_heredoc
def allowed_heredoc cop_config['AllowHeredoc'] end
def allowed_uri_position?(line, uri_range)
def allowed_uri_position?(line, uri_range) uri_range.begin < max && (uri_range.end == line_length(line) || uri_range.end == line_length(line) - 1) end
def check_directive_line(line, index)
def check_directive_line(line, index) return if line_length_without_directive(line) <= max range = max..(line_length_without_directive(line) - 1) register_offense( source_range( processed_source.buffer, index + 1, range ), line ) end
def check_line(line, index, heredocs)
def check_line(line, index, heredocs) return if line_length(line) <= max return if ignored_line?(line, index, heredocs) if ignore_cop_directives? && directive_on_source_line?(index) return check_directive_line(line, index) end return check_uri_line(line, index) if allow_uri? register_offense( source_range( processed_source.buffer, index, highligh_start(line)...line_length(line) ), line ) end
def check_uri_line(line, index)
def check_uri_line(line, index) uri_range = find_excessive_uri_range(line) return if uri_range && allowed_uri_position?(line, uri_range) register_offense(excess_range(uri_range, line, index), line) end
def directive_on_source_line?(index)
def directive_on_source_line?(index) source_line_number = index + processed_source.buffer.first_line comment = processed_source .comments .detect { |e| e.location.line == source_line_number } return false unless comment comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP) end
def excess_range(uri_range, line, index)
def excess_range(uri_range, line, index) excessive_position = if uri_range && uri_range.begin < max uri_range.end else highligh_start(line) end source_range(processed_source.buffer, index + 1, excessive_position...(line_length(line))) end
def extract_heredocs(ast)
def extract_heredocs(ast) return [] unless ast ast.each_node(:str, :dstr, :xstr).select(&:heredoc?).map do |node| body = node.location.heredoc_body delimiter = node.location.heredoc_end.source.strip [body.first_line...body.last_line, delimiter] end end
def find_excessive_uri_range(line)
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
def highligh_start(line)
def highligh_start(line) return max unless tab_indentation_width max - (tab_indentation_width - 1) * line.count("\t") end
def ignore_cop_directives?
def ignore_cop_directives? cop_config['IgnoreCopDirectives'] end
def ignored_line?(line, index, heredocs)
def ignored_line?(line, index, heredocs) matches_ignored_pattern?(line) || heredocs && line_in_whitelisted_heredoc?(heredocs, index.succ) end
def investigate(processed_source)
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
def line_in_whitelisted_heredoc?(heredocs, line_number)
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
def line_length(line)
def line_length(line) if tab_indentation_width line = line.gsub("\t", ' ' * tab_indentation_width) end line.length end
def line_length_without_directive(line)
def line_length_without_directive(line) before_comment, = line.split(CommentConfig::COMMENT_DIRECTIVE_REGEXP) before_comment.rstrip.length end
def match_uris(string)
def match_uris(string) matches = [] string.scan(uri_regexp) do matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0]) end matches end
def max
def max cop_config['Max'] end
def register_offense(loc, line)
def register_offense(loc, line) message = format(MSG, length: line_length(line), max: max) add_offense(nil, location: loc, message: message) do self.max = line_length(line) end end
def tab_indentation_width
def tab_indentation_width config.for_cop('Layout/Tab')['IndentationWidth'] end
def uri_regexp
def uri_regexp @uri_regexp ||= URI::DEFAULT_PARSER.make_regexp(cop_config['URISchemes']) end
def valid_uri?(uri_ish_string)
def valid_uri?(uri_ish_string) URI.parse(uri_ish_string) true rescue URI::InvalidURIError, NoMethodError false end