From 338eb2f3c6f47ca28e152d8bfb55c4df1fb04fee Mon Sep 17 00:00:00 2001 From: Mauro Otonelli Date: Tue, 12 Oct 2021 23:58:15 -0300 Subject: [PATCH 1/2] Handle mismatched end when using rescue without begin Handles cases where mismatched ends happens on methods using rescue without `begin`: ``` unexpected `rescue', expecting `end' ``` --- lib/dead_end/who_dis_syntax_error.rb | 2 +- spec/unit/who_dis_syntax_error_spec.rb | 43 ++++++++++++++++++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/dead_end/who_dis_syntax_error.rb b/lib/dead_end/who_dis_syntax_error.rb index a421bc9..dec9513 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 From a0629c255ea96d6fbf12a0f337625522f9a04f50 Mon Sep 17 00:00:00 2001 From: Mauro Otonelli Date: Wed, 13 Oct 2021 00:36:17 -0300 Subject: [PATCH 2/2] Adjust regex to support 2.6 --- CHANGELOG.md | 1 + lib/dead_end/who_dis_syntax_error.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9df9727..2f9dba0 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) - Let -v respond with gem version instead of 'unknown' (https://github.com/zombocom/dead_end/pull/82) ## 2.0.0 diff --git a/lib/dead_end/who_dis_syntax_error.rb b/lib/dead_end/who_dis_syntax_error.rb index dec9513..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