Skip to content

Raise ParseError on missing end for else statement #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,11 @@ def on_else(statements)
token.is_a?(Kw) && %w[end ensure].include?(token.value)
end

if index.nil?
message = "Cannot find expected else ending"
raise ParseError.new(message, *find_token_error(keyword.location))
end

node = tokens[index]
ending = node.value == "end" ? tokens.delete_at(index) : node

Expand Down
10 changes: 10 additions & 0 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,15 @@ def test_errors_on_missing_token_without_location
def test_handles_strings_with_non_terminated_embedded_expressions
assert_raises(Parser::ParseError) { SyntaxTree.parse('"#{"') }
end

def test_errors_on_else_missing_two_ends
assert_raises(Parser::ParseError) { SyntaxTree.parse(<<~RUBY) }
def foo
if something
else
call do
end
RUBY
end
end
end