From 02d7f46e498b870ea5a7e7b815165a6b3af212ec Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Sat, 19 Mar 2022 02:21:54 +0900 Subject: [PATCH] [Fix #10374] Fix a false positive for `Layout/LineLength` when long URIs in yardoc comments to have titles --- ...a_false_positive_for_layout_line_length.md | 1 + lib/rubocop/cop/mixin/line_length_help.rb | 23 +++++++++++----- spec/rubocop/cop/layout/line_length_spec.rb | 26 +++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_layout_line_length.md diff --git a/changelog/fix_a_false_positive_for_layout_line_length.md b/changelog/fix_a_false_positive_for_layout_line_length.md new file mode 100644 index 00000000000..74744ae5a97 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/mixin/line_length_help.rb b/lib/rubocop/cop/mixin/line_length_help.rb index 4ec801cfc20..9eadb30b884 100644 --- a/lib/rubocop/cop/mixin/line_length_help.rb +++ b/lib/rubocop/cop/mixin/line_length_help.rb @@ -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 @@ -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 { } + 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'] diff --git a/spec/rubocop/cop/layout/line_length_spec.rb b/spec/rubocop/cop/layout/line_length_spec.rb index 05183650185..44b72d0e3c5 100644 --- a/spec/rubocop/cop/layout/line_length_spec.rb +++ b/spec/rubocop/cop/layout/line_length_spec.rb @@ -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 @@ -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)