Skip to content

Commit

Permalink
Merge pull request #6584 from roooodcastro/add-ignore-disable-comments
Browse files Browse the repository at this point in the history
[Fix #6150] Add runtime option to ignore cop disable comments
  • Loading branch information
pocke authored Jan 15, 2019
2 parents 7abc4ba + d054c0f commit ad65d4f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#6604](https://github.com/rubocop-hq/rubocop/pull/6604): Add auto-correct support to `Rails/LinkToBlank`. ([@Intrepidd][])
* [#6660](https://github.com/rubocop-hq/rubocop/pull/6660): Add new `Rails/IgnoredSkipActionFilterOption` cop. ([@wata727][])
* [#6363](https://github.com/rubocop-hq/rubocop/issues/6363): Allow `Style/YodaCondition` cop to be configured to enforce yoda conditions. ([@tejasbubane][])
* [#6150](https://github.com/rubocop-hq/rubocop/issues/6150): Add support to enforce disabled cops to be executed. ([@roooodcastro][])
* [#6596](https://github.com/rubocop-hq/rubocop/pull/6596): Add new `Rails/BelongsTo` cop with auto-correct for Rails >= 5. ([@petehamilton][])

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def file_name_matches_any?(file, parameter, default_result)
end

def enabled_line?(line_number)
return true unless @processed_source
return true if @options[:ignore_disable_comments] || !@processed_source

@processed_source.comment_config.cop_enabled_at_line?(self, line_number)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def add_boolean_flags(opts)
option(opts, '-S', '--display-style-guide')
option(opts, '-R', '--rails')
option(opts, '-a', '--auto-correct')
option(opts, '--ignore-disable-comments')

option(opts, '--safe')

Expand Down Expand Up @@ -375,6 +376,8 @@ module OptionsHelp
force_exclusion: ['Force excluding files specified in the',
'configuration `Exclude` even if they are',
'explicitly passed as arguments.'],
ignore_disable_comments: ['Run cops even when they are disabled locally',
'with a comment.'],
ignore_parent_exclusion: ['Prevent from inheriting AllCops/Exclude from',
'parent folders.'],
force_default_config: ['Use default configuration even if configuration',
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/cop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@
.to output(/Warning: Invalid severity 'superbad'./).to_stderr
end

it 'will set offense as disabled if ignore_disable_comments is false' do
comment_config = instance_double(RuboCop::CommentConfig,
cop_enabled_at_line?: false)
processed_source = instance_double(RuboCop::ProcessedSource,
comment_config: comment_config)
cop.processed_source = processed_source
cop.instance_variable_set(:@options, ignore_disable_comments: false)
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.status).to eq :disabled
end

it 'will not set offense as disabled if ignore_disable_comments is true' do
comment_config = instance_double(RuboCop::CommentConfig,
cop_enabled_at_line?: false)
processed_source = instance_double(RuboCop::ProcessedSource,
comment_config: comment_config)
cop.processed_source = processed_source
cop.instance_variable_set(:@options, ignore_disable_comments: true)
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.status).not_to eq :disabled
end

it 'registers offense with its name' do
cop = RuboCop::Cop::Style::For.new
cop.add_offense(nil, location: location, message: 'message')
Expand Down
2 changes: 2 additions & 0 deletions spec/rubocop/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def abs(path)
-S, --display-style-guide Display style guide URLs in offense messages.
-R, --rails Run extra Rails cops.
-a, --auto-correct Auto-correct offenses.
--ignore-disable-comments Run cops even when they are disabled locally
with a comment.
--safe Run only safe cops.
--[no-]color Force color output on or off.
-v, --version Display version.
Expand Down

0 comments on commit ad65d4f

Please sign in to comment.