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

Handle encoding error from the parser gem #289

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added changelog/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions changelog/fix_error_for_magic_encoding_comment.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading