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

Commit f00ec60

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 f00ec60

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,28 @@ 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+
256+
let(:line_number) { 1 }
257+
258+
let(:source) { <<-EOS.gsub(/^ +\|/, '') }
259+
|expect("some string").to include(
260+
| "some", "string"
261+
|]
262+
EOS
263+
264+
before do
265+
File.open(file_path, 'w') { |file| file.write(source) }
266+
end
267+
268+
it 'returns the line by falling back to the simple single line extraction' do
269+
expect(expression_lines).to eq([
270+
'expect("some string").to include('
271+
])
272+
end
273+
end
274+
253275
context 'when max line count is given' do
254276
let(:max_line_count) do
255277
2

0 commit comments

Comments
 (0)