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 Layout/LineLength reported length when ignoring directive comments #10078

Merged
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_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