Skip to content

Commit

Permalink
Fix Layout/LineLength reported length when ignoring directive comme…
Browse files Browse the repository at this point in the history
…nts.

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.
  • Loading branch information
dvandersluis committed Sep 13, 2021
1 parent eac703f commit b1f4871
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions 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][])
12 changes: 7 additions & 5 deletions lib/rubocop/cop/layout/line_length.rb
Expand Up @@ -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]

Expand Down Expand Up @@ -241,17 +241,19 @@ 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,
line_index + 1,
range
),
line,
line_index
line_index,
length: length_without_directive
)
end

Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/cop/layout/line_length_spec.rb
Expand Up @@ -345,15 +345,15 @@ 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

context 'and the source contains non-directive # as comment' do
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
Expand All @@ -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
Expand Down

0 comments on commit b1f4871

Please sign in to comment.