Skip to content

Commit

Permalink
Handle encoding error from the parser gem
Browse files Browse the repository at this point in the history
This specific exception has been added only in the most recent release.
Previously it threw an `ArgumentError`.
  • Loading branch information
Earlopain authored and marcandre committed Apr 29, 2024
1 parent 4796a1d commit 58290df
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
Empty file added changelog/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions changelog/fix_error_for_magic_encoding_comment.md
@@ -0,0 +1 @@
* [#289](https://github.com/rubocop/rubocop-ast/pull/289): Fix an error during parsing when encountering unknown encodings in the encoding magic comment. ([@Earlopain][])
2 changes: 1 addition & 1 deletion lib/rubocop/ast/processed_source.rb
Expand Up @@ -209,7 +209,7 @@ def parse(source, ruby_version, parser_engine)

begin
@buffer.source = source
rescue EncodingError => e
rescue EncodingError, Parser::UnknownEncodingInMagicComment => e
@parser_error = e
@ast = nil
@comments = []
Expand Down
2 changes: 1 addition & 1 deletion rubocop-ast.gemspec
Expand Up @@ -33,7 +33,7 @@ Gem::Specification.new do |s|
'rubygems_mfa_required' => 'true'
}

s.add_runtime_dependency('parser', '>= 3.3.0.4')
s.add_runtime_dependency('parser', '>= 3.3.1.0')

##### Do NOT add `rubocop` (or anything depending on `rubocop`) here. See Gemfile
end
10 changes: 10 additions & 0 deletions spec/rubocop/ast/processed_source_spec.rb
Expand Up @@ -28,6 +28,16 @@ def some_method
end
end

context 'when parsing code with an invalid encoding comment' do
let(:source) { '# encoding: foobar' }

it 'returns a parser error' do
expect(processed_source.parser_error).to be_a(Parser::UnknownEncodingInMagicComment)
expect(processed_source.parser_error.message)
.to include('unknown encoding name - foobar')
end
end

shared_examples 'invalid parser_engine' do
it 'raises ArgumentError' do
expect { processed_source }.to raise_error(ArgumentError) do |e|
Expand Down

0 comments on commit 58290df

Please sign in to comment.