-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Style/DoubleCopDisableDirective cop
This cop is the easiest solution to the problem of `--disable-uncorrectable` generating lines with more than one `rubocop:disable` comment.
- Loading branch information
Showing
7 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Lint/UnneededCopDisableDirective | ||
# rubocop:disable Style/DoubleCopDisableDirective | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Detects double disable comments on one line. This is mostly to catch | ||
# automatically generated comments that need to be regenerated. | ||
# | ||
# @example | ||
# # bad | ||
# def f # rubocop:disable Style/For # rubocop:disable Metrics/AbcSize | ||
# end | ||
# | ||
# # good | ||
# # rubocop:disable Metrics/AbcSize | ||
# def f # rubocop:disable Style/For | ||
# end | ||
# # rubocop:enable Metrics/AbcSize | ||
# | ||
# # if both fit on one line | ||
# def f # rubocop:disable Style/For, Metrics/AbcSize | ||
# end | ||
# | ||
class DoubleCopDisableDirective < Cop | ||
# rubocop:enable Style/For, Style/DoubleCopDisableDirective | ||
# rubocop:enable Lint/UnneededCopDisableDirective, Metrics/AbcSize | ||
MSG = 'More than one disable comment on one line.' | ||
|
||
def investigate(processed_source) | ||
processed_source.comments.each do |comment| | ||
next unless comment.text.scan('# rubocop:disable').size > 1 | ||
|
||
add_offense(comment) | ||
end | ||
end | ||
|
||
def autocorrect(comment) | ||
lambda do |corrector| | ||
corrector.replace(comment.loc.expression, | ||
comment.text[/# rubocop:disable \S+/]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
spec/rubocop/cop/style/double_cop_disable_directive_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::DoubleCopDisableDirective do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when using `#bad_method`' do | ||
expect_offense(<<~RUBY) | ||
def choose_move(who_to_move) # rubocop:disable Metrics/CyclomaticComplexity # rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/MethodLength | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ More than one disable comment on one line. | ||
end | ||
RUBY | ||
expect_correction(<<~RUBY) | ||
def choose_move(who_to_move) # rubocop:disable Metrics/CyclomaticComplexity | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#good_method`' do | ||
expect_no_offenses(<<~RUBY) | ||
def choose_move(who_to_move) # rubocop:disable Metrics/CyclomaticComplexity | ||
end | ||
RUBY | ||
end | ||
end |