Skip to content

Commit

Permalink
Merge pull request #58 from zombocom/consider-unexpected-local-variab…
Browse files Browse the repository at this point in the history
…le-or-method

Consider if syntax error caused an unexpected variable instead of end
  • Loading branch information
schneems committed Feb 25, 2021
2 parents bcdcdb9 + 66ac998 commit 34f7e01
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
## HEAD (unreleased)

- Consider if syntax error caused an unexpected variable instead of end (https://github.com/zombocom/dead_end/pull/58)

## 1.1.5

- Parse error once and not twice if there's more than one available (https://github.com/zombocom/dead_end/pull/57)

## 1.1.4
Expand Down
14 changes: 7 additions & 7 deletions lib/dead_end/who_dis_syntax_error.rb
Expand Up @@ -51,14 +51,14 @@ def on_parse_error(msg)
when /unexpected end-of-input/
@error_symbol = :missing_end
when /expecting end-of-input/
@error_symbol = :unmatched_syntax
@unmatched_symbol = :end
when /unexpected `end'/, # Ruby 2.7 and 3.0
/unexpected end/, # Ruby 2.6
/unexpected keyword_end/i # Ruby 2.5

match = @error.match(/expecting '(?<unmatched_symbol>.*)'/)
@unmatched_symbol = match[:unmatched_symbol].to_sym if match
@error_symbol = :unmatched_syntax
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
/unexpected end/, # Ruby 2.6
/unexpected keyword_end/i # Ruby 2.5

@error_symbol = :unmatched_syntax
else
Expand Down
52 changes: 36 additions & 16 deletions spec/unit/who_dis_syntax_error_spec.rb
Expand Up @@ -18,25 +18,45 @@ module DeadEnd
).to eq(:end)
end

it "determines the type of syntax error to be an unmatched pipe" do
source = <<~EOM
class Blerg
Foo.call do |a
end # one
context "determines the type of syntax error to be an unmatched pipe" do
it "with unexpected 'end'" do
source = <<~EOM
class Blerg
Foo.call do |a
end # one
puts lol
class Foo
end # two
end # three
EOM
puts lol
class Foo
end # two
end # three
EOM

expect(
DeadEnd.invalid_type(source).error_symbol
).to eq(:unmatched_syntax)
expect(
DeadEnd.invalid_type(source).error_symbol
).to eq(:unmatched_syntax)

expect(
DeadEnd.invalid_type(source).unmatched_symbol
).to eq(:|)
expect(
DeadEnd.invalid_type(source).unmatched_symbol
).to eq(:|)
end

it "with unexpected local variable or method" do
source = <<~EOM
class Blerg
[].each do |a
puts a
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

it "determines the type of syntax error to be an unmatched bracket" do
Expand Down

0 comments on commit 34f7e01

Please sign in to comment.