Skip to content

Commit

Permalink
[Fix rubocop#8590] Fix an error when auto-correcting encoding mismatc…
Browse files Browse the repository at this point in the history
…h file

Fixes rubocop#8590.

This PR fixes the following error when auto-correcting encoding mismatch file.

```ruby
% cat example.rb
# encoding: Shift_JIS

puts 'This file encoding is UTF-8.'
```

## Before

```console
% bundle exec rubocop --cache false -a
(snip)

Cannot extract source from uninitialized Source::Buffer
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/parser-2.7.1.4/lib/
parser/source/buffer.rb:148:in `source'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/parser-2.7.1.4/lib/
parser/source/buffer.rb:297:in `source_range'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/parser-2.7.1.4/lib/
parser/source/tree_rewriter.rb:116:in `initialize'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/corrector.rb:18:in
`initialize'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/team.rb:182:in
`new'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/team.rb:182:in
`collate_corrections'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/team.rb:176:in
`autocorrect_report'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/team.rb:121:in
`autocorrect'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/team.rb:79:in
`investigate'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:295:in `inspect_file'
```

## After

```console
% bundle exec rubocop --cache false -a
(snip)

Inspecting 1 file
F

Offenses:

example.rb:1:1: F: Lint/Syntax: "\xef\xbc" from shift_jis to utf-8.

1 file inspected, 1 offense detected
```
  • Loading branch information
koic committed Aug 26, 2020
1 parent dea6296 commit 7315ad0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,7 @@
* [#8518](https://github.com/rubocop-hq/rubocop/issues/8518): Fix `Lint/ConstantResolution` cop reporting offense for `module` and `class` definitions. ([@tejasbubane][])
* [#8158](https://github.com/rubocop-hq/rubocop/issues/8158): Fix `Style/MultilineWhenThen` cop to correctly handle cases with multiline body. ([@dsavochkin][])
* [#7705](https://github.com/rubocop-hq/rubocop/issues/7705): Fix `Style/OneLineConditional` cop to handle if/then/elsif/then/else/end cases. Add `AlwaysCorrectToMultiline` config option to this cop to always convert offenses to the multi-line form (false by default). ([@Lykos][], [@dsavochkin][])
* [#8590](https://github.com/rubocop-hq/rubocop/issues/8590): Fix an error when auto-correcting encoding mismatch file. ([@koic][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/team.rb
Expand Up @@ -117,6 +117,7 @@ def external_dependency_checksum
def autocorrect(processed_source, report)
@updated_source_file = false
return unless autocorrect?
return if report.processed_source.parser_error

new_source = autocorrect_report(report)

Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/team_spec.rb
Expand Up @@ -156,6 +156,24 @@ def a
end
end

context 'when autocorrection is enabled and file encoding is mismatch' do
let(:options) { { auto_correct: true } }

before do
create_file(file_path, <<~RUBY)
# encoding: Shift_JIS
puts 'This file encoding is UTF-8.'
RUBY
end

it 'no error occurs' do
source = RuboCop::ProcessedSource.from_file(file_path, ruby_version)
team.inspect_file(source)

expect(team.errors.empty?).to be(true)
end
end

context 'when Cop#on_* raises an error' do
include_context 'mock console output'
before do
Expand Down

0 comments on commit 7315ad0

Please sign in to comment.