Skip to content

Commit

Permalink
Merge pull request #83 from zombocom/mauro-oto/handle-mismatched-end-…
Browse files Browse the repository at this point in the history
…on-rescue-without-begin

Handle mismatched end when using rescue without begin
  • Loading branch information
schneems authored Oct 13, 2021
2 parents 698e178 + 088ff88 commit 619eeef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/dead_end/who_dis_syntax_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def on_parse_error(msg)
when /expecting end-of-input/
@unmatched_symbol = :end
@error_symbol = :unmatched_syntax
when /unexpected .* expecting '(?<unmatched_symbol>.*)'/
when /unexpected .* expecting ['`]?(?<unmatched_symbol>[^']*)/
@unmatched_symbol = $1.to_sym if $1
@error_symbol = :unmatched_syntax
when /unexpected `end'/, # Ruby 2.7 and 3.0
Expand Down
43 changes: 33 additions & 10 deletions spec/unit/who_dis_syntax_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 619eeef

Please sign in to comment.