Skip to content

Commit

Permalink
Merge pull request #10078 from dvandersluis/layout/line-length-withou…
Browse files Browse the repository at this point in the history
…t-directive

Fix `Layout/LineLength` reported length when ignoring directive comments
  • Loading branch information
koic committed Sep 14, 2021
2 parents 1fd77b0 + b1f4871 commit 35d3540
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 35d3540

Please sign in to comment.