Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7384 from egze/disable_cops_within_source_code_di…
…rective implement Style/DisableCopsWithinSourceCodeDirective cop
- Loading branch information
Showing
11 changed files
with
112 additions
and
143 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
49 changes: 49 additions & 0 deletions
49
lib/rubocop/cop/style/disable_cops_within_source_code_directive.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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Lint/RedundantCopDisableDirective | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Detects comments to enable/disable RuboCop. | ||
# This is useful if want to make sure that every RuboCop error gets fixed | ||
# and not quickly disabled with a comment. | ||
# | ||
# @example | ||
# # bad | ||
# # rubocop:disable Metrics/AbcSize | ||
# def f | ||
# end | ||
# # rubocop:enable Metrics/AbcSize | ||
# | ||
# # good | ||
# def fixed_method_name_and_no_rubocop_comments | ||
# end | ||
# | ||
class DisableCopsWithinSourceCodeDirective < Cop | ||
# rubocop:enable Lint/RedundantCopDisableDirective | ||
MSG = 'Comment to disable/enable RuboCop.' | ||
|
||
def investigate(processed_source) | ||
processed_source.comments.each do |comment| | ||
next unless rubocop_directive_comment?(comment) | ||
|
||
add_offense(comment) | ||
end | ||
end | ||
|
||
def autocorrect(comment) | ||
lambda do |corrector| | ||
corrector.replace(comment.loc.expression, '') | ||
end | ||
end | ||
|
||
private | ||
|
||
def rubocop_directive_comment?(comment) | ||
comment.text =~ CommentConfig::COMMENT_DIRECTIVE_REGEXP | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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
29 changes: 29 additions & 0 deletions
29
spec/rubocop/cop/style/disable_cops_within_source_code_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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::DisableCopsWithinSourceCodeDirective do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense for disabled cop within source code' do | ||
expect_offense(<<~RUBY) | ||
def choose_move(who_to_move)# rubocop:disable Metrics/CyclomaticComplexity | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Comment to disable/enable RuboCop. | ||
end | ||
RUBY | ||
expect_correction(<<~RUBY) | ||
def choose_move(who_to_move) | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for enabled cop within source code' do | ||
expect_offense(<<~RUBY) | ||
def choose_move(who_to_move)# rubocop:enable Metrics/CyclomaticComplexity | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Comment to disable/enable RuboCop. | ||
end | ||
RUBY | ||
expect_correction(<<~RUBY) | ||
def choose_move(who_to_move) | ||
end | ||
RUBY | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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