Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 9aab631

Browse files
committed
Handle syntax error in Ripper
Ripper might fail to parse a Ruby source even if the current runtime parsed it properly, because some versions of JRuby have different implementation of runtime parser and Ripper parser. jruby/jruby#2427 So we handle the syntax error in Ripper and fall back to the simple single line extraction in that case.
1 parent eedf9a9 commit 9aab631

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

lib/rspec/core/formatters/snippet_extractor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def expression_lines
5555
end
5656

5757
source.lines[(line_range.begin - 1)..(line_range.end - 1)]
58-
rescue NoExpressionAtLineError
58+
rescue SyntaxError, NoExpressionAtLineError
5959
[self.class.extract_line_at(source.path, beginning_line_number)]
6060
end
6161

lib/rspec/core/source.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def ast
2626
@ast ||= begin
2727
require 'ripper'
2828
sexp = Ripper.sexp(source)
29+
raise SyntaxError unless sexp
2930
Node.new(sexp)
3031
end
3132
end

spec/rspec/core/formatters/snippet_extractor_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,24 @@ def another_expression(*)
250250
end
251251
end
252252

253+
context 'when Ripper cannot parse the source', :isolated_directory do
254+
let(:file_path) do
255+
'invalid_source.rb'
256+
end
257+
258+
let(:line_number) do
259+
1
260+
end
261+
262+
before do
263+
File.write(file_path, 'invalid_source]')
264+
end
265+
266+
it 'returns the line by falling back to the simple single line extraction' do
267+
expect(expression_lines).to eq(['invalid_source]'])
268+
end
269+
end
270+
253271
context 'when max line count is given' do
254272
let(:max_line_count) do
255273
2

0 commit comments

Comments
 (0)