Skip to content

Commit 9529fff

Browse files
committed
Consider if syntax error caused an unexpected variable instead of end
`tIDENTIFIER` in Ruby 2.6 and `local variable or method` in Ruby 2.7 and 3.0
1 parent bcdcdb9 commit 9529fff

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

lib/dead_end/who_dis_syntax_error.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ def on_parse_error(msg)
5353
when /expecting end-of-input/
5454
@error_symbol = :unmatched_syntax
5555
@unmatched_symbol = :end
56-
when /unexpected `end'/, # Ruby 2.7 and 3.0
57-
/unexpected end/, # Ruby 2.6
58-
/unexpected keyword_end/i # Ruby 2.5
56+
when /unexpected `end'/, # Ruby 2.7 and 3.0
57+
/unexpected local variable/, #
58+
/unexpected end/, # Ruby 2.6
59+
/unexpected tIDENTIFIER/, #
60+
/unexpected keyword_end/i # Ruby 2.5
5961

6062
match = @error.match(/expecting '(?<unmatched_symbol>.*)'/)
6163
@unmatched_symbol = match[:unmatched_symbol].to_sym if match

spec/unit/who_dis_syntax_error_spec.rb

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,45 @@ module DeadEnd
1818
).to eq(:end)
1919
end
2020

21-
it "determines the type of syntax error to be an unmatched pipe" do
22-
source = <<~EOM
23-
class Blerg
24-
Foo.call do |a
25-
end # one
21+
context "determines the type of syntax error to be an unmatched pipe" do
22+
it "with unexpected 'end'" do
23+
source = <<~EOM
24+
class Blerg
25+
Foo.call do |a
26+
end # one
2627
27-
puts lol
28-
class Foo
29-
end # two
30-
end # three
31-
EOM
28+
puts lol
29+
class Foo
30+
end # two
31+
end # three
32+
EOM
3233

33-
expect(
34-
DeadEnd.invalid_type(source).error_symbol
35-
).to eq(:unmatched_syntax)
34+
expect(
35+
DeadEnd.invalid_type(source).error_symbol
36+
).to eq(:unmatched_syntax)
3637

37-
expect(
38-
DeadEnd.invalid_type(source).unmatched_symbol
39-
).to eq(:|)
38+
expect(
39+
DeadEnd.invalid_type(source).unmatched_symbol
40+
).to eq(:|)
41+
end
42+
43+
it "with unexpected local variable or method" do
44+
source = <<~EOM
45+
class Blerg
46+
[].each do |a
47+
puts a
48+
end
49+
end
50+
EOM
51+
52+
expect(
53+
DeadEnd.invalid_type(source).error_symbol
54+
).to eq(:unmatched_syntax)
55+
56+
expect(
57+
DeadEnd.invalid_type(source).unmatched_symbol
58+
).to eq(:|)
59+
end
4060
end
4161

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

0 commit comments

Comments
 (0)