forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix rubocop#5356] Add new Lint/UnneededCopEnableDirective cop
- Loading branch information
1 parent
44c3ba0
commit f306205
Showing
6 changed files
with
216 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
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,77 @@ | ||
# frozen_string_literal: true | ||
|
||
# The Lint/UnneededCopEnableDirective cop needs to be disabled so as | ||
# to be able to provide a (bad) example of an unneeded enable. | ||
|
||
# rubocop:disable Lint/UnneededCopEnableDirective | ||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop detects instances of rubocop:enable comments that can be | ||
# removed. | ||
# | ||
# @example | ||
# # bad | ||
# foo = 1 | ||
# # rubocop:enable Metrics/LineLength | ||
# | ||
# # good | ||
# foo = 1 | ||
class UnneededCopEnableDirective < Cop | ||
include RangeHelp | ||
|
||
MSG = 'Unnecessary enabling of %<cop>s.'.freeze | ||
|
||
def investigate(processed_source) | ||
return if processed_source.blank? | ||
offenses = processed_source.comment_config.extra_enabled_comments | ||
offenses.each do |comment, name| | ||
add_offense( | ||
[comment, name], | ||
location: range_of_offense(comment, name), | ||
message: format(MSG, cop: name) | ||
) | ||
end | ||
end | ||
|
||
def autocorrect(comment_and_name) | ||
lambda do |corrector| | ||
comment, name = *comment_and_name | ||
range = range_of_offense(*comment_and_name) | ||
index = cop_name_indention(comment, name) | ||
make_corrections(corrector, comment, name, range, index) | ||
end | ||
end | ||
|
||
private | ||
|
||
def range_of_offense(comment, name) | ||
comment_start = comment.loc.expression.begin_pos | ||
offense_start = comment_start + cop_name_indention(comment, name) | ||
range_between(offense_start, offense_start + name.size) | ||
end | ||
|
||
def cop_name_indention(comment, name) | ||
comment.text.index(name) | ||
end | ||
|
||
# rubocop:disable Metrics/AbcSize | ||
def make_corrections(corrector, comment, name, range, index) | ||
if comment.text[index - 2] == ',' | ||
corrector.remove( | ||
range_between(range.begin_pos - 2, range.end_pos) | ||
) | ||
elsif comment.text[index + name.size] == ',' | ||
corrector.remove( | ||
range_between(range.begin_pos, range.end_pos + 2) | ||
) | ||
else | ||
corrector.remove(comment.loc.expression) | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize | ||
end | ||
end | ||
end | ||
end | ||
# rubocop:enable Lint/UnneededCopEnableDirective |
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
109 changes: 109 additions & 0 deletions
109
spec/rubocop/cop/lint/unneeded_cop_enable_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,109 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::UnneededCopEnableDirective do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers offense for unnecessary enable' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
foo | ||
# rubocop:enable Metrics/LineLength | ||
^^^^^^^^^^^^^^^^^^ Unnecessary enabling of Metrics/LineLength. | ||
RUBY | ||
end | ||
|
||
it 'registers multiple offenses for same comment' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
foo | ||
# rubocop:enable Metrics/ModuleLength, Metrics/AbcSize | ||
^^^^^^^^^^^^^^^ Unnecessary enabling of Metrics/AbcSize. | ||
^^^^^^^^^^^^^^^^^^^^ Unnecessary enabling of Metrics/ModuleLength. | ||
bar | ||
RUBY | ||
end | ||
|
||
it 'registers correct offense when combined with necessary enable' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
# rubocop:disable Metrics/LineLength | ||
fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = barrrrrrrrrrrrrrrrrrrrrrrrrr | ||
# rubocop:enable Metrics/AbcSize, Metrics/LineLength | ||
^^^^^^^^^^^^^^^ Unnecessary enabling of Metrics/AbcSize. | ||
bar | ||
RUBY | ||
end | ||
|
||
context 'autocorrection' do | ||
context 'when entire comment unnecessarily enables' do | ||
let(:source) do | ||
<<-RUBY.strip_indent | ||
foo | ||
# rubocop:enable Metrics/LineLength | ||
RUBY | ||
end | ||
|
||
it 'removes unnecessary enables' do | ||
corrected = autocorrect_source(source) | ||
expect(corrected).to eq(<<-RUBY.strip_indent) | ||
foo | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when first cop unnecessarily enables' do | ||
let(:source) do | ||
<<-RUBY.strip_indent | ||
# rubocop:disable Metrics/LineLength | ||
foo | ||
# rubocop:enable Metrics/AbcSize, Metrics/LineLength | ||
RUBY | ||
end | ||
|
||
it 'removes unnecessary enables' do | ||
corrected = autocorrect_source(source) | ||
expect(corrected).to eq(<<-RUBY.strip_indent) | ||
# rubocop:disable Metrics/LineLength | ||
foo | ||
# rubocop:enable Metrics/LineLength | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when last cop unnecessarily enables' do | ||
let(:source) do | ||
<<-RUBY.strip_indent | ||
# rubocop:disable Metrics/LineLength | ||
foo | ||
# rubocop:enable Metrics/LineLength, Metrics/AbcSize | ||
RUBY | ||
end | ||
|
||
it 'removes unnecessary enables' do | ||
corrected = autocorrect_source(source) | ||
expect(corrected).to eq(<<-RUBY.strip_indent) | ||
# rubocop:disable Metrics/LineLength | ||
foo | ||
# rubocop:enable Metrics/LineLength | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when middle cop unnecessarily enables' do | ||
let(:source) do | ||
<<-RUBY.strip_indent | ||
# rubocop:disable Metrics/LineLength, Lint/Debugger | ||
foo | ||
# rubocop:enable Metrics/LineLength, Metrics/AbcSize, Lint/Debugger | ||
RUBY | ||
end | ||
|
||
it 'removes unnecessary enables' do | ||
corrected = autocorrect_source(source) | ||
expect(corrected).to eq(<<-RUBY.strip_indent) | ||
# rubocop:disable Metrics/LineLength, Lint/Debugger | ||
foo | ||
# rubocop:enable Metrics/LineLength, Lint/Debugger | ||
RUBY | ||
end | ||
end | ||
end | ||
end |