Skip to content

Commit

Permalink
Improve error message when passing wrong parser_engine and accept s…
Browse files Browse the repository at this point in the history
…trings
  • Loading branch information
Earlopain authored and marcandre committed Mar 8, 2024
1 parent 2514755 commit e424be0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/rubocop/ast/processed_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def self.from_file(path, ruby_version, parser_engine: :parser_whitequark)
end

def initialize(source, ruby_version, path = nil, parser_engine: :parser_whitequark)
parser_engine = parser_engine.to_sym
unless PARSER_ENGINES.include?(parser_engine)
raise ArgumentError, 'The keyword argument `parser_engine` accepts ' \
"`parser` or `parser_prism`, but `#{parser_engine}` was passed."
raise ArgumentError, 'The keyword argument `parser_engine` accepts `parser_whitequark` ' \
"or `parser_prism`, but `#{parser_engine}` was passed."
end

# Defaults source encoding to UTF-8, regardless of the encoding it has
Expand Down
23 changes: 17 additions & 6 deletions spec/rubocop/ast/processed_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,27 @@ def some_method
end
end

context 'when using invalid `parser_engine` argument' do
let(:parser_engine) { :unknown_parser_engine }

it 'raises a Errno::ENOENT when the file does not exist' do
shared_examples 'invalid parser_engine' do
it 'raises ArgumentError' do
expect { processed_source }.to raise_error(ArgumentError) do |e|
expect(e.message).to eq 'The keyword argument `parser_engine` accepts `parser` or ' \
'`parser_prism`, but `unknown_parser_engine` was passed.'
expected = 'The keyword argument `parser_engine` accepts `parser_whitequark` ' \
"or `parser_prism`, but `#{parser_engine}` was passed."
expect(e.message).to eq(expected)
end
end
end

context 'when using an invalid `parser_engine` symbol argument' do
let(:parser_engine) { :unknown_parser_engine }

it_behaves_like 'invalid parser_engine'
end

context 'when using an invalid `parser_engine` string argument' do
let(:parser_engine) { 'unknown_parser_engine' }

it_behaves_like 'invalid parser_engine'
end
end

describe '.from_file' do
Expand Down

0 comments on commit e424be0

Please sign in to comment.