-
-
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.
… removing empty `# rubocop:enable` comments. (#8745) Added `RuboCop::DirectiveComment` to encapsulate some logic about determining whether a rubocop directive comment should be removed (if the cops in it are all redundant). Co-authored-by: Bozhidar Batsov <[email protected]>
- Loading branch information
1 parent
b59c48a
commit c21be8f
Showing
8 changed files
with
196 additions
and
16 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
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
# This class wraps the `Parser::Source::Comment` object that represents a | ||
# special `rubocop:disable` and `rubocop:enable` comment and exposes what | ||
# cops it contains. | ||
class DirectiveComment | ||
attr_reader :comment | ||
|
||
def initialize(comment) | ||
@comment = comment | ||
end | ||
|
||
# Return all the cops specified in the directive | ||
def cops | ||
match = comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP) | ||
return unless match | ||
|
||
cops_string = match.captures[1] | ||
cops_string.split(/,\s*/).uniq.sort | ||
end | ||
|
||
# Checks if this directive contains all the given cop names | ||
def match?(cop_names) | ||
cops == cop_names.uniq.sort | ||
end | ||
|
||
def range | ||
comment.location.expression | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::DirectiveComment do | ||
subject(:directive_comment) { described_class.new(comment) } | ||
|
||
let(:comment) { instance_double(Parser::Source::Comment, text: text) } | ||
let(:comment_cop_names) { 'all' } | ||
let(:text) { "#rubocop:enable #{comment_cop_names}" } | ||
|
||
describe '#cops' do | ||
subject(:cops) { directive_comment.cops } | ||
|
||
context 'all' do | ||
let(:comment_cop_names) { 'all' } | ||
|
||
it 'returns [all]' do | ||
expect(cops).to eq(%w[all]) | ||
end | ||
end | ||
|
||
context 'single cop' do | ||
let(:comment_cop_names) { 'Metrics/AbcSize' } | ||
|
||
it 'returns [Metrics/AbcSize]' do | ||
expect(cops).to eq(%w[Metrics/AbcSize]) | ||
end | ||
end | ||
|
||
context 'single cop duplicated' do | ||
let(:comment_cop_names) { 'Metrics/AbcSize,Metrics/AbcSize' } | ||
|
||
it 'returns [Metrics/AbcSize]' do | ||
expect(cops).to eq(%w[Metrics/AbcSize]) | ||
end | ||
end | ||
|
||
context 'multiple cops' do | ||
let(:comment_cop_names) { 'Style/Not, Metrics/AbcSize' } | ||
|
||
it 'returns the cops in alphabetical order' do | ||
expect(cops).to eq(%w[Metrics/AbcSize Style/Not]) | ||
end | ||
end | ||
end | ||
|
||
describe '#match?' do | ||
subject(:match) { directive_comment.match?(cop_names) } | ||
|
||
let(:comment_cop_names) { 'Metrics/AbcSize, Metrics/PerceivedComplexity, Style/Not' } | ||
|
||
context 'no comment_cop_names' do | ||
let(:cop_names) { [] } | ||
|
||
it 'returns false' do | ||
expect(match).to eq(false) | ||
end | ||
end | ||
|
||
context 'same cop names as in the comment' do | ||
let(:cop_names) { %w[Metrics/AbcSize Metrics/PerceivedComplexity Style/Not] } | ||
|
||
it 'returns true' do | ||
expect(match).to eq(true) | ||
end | ||
end | ||
|
||
context 'same cop names as in the comment in a different order' do | ||
let(:cop_names) { %w[Style/Not Metrics/AbcSize Metrics/PerceivedComplexity] } | ||
|
||
it 'returns true' do | ||
expect(match).to eq(true) | ||
end | ||
end | ||
|
||
context 'subset of names' do | ||
let(:cop_names) { %w[Metrics/AbcSize Style/Not] } | ||
|
||
it 'returns false' do | ||
expect(match).to eq(false) | ||
end | ||
end | ||
|
||
context 'superset of names' do | ||
let(:cop_names) { %w[Lint/Void Metrics/AbcSize Metrics/PerceivedComplexity Style/Not] } | ||
|
||
it 'returns false' do | ||
expect(match).to eq(false) | ||
end | ||
end | ||
|
||
context 'duplicate names' do | ||
let(:cop_names) { %w[Metrics/AbcSize Metrics/AbcSize Metrics/PerceivedComplexity Style/Not] } | ||
|
||
it 'returns true' do | ||
expect(match).to eq(true) | ||
end | ||
end | ||
end | ||
end |