Skip to content

Commit

Permalink
[Fix #10374] Fix a false positive for Layout/LineLength when long U…
Browse files Browse the repository at this point in the history
…RIs in yardoc comments to have titles
  • Loading branch information
ydah committed Mar 18, 2022
1 parent 5b6e3a2 commit 02d7f46
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_layout_line_length.md
@@ -0,0 +1 @@
* [#10459](https://github.com/rubocop/rubocop/pull/10459): Fix a false positive for `Layout/LineLength` when long URIs in yardoc comments to have titles. ([@ydah][])
23 changes: 17 additions & 6 deletions lib/rubocop/cop/mixin/line_length_help.rb
Expand Up @@ -39,12 +39,7 @@ def find_excessive_uri_range(line)
pos + indentation_difference(line)
end

# Extend the end position until the start of the next word, if any.
# This allows for URIs that are wrapped in quotes or parens to be handled properly
# while not allowing additional words to be added after the URL.
if (match = line[end_position..line_length(line)]&.match(/^\S+(?=\s|$)/))
end_position += match.offset(0).last
end
end_position = extend_uri_end_position(line, end_position)

return nil if begin_position < max_line_length && end_position < max_line_length

Expand All @@ -65,6 +60,22 @@ def indentation_difference(line)
(line.index(/[^\t]/) || 0) * (tab_indentation_width - 1)
end

def extend_uri_end_position(line, end_position)
# Extend the end position YARD comments with linked URLs of the form {<uri> <title>}
if line&.match(/{(\s|\S)*}$/)
match = line[end_position..line_length(line)]&.match(/(\s|\S)*}/)
end_position += match.offset(0).last
end

# Extend the end position until the start of the next word, if any.
# This allows for URIs that are wrapped in quotes or parens to be handled properly
# while not allowing additional words to be added after the URL.
if (match = line[end_position..line_length(line)]&.match(/^\S+(?=\s|$)/))
end_position += match.offset(0).last
end
end_position
end

def tab_indentation_width
config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
config.for_cop('Layout/IndentationWidth')['Width']
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cop/layout/line_length_spec.rb
Expand Up @@ -100,6 +100,22 @@
RUBY
end
end

context 'and the URL is wrapped in braces' do
it 'accepts the line' do
expect_no_offenses(<<-RUBY)
# See: {https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c}
RUBY
end
end

context 'and the URL is wrapped in braces with title' do
it 'accepts the line' do
expect_no_offenses(<<-RUBY)
# See: {https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c Optional Title}
RUBY
end
end
end

context 'and the excessive characters include a complete URL' do
Expand Down Expand Up @@ -131,6 +147,16 @@
end
end

context 'and the excessive characters include part of a URL in braces and another word' do
it 'registers an offense for the line' do
expect_offense(<<-RUBY)
# See: {https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c} and
^^^^ Line is too long. [105/80]
# http://google.com/
RUBY
end
end

context 'and the excessive characters include part of a URL and trailing whitespace' do
it 'registers an offense for the line' do
expect_offense(<<-RUBY)
Expand Down

0 comments on commit 02d7f46

Please sign in to comment.