Skip to content

Commit

Permalink
[Fix rubocop#12034] Fix an error when using an invalid encoding string
Browse files Browse the repository at this point in the history
Fixes rubocop#12034.

This PR fixes invalid byte sequence in UTF-8 error when using an invalid encoding string.
  • Loading branch information
koic committed Jul 8, 2023
1 parent 1a13d7a commit a27433a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12034](https://github.com/rubocop/rubocop/issues/12034): Fix invalid byte sequence in UTF-8 error when using an invalid encoding string. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/frozen_string_literal_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def last_special_comment(processed_source)
end

next_token = processed_source.tokens[token_number]
token = next_token if Encoding::ENCODING_PATTERN.match?(next_token&.text)
if next_token&.text&.valid_encoding? && Encoding::ENCODING_PATTERN.match?(next_token.text)
token = next_token
end

token
end
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@
end
end

context 'when using an invalid encoding string' do
it 'does not crash and reports an offense' do
create_file('example.rb', <<~'RUBY')
"\xE3\xD3\x8B\xE3\x83\xBC\x83\xE3\x83\xE3\x82\xB3\xA3\x82\x99"
RUBY
result = cli.run(['--format', 'simple', 'example.rb'])
expect(result).to eq(1)
expect($stdout.string)
.to eq(<<~RESULT)
== example.rb ==
C: 1: 1: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
1 file inspected, 1 offense detected, 1 offense autocorrectable
RESULT
expect($stderr.string).to eq ''
end
end

context 'when checking a correct file' do
it 'returns 0' do
create_file('example.rb', <<~RUBY)
Expand Down

0 comments on commit a27433a

Please sign in to comment.