Skip to content

Commit

Permalink
Merge pull request rubocop#915 from jonas054/improve_only_option
Browse files Browse the repository at this point in the history
Make --only enable the given cop
  • Loading branch information
bbatsov committed Mar 23, 2014
2 parents d24a1eb + f32bd4a commit 4803b54
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* [#912](https://github.com/bbatsov/rubocop/issues/912): Fix a false positive in `LineEndConcatenation` for `%` string literals. ([@bbatsov][])
* [#912](https://github.com/bbatsov/rubocop/issues/912): Handle top-level constant resolution in `DeprecatedClassMethods` (e.g. `::File.exists?`). ([@bbatsov][])
* [#914](https://github.com/bbatsov/rubocop/issues/914): Fixed rdoc error during gem installation. ([@bbatsov][])
* The `--only` option now enables the given cop in case it is disabled in configuration. ([@jonas054][])

## 0.19.1 (17/03/2014)

Expand Down
12 changes: 9 additions & 3 deletions lib/rubocop/cop/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,21 @@ def inspect_file(file)

def cops
@cops ||= begin
@cop_classes.reduce([]) do |instances, cop_class|
next instances unless @config.cop_enabled?(cop_class)
instances << cop_class.new(@config, @options)
@cop_classes.each_with_object([]) do |cop_class, instances|
if cop_enabled?(cop_class)
instances << cop_class.new(@config, @options)
end
end
end
end

private

def cop_enabled?(cop_class)
@config.cop_enabled?(cop_class) ||
cop_class.cop_name == @options[:only]
end

def autocorrect(buffer, cops)
@updated_source_file = false
return unless autocorrect?
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,27 @@ def abs(path)
'1 file inspected, 1 offense detected',
''].join("\n"))
end

it 'enables the given cop' do
create_file('example.rb', ['x = 0 ',
# Disabling comments still apply.
'# rubocop:disable TrailingWhitespace',
'y = 1 '])

create_file('.rubocop.yml', ['TrailingWhitespace:',
' Enabled: false'])

expect(cli.run(['--format', 'simple',
'--only', 'TrailingWhitespace',
'example.rb'])).to eq(1)
expect($stderr.string).to eq('')
expect($stdout.string)
.to eq(['== example.rb ==',
'C: 1: 6: Trailing whitespace detected.',
'',
'1 file inspected, 1 offense detected',
''].join("\n"))
end
end

describe '--lint' do
Expand Down

0 comments on commit 4803b54

Please sign in to comment.