Skip to content

Commit

Permalink
Formatter: fix for begin ... rescue ... end
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and ysbaddaden committed Jun 26, 2018
1 parent 0004179 commit e74e9ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1147,4 +1147,7 @@ describe Crystal::Formatter do
assert_format "Hash{\n foo => <<-EOF,\n foo\n EOF\n bar => <<-BAR,\n bar\n BAR\n}"
assert_format "Hash{\n foo => <<-EOF\n foo\n EOF\n}"
assert_format "{\n <<-KEY => 1,\n key\n KEY\n}"

assert_format "begin 0[1] rescue 2 end"
assert_format "begin\n 0[1] rescue 2 end", "begin 0[1] rescue 2 end"
end
26 changes: 26 additions & 0 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3691,6 +3691,25 @@ module Crystal
column = @implicit_exception_handler_indent
else
if node.suffix
inline = false

# This is the case of:
#
# begin exp rescue exp end
#
# It's parsed as:
#
# begin (exp rescue exp) end
#
# So it's a suffix rescue inside a begin/end, but the Parser
# returns it as an ExceptionHandler node.
if @token.keyword?(:begin)
inline = true
write_keyword :begin
skip_space_or_newline
write " "
end

accept node.body
passed_backslash_newline = @token.passed_backslash_newline
skip_space
Expand All @@ -3708,6 +3727,13 @@ module Crystal
else
raise "expected 'rescue' or 'ensure'"
end

if inline
skip_space_or_newline
write " "
write_keyword :end
end

return false
end
end
Expand Down

0 comments on commit e74e9ea

Please sign in to comment.