1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Merge pull request #2117 from barrettkingram/fix-syntax-error-display

Display all syntax error messages when catching SyntaxException
This commit is contained in:
Kyrylo Silin 2020-03-22 00:35:33 +08:00 committed by GitHub
commit e9ad447996
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 6 deletions

View file

@ -627,7 +627,7 @@ class Pry
begin
complete_expr = Pry::Code.complete_expression?(@eval_string)
rescue SyntaxError => e
output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
output.puts e.message.gsub(/^.*syntax error, */, "SyntaxError: ")
reset_eval_string
end

View file

@ -23,17 +23,21 @@ describe Pry do
end
end
([
[
["end"],
["puts )("],
["1 1"],
["puts :"]
] + [
["puts :"],
# in this case the syntax error is "expecting ')'".
["def", "method(1"],
# in this case the syntax error is "expecting keyword_end".
["o = Object.new.tap{ def o.render;", "'MEH'", "}"]
]).compact.each do |foo|
["o = Object.new.tap{ def o.render;", "'MEH'", "}"],
# multiple syntax errors reported in one SyntaxException
["puts {'key'=>'val'}.to_json"]
].compact.each do |foo|
it "should raise an error on invalid syntax like #{foo.inspect}" do
redirect_pry_io(InputTester.new(*foo), @str_output) do
Pry.start
@ -41,6 +45,20 @@ describe Pry do
expect(@str_output.string).to match(/SyntaxError/)
end
it "should display correct number of errors on invalid syntax like #{foo.inspect}" do
begin
# rubocop:disable Security/Eval
eval(foo.join("\n"))
# rubocop:enable Security/Eval
rescue SyntaxError => e
error_count = e.message.scan(/syntax error/).count
end
expect(error_count).not_to be_nil
pry_output = mock_pry(*foo)
expect(pry_output.scan(/SyntaxError/).count).to eq(error_count)
end
end
it "should not intefere with syntax errors explicitly raised" do