Skip to content

Commit

Permalink
Replace useless public DirectiveComment#cops with private method
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Eres authored and bbatsov committed Mar 17, 2021
1 parent c91b89b commit 80ea6a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 46 deletions.
19 changes: 9 additions & 10 deletions lib/rubocop/directive_comment.rb
Expand Up @@ -21,18 +21,11 @@ def self.before_comment(line)
line.split(DIRECTIVE_COMMENT_REGEXP).first
end

attr_reader :comment
attr_reader :comment, :mode, :cops

def initialize(comment)
@comment = comment
end

# Return all the cops specified in the directive
def cops
return unless match_captures

cops_string = match_captures[1]
cops_string.split(/,\s*/).uniq.sort
@mode, @cops = match_captures
end

# Checks if this directive relates to single line
Expand All @@ -42,7 +35,7 @@ def single_line?

# Checks if this directive contains all the given cop names
def match?(cop_names)
cops == cop_names.uniq.sort
parsed_cop_names.uniq.sort == cop_names.uniq.sort
end

def range
Expand All @@ -53,5 +46,11 @@ def range
def match_captures
@match_captures ||= comment.text.match(DIRECTIVE_COMMENT_REGEXP)&.captures
end

private

def parsed_cop_names
(cops || '').split(/,\s*/)
end
end
end
45 changes: 9 additions & 36 deletions spec/rubocop/directive_comment_spec.rb
Expand Up @@ -22,42 +22,6 @@
end
end

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) }

Expand Down Expand Up @@ -110,6 +74,15 @@
expect(match).to eq(true)
end
end

context 'all' do
let(:comment_cop_names) { 'all' }
let(:cop_names) { %w[all] }

it 'returns true' do
expect(match).to eq(true)
end
end
end

describe '#match_captures' do
Expand Down

0 comments on commit 80ea6a5

Please sign in to comment.