From b1f4871411b1c9771b2c82315f1ff75dbcbb7fc6 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Mon, 13 Sep 2021 15:17:41 -0400 Subject: [PATCH] Fix `Layout/LineLength` reported length when ignoring directive comments. If `IgnoreCopDirectives` is enabled, the directive comment is not included in the calculation, but it is included in the count that is shown in the offense. This change make it not be included there either. --- .../fix_fix_layoutlinelength_reported_length.md | 1 + lib/rubocop/cop/layout/line_length.rb | 12 +++++++----- spec/rubocop/cop/layout/line_length_spec.rb | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 changelog/fix_fix_layoutlinelength_reported_length.md diff --git a/changelog/fix_fix_layoutlinelength_reported_length.md b/changelog/fix_fix_layoutlinelength_reported_length.md new file mode 100644 index 00000000000..bd50770a4fd --- /dev/null +++ b/changelog/fix_fix_layoutlinelength_reported_length.md @@ -0,0 +1 @@ +* [#10078](https://github.com/rubocop/rubocop/pull/10078): Fix `Layout/LineLength` reported length when ignoring directive comments. ([@dvandersluis][]) diff --git a/lib/rubocop/cop/layout/line_length.rb b/lib/rubocop/cop/layout/line_length.rb index cb50a69639e..47fa8c53d5f 100644 --- a/lib/rubocop/cop/layout/line_length.rb +++ b/lib/rubocop/cop/layout/line_length.rb @@ -183,8 +183,8 @@ def shebang?(line, line_index) line_index.zero? && line.start_with?('#!') end - def register_offense(loc, line, line_index) - message = format(MSG, length: line_length(line), max: max) + def register_offense(loc, line, line_index, length: line_length(line)) + message = format(MSG, length: length, max: max) self.breakable_range = breakable_range_by_line_index[line_index] @@ -241,9 +241,10 @@ def line_in_heredoc?(line_number) end def check_directive_line(line, line_index) - return if line_length_without_directive(line) <= max + length_without_directive = line_length_without_directive(line) + return if length_without_directive <= max - range = max..(line_length_without_directive(line) - 1) + range = max..(length_without_directive - 1) register_offense( source_range( processed_source.buffer, @@ -251,7 +252,8 @@ def check_directive_line(line, line_index) range ), line, - line_index + line_index, + length: length_without_directive ) end diff --git a/spec/rubocop/cop/layout/line_length_spec.rb b/spec/rubocop/cop/layout/line_length_spec.rb index 29eb59e098c..42dcaa6e5e9 100644 --- a/spec/rubocop/cop/layout/line_length_spec.rb +++ b/spec/rubocop/cop/layout/line_length_spec.rb @@ -345,7 +345,7 @@ def method_definition_that_is_just_under_the_line_length_limit(foo) # rubocop:di it 'highlights only the non-directive part' do expect_offense(<<~RUBY) #{'a' * 80}bcd # rubocop:enable Style/ClassVars - #{' ' * 80}^^^ Line is too long. [116/80] + #{' ' * 80}^^^ Line is too long. [83/80] RUBY end @@ -353,7 +353,7 @@ def method_definition_that_is_just_under_the_line_length_limit(foo) # rubocop:di it 'highlights only the non-directive part' do expect_offense(<<~RUBY) #{'a' * 70} # bbbbbbbbbbbbbb # rubocop:enable Style/ClassVars' - #{' ' * 70} ^^^^^^^ Line is too long. [121/80] + #{' ' * 70} ^^^^^^^ Line is too long. [87/80] RUBY end end @@ -362,7 +362,7 @@ def method_definition_that_is_just_under_the_line_length_limit(foo) # rubocop:di it 'registers an offense for the line' do expect_offense(<<-RUBY) LARGE_DATA_STRING_PATTERN = %r{\A([A-Za-z0-9\+\/#]*\={0,2})#([A-Za-z0-9\+\/#]*\={0,2})#([A-Za-z0-9\+\/#]*\={0,2})\z} # rubocop:disable Layout/LineLength - #{' ' * 68}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [153/80] + #{' ' * 68}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [117/80] RUBY end end