Skip to content

Commit

Permalink
Merge pull request #2382 from jonas054/2003_unneeded_disable_better_s…
Browse files Browse the repository at this point in the history
…olution

[Fix #2003] Handle --auto-correct in Lint/UnneededDisable
  • Loading branch information
bbatsov committed Nov 1, 2015
2 parents 5a6058d + 1eef61c commit 37f191b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* [#2321](https://github.com/bbatsov/rubocop/issues/2321): In `Style/EachWithObject`, don't replace reduce with each_with_object if the accumulator parameter is assigned to in the block. ([@alexdowad][])
* [#1981](https://github.com/bbatsov/rubocop/issues/1981): `Lint/UselessAssignment` doesn't erroneously identify assignments in identical if branches as useless. ([@alexdowad][])
* [#2323](https://github.com/bbatsov/rubocop/issues/2323): `Style/IfUnlessModifier` cop parenthesizes autocorrected code when necessary due to operator precedence, to avoid changing its meaning. ([@alexdowad][])
* [#2003](https://github.com/bbatsov/rubocop/issues/2003): Make `Lint/UnneededDisable` work with `--auto-correct`. ([@jonas054][])

## 0.34.2 (21/09/2015)

Expand Down
19 changes: 15 additions & 4 deletions lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ def process_file(file)
file_started(file, disabled_line_ranges, comments)
else
processed_source = get_processed_source(file)
file_started(file, processed_source.disabled_line_ranges,
processed_source.comments)
offenses = do_inspection_loop(file, processed_source)
# Use delegators for objects sent to the formatters. These can be
# updated when the file is re-inspected.
disabled_line_ranges =
SimpleDelegator.new(processed_source.disabled_line_ranges)
comments = SimpleDelegator.new(processed_source.comments)

file_started(file, disabled_line_ranges, comments)
offenses = do_inspection_loop(file, processed_source,
disabled_line_ranges, comments)
save_in_cache(cache, offenses, processed_source)
end

Expand Down Expand Up @@ -129,7 +135,8 @@ def save_in_cache(cache, offenses, processed_source)
processed_source.comments)
end

def do_inspection_loop(file, processed_source)
def do_inspection_loop(file, processed_source, disabled_line_ranges,
comments)
offenses = []

# Keep track of the state of the source. If a cop modifies the source
Expand All @@ -156,6 +163,10 @@ def do_inspection_loop(file, processed_source)
break unless updated_source_file

processed_source = get_processed_source(file)

# Update delegators with new objects.
disabled_line_ranges.__setobj__(processed_source.disabled_line_ranges)
comments.__setobj__(processed_source.comments)
end

offenses
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,26 @@ def interrupt
''].join("\n"))
end

context 'when --auto-correct is given' do
it 'does not trigger UnneededDisable due to lines moving around' do
src = ['a = 1 # rubocop:disable Lint/UselessAssignment']
create_file('example.rb', src)
create_file('.rubocop.yml', ['Style/Encoding:',
' Enabled: true'])
expect(cli.run(['--format', 'offenses', '-a', 'example.rb'])).to eq(0)
expect($stdout.string).to eq(['',
'1 Style/Encoding',
'--',
'1 Total',
'',
''].join("\n"))
expect(IO.read('example.rb'))
.to eq(['# encoding: utf-8',
'a = 1 # rubocop:disable Lint/UselessAssignment',
''].join("\n"))
end
end

it 'can disable selected cops in a code section' do
create_file('example.rb',
['# encoding: utf-8',
Expand Down

0 comments on commit 37f191b

Please sign in to comment.