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

Commit d439a70

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 d439a70

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ def another_expression(*)
250250
end
251251
end
252252

253+
context 'when Ripper cannot parse the source (which can happen on JRuby -- see jruby/jruby#2427)', :isolated_directory do
254+
let(:file_path) { 'invalid_source.rb' }
255+
let(:line_number) { 1 }
256+
257+
before do
258+
File.write(file_path, <<-EOS.gsub(/^ +\|/, ''))
259+
|expect("some string").to include(
260+
| "some", "string"
261+
|))
262+
EOS
263+
end
264+
265+
it 'returns the line by falling back to the simple single line extraction' do
266+
expect(expression_lines).to eq([
267+
'expect("some string").to include('
268+
])
269+
end
270+
end
271+
253272
context 'when max line count is given' do
254273
let(:max_line_count) do
255274
2

0 commit comments

Comments
 (0)