Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10374] Fix a false positive for Layout/LineLength when long URIs in yardoc comments to have titles #10459

Merged
merged 1 commit into from Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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