Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #8590] Fix an error when auto-correcting encoding mismatch file #8591

Merged
merged 1 commit into from Aug 26, 2020

Conversation

koic
Copy link
Member

@koic koic commented Aug 26, 2020

Fixes #8590.

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

% cat example.rb
# encoding: Shift_JIS

puts 'This file encoding is UTF-8.'

Before

% 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

% 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

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

…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
```
Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@koic koic merged commit e3c930d into rubocop:master Aug 26, 2020
@koic koic deleted the fix_an_error_when_autocorrecting branch August 26, 2020 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--auto-correct stops with error when parsing invalid encoding file
2 participants