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

Return parser error on parsing invalid encoding regexp #305

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 1 addition & 3 deletions lib/rubocop/ast/processed_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,13 @@ def parse(source, ruby_version, parser_engine)

begin
@buffer.source = source
@ast, @comments, @tokens = tokenize(create_parser(ruby_version, parser_engine))
rescue EncodingError, Parser::UnknownEncodingInMagicComment => e
@parser_error = e
@ast = nil
@comments = []
@tokens = []
return
end

@ast, @comments, @tokens = tokenize(create_parser(ruby_version, parser_engine))
end

def tokenize(parser)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/ast/processed_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def some_method
end
end

context 'when parsing invalid encoding regexp', broken_on: :prism do
let(:source) { '/あ/n' }

it 'returns a parser error' do
expect(processed_source.parser_error).to be_a(EncodingError)
end
end

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