Skip to content

Commit

Permalink
[Fix rubocop#3191] Allow arbitrary comments after cop names in Commen…
Browse files Browse the repository at this point in the history
…tConfig lines (rubocop#3201)
  • Loading branch information
owst authored and Neodelf committed Oct 15, 2016
1 parent f9cfd90 commit 06fd4de
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [#3105](https://github.com/bbatsov/rubocop/issues/3105): Add new `Style/RequestReferer` cop. ([@giannileggio][])
* [#3200](https://github.com/bbatsov/rubocop/pull/3200): Add autocorrect for `Style/EachForSimpleLoop` cop. ([@tejasbubane][])
* [#3179](https://github.com/bbatsov/rubocop/pull/3179): Expose files to support testings Cops using RSpec. ([@tjwp][])
* [#3191](https://github.com/bbatsov/rubocop/issues/3191): Allow arbitrary comments after cop names in CommentConfig lines (e.g. rubocop:enable). ([@owst][])

### Bug fixes

Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/comment_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ module RuboCop
# and provides a way to check if each cop is enabled at arbitrary line.
class CommentConfig
UNNEEDED_DISABLE = 'Lint/UnneededDisable'.freeze

COP_NAME_PATTERN = '([A-Z][a-z]+/)?(?:[A-Z][a-z]+)+'.freeze
COP_NAMES_PATTERN = "(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}".freeze
COPS_PATTERN = "(all|#{COP_NAMES_PATTERN})".freeze

COMMENT_DIRECTIVE_REGEXP = Regexp.new(
'\A# rubocop : ((?:dis|en)able)\b ((?:[\w/]+,? )+)'.gsub(' ', '\s*')
('\A# rubocop : ((?:dis|en)able)\b ' + COPS_PATTERN).gsub(' ', '\s*')
)

attr_reader :processed_source
Expand Down
50 changes: 42 additions & 8 deletions spec/rubocop/comment_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[
'# encoding: utf-8',
'',
'# rubocop:disable Metrics/MethodLength',
'# rubocop:disable Metrics/MethodLength with a comment why',
'def some_method',
" puts 'foo'", # 5
'end',
Expand All @@ -38,7 +38,7 @@
'',
'# rubocop:enable Lint/Void',
'',
'# rubocop:disable Style/For',
'# rubocop:disable Style/For, Style/Not,Style/Tab',
'foo', # 30
'',
'class One',
Expand All @@ -49,7 +49,15 @@
'class Two',
' # rubocop:disable Style/ClassVars',
' @@class_var = 2',
'end' # 40
'end', # 40
'# rubocop:enable Style/Not,Style/Tab',
'# rubocop:disable Style/Send, Lint/RandOne some comment why',
'# rubocop:disable Lint/BlockAlignment some comment why',
'# rubocop:enable Style/Send, Lint/BlockAlignment but why?',
'# rubocop:enable Lint/RandOne foo bar!', # 45
'# rubocop:disable FlatMap',
'[1, 2, 3, 4].map { |e| [e, e] }.flatten(1)',
'# rubocop:enable FlatMap'
]
end

Expand All @@ -64,15 +72,41 @@ def disabled_lines_of_cop(cop)
method_length_disabled_lines =
disabled_lines_of_cop('Metrics/MethodLength')
expected_part = (3..6).to_a
expect(method_length_disabled_lines & expected_part)
.to eq(expected_part)
expect(method_length_disabled_lines & expected_part).to eq(expected_part)
end

it 'supports enabling/disabling multiple cops in a single directive' do
not_disabled_lines = disabled_lines_of_cop('Style/Not')
tab_disabled_lines = disabled_lines_of_cop('Style/Tab')

expect(not_disabled_lines).to eq(tab_disabled_lines)
expected_part = (29..41).to_a
expect(not_disabled_lines & expected_part).to eq(expected_part)
end

it 'supports enabling/disabling multiple cops along with a comment' do
{
'Style/Send' => 42..44,
'Lint/RandOne' => 42..45,
'Lint/BlockAlignment' => 43..44
}.each do |cop_name, expected|
actual = disabled_lines_of_cop(cop_name)
expect(actual & expected.to_a).to eq(expected.to_a)
end
end

it 'supports enabling/disabling cops without a prefix' do
flat_map_disabled_lines = disabled_lines_of_cop('Performance/FlatMap')

expected = (46..48).to_a

expect(flat_map_disabled_lines & expected).to eq(expected)
end

it 'supports disabling all lines after a directive' do
for_disabled_lines = disabled_lines_of_cop('Style/For')
expected_part = (29..source.size).to_a
expect(for_disabled_lines & expected_part)
.to eq(expected_part)
expect(for_disabled_lines & expected_part).to eq(expected_part)
end

it 'just ignores unpaired enabling directives' do
Expand Down Expand Up @@ -120,7 +154,7 @@ def disabled_lines_of_cop(cop)

it 'can handle double disable of one cop' do
expect(disabled_lines_of_cop('Style/ClassVars'))
.to eq([9, 10, 11] + (33..40).to_a)
.to eq([9, 10, 11] + (33..source.size).to_a)
end
end
end

0 comments on commit 06fd4de

Please sign in to comment.