From 9fdd6e66e62c7ef0f01697974eb013987abf9ce7 Mon Sep 17 00:00:00 2001 From: Mauro Otonelli Date: Wed, 10 Feb 2021 00:38:36 -0300 Subject: [PATCH] Parse error once and not twice if there's more than one available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a Ruby stacktrace includes two syntax errors, like below: ``` ➜ dead_end git:(main) ✗ ruby dog.rb dog.rb:5: syntax error, unexpected `end', expecting ']' end dog.rb:10: syntax error, unexpected end-of-input, expecting `end' ``` We should parse only the first time we encounter errors, and not the second time. If we know the type of error and unmatched symbol, we shouldn't need to check those again. --- CHANGELOG.md | 2 ++ lib/dead_end/who_dis_syntax_error.rb | 2 ++ spec/unit/who_dis_syntax_error_spec.rb | 27 ++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a25f14..830ee7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## HEAD (unreleased) +- Parse error once and not twice if there's more than one available (https://github.com/zombocom/dead_end/pull/57) + ## 1.1.4 - Avoid including demo gif in built gem (https://github.com/zombocom/dead_end/pull/53) diff --git a/lib/dead_end/who_dis_syntax_error.rb b/lib/dead_end/who_dis_syntax_error.rb index 4f5e360..98bd3b4 100644 --- a/lib/dead_end/who_dis_syntax_error.rb +++ b/lib/dead_end/who_dis_syntax_error.rb @@ -42,6 +42,8 @@ def call end def on_parse_error(msg) + return if @error_symbol && @unmatched_symbol + @error = msg @unmatched_symbol = :unknown diff --git a/spec/unit/who_dis_syntax_error_spec.rb b/spec/unit/who_dis_syntax_error_spec.rb index b23d55f..d2415e6 100644 --- a/spec/unit/who_dis_syntax_error_spec.rb +++ b/spec/unit/who_dis_syntax_error_spec.rb @@ -4,7 +4,7 @@ module DeadEnd RSpec.describe WhoDisSyntaxError do - it "determines the type of syntax error" 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) @@ -18,7 +18,7 @@ module DeadEnd ).to eq(:end) end - it "" do + it "determines the type of syntax error to be an unmatched pipe" do source = <<~EOM class Blerg Foo.call do |a @@ -38,5 +38,28 @@ class Foo DeadEnd.invalid_type(source).unmatched_symbol ).to eq(:|) end + + it "determines the type of syntax error to be an unmatched bracket" do + source = <<~EOM + module Hey + class Foo + def initialize + [1,2,3 + end + + def call + end + end + end + EOM + + expect( + DeadEnd.invalid_type(source).error_symbol + ).to eq(:unmatched_syntax) + + expect( + DeadEnd.invalid_type(source).unmatched_symbol + ).to eq(:"]") + end end end