diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cf3ed1..92491ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## HEAD (unreleased) +- Handle mismatched end when using rescue without begin (https://github.com/zombocom/dead_end/pull/83) - CLI returns non-zero exit code when syntax error is found (https://github.com/zombocom/dead_end/pull/86) - Let -v respond with gem version instead of 'unknown' (https://github.com/zombocom/dead_end/pull/82) diff --git a/lib/dead_end/who_dis_syntax_error.rb b/lib/dead_end/who_dis_syntax_error.rb index a421bc9..d4770f5 100644 --- a/lib/dead_end/who_dis_syntax_error.rb +++ b/lib/dead_end/who_dis_syntax_error.rb @@ -58,7 +58,7 @@ def on_parse_error(msg) when /expecting end-of-input/ @unmatched_symbol = :end @error_symbol = :unmatched_syntax - when /unexpected .* expecting '(?.*)'/ + when /unexpected .* expecting ['`]?(?[^']*)/ @unmatched_symbol = $1.to_sym if $1 @error_symbol = :unmatched_syntax when /unexpected `end'/, # Ruby 2.7 and 3.0 diff --git a/spec/unit/who_dis_syntax_error_spec.rb b/spec/unit/who_dis_syntax_error_spec.rb index e72e611..732e96d 100644 --- a/spec/unit/who_dis_syntax_error_spec.rb +++ b/spec/unit/who_dis_syntax_error_spec.rb @@ -4,18 +4,41 @@ module DeadEnd RSpec.describe WhoDisSyntaxError do - it "determines the type of syntax error to be an unmatched end" do - expect( - WhoDisSyntaxError.new("def foo;").call.error_symbol - ).to eq(:missing_end) + context "determines the type of syntax error to be an unmatched end" do + it "with missing or extra end's" do + expect( + WhoDisSyntaxError.new("def foo;").call.error_symbol + ).to eq(:missing_end) - expect( - WhoDisSyntaxError.new("def foo; end; end").call.error_symbol - ).to eq(:unmatched_syntax) + expect( + WhoDisSyntaxError.new("def foo; end; end").call.error_symbol + ).to eq(:unmatched_syntax) - expect( - WhoDisSyntaxError.new("def foo; end; end").call.unmatched_symbol - ).to eq(:end) + expect( + WhoDisSyntaxError.new("def foo; end; end").call.unmatched_symbol + ).to eq(:end) + end + + it "with unexpected rescue" do + source = <<~EOM + def foo + if bar + "baz" + else + "foo" + rescue FooBar + nil + end + EOM + + expect( + WhoDisSyntaxError.new(source).call.error_symbol + ).to eq(:unmatched_syntax) + + expect( + WhoDisSyntaxError.new(source).call.unmatched_symbol + ).to eq(:end) + end end context "determines the type of syntax error to be an unmatched pipe" do