Skip to content

Commit

Permalink
[Fix rubocop#9298] Fix an incorrect auto-correct for `Lint/RedundantC…
Browse files Browse the repository at this point in the history
…opDisableDirective`

Fixes rubocop#9298.

This PR fixes an incorrect auto-correct for `Lint/RedundantCopDisableDirective`
when there is a blank line before inline comment.
  • Loading branch information
koic committed Dec 29, 2020
1 parent 5ec7718 commit 7809e1d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9298](https://github.com/rubocop-hq/rubocop/issues/9298): Fix an incorrect auto-correct for `Lint/RedundantCopDisableDirective` when there is a blank line before inline comment. ([@koic][])
12 changes: 6 additions & 6 deletions lib/rubocop/comment_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def extra_enabled_comments
)
end

def comment_only_line?(line_number)
non_comment_token_line_numbers.none? do |non_comment_line_number|
non_comment_line_number == line_number
end
end

private

def extra_enabled_comments_with_names(extras:, names:)
Expand Down Expand Up @@ -166,12 +172,6 @@ def all_cop_names
@all_cop_names ||= Cop::Registry.global.names - [REDUNDANT_DISABLE]
end

def comment_only_line?(line_number)
non_comment_token_line_numbers.none? do |non_comment_line_number|
non_comment_line_number == line_number
end
end

def non_comment_token_line_numbers
@non_comment_token_line_numbers ||= begin
non_comment_tokens = processed_source.tokens.reject(&:comment?)
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/lint/redundant_cop_disable_directive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def previous_line_blank?(range)
end

def comment_range_with_surrounding_space(range)
if previous_line_blank?(range)
if previous_line_blank?(range) &&
processed_source.comment_config.comment_only_line?(range.line)
# When the previous line is blank, it should be retained
range_with_surrounding_space(range: range, side: :right)
else
Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cop/lint/redundant_cop_disable_directive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,29 @@ class Baz
RUBY
end
end

context 'when there is a blank line before inline comment' do
it 'removes the comment and preceding whitespace' do
expect_offense(<<~RUBY)
def foo; end
def bar # rubocop:disable Metrics/ClassLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/ClassLength`.
do_something do
end
end
RUBY

expect_correction(<<~RUBY)
def foo; end
def bar
do_something do
end
end
RUBY
end
end
end
end
end

0 comments on commit 7809e1d

Please sign in to comment.