Skip to content

Commit

Permalink
Showing 3 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
@@ -465,6 +465,7 @@ describe Crystal::Formatter do
assert_format "begin\n1\nrescue Int32 \n3\nend", "begin\n 1\nrescue Int32\n 3\nend"
assert_format "if 1\nbegin\n2\nensure\n3\nend\nend", "if 1\n begin\n 2\n ensure\n 3\n end\nend"
assert_format "1 rescue 2"
assert_format "1 ensure 2"

assert_format "def foo\n1\nrescue\n2\nend", "def foo\n 1\nrescue\n 2\nend"
assert_format "def foo\n1\nensure\n2\nend", "def foo\n 1\nensure\n 2\nend"
4 changes: 2 additions & 2 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
@@ -261,9 +261,9 @@ module Crystal
next_token_skip_space
ensure_body = parse_expression
if atomic.is_a?(Assign)
atomic.value = ExceptionHandler.new(atomic.value, ensure: ensure_body).at(location)
atomic.value = ExceptionHandler.new(atomic.value, ensure: ensure_body).at(location).tap { |e| e.suffix = true }
else
atomic = ExceptionHandler.new(atomic, ensure: ensure_body).at(location)
atomic = ExceptionHandler.new(atomic, ensure: ensure_body).at(location).tap { |e| e.suffix = true }
end
when :ifdef
next_token_skip_statement_end
17 changes: 14 additions & 3 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
@@ -3304,9 +3304,20 @@ module Crystal
if node.suffix
accept node.body
skip_space
write " rescue "
next_token_skip_space_or_newline
accept node.rescues.not_nil!.first.not_nil!.body
write " "
if @token.keyword?(:rescue)
write_keyword :rescue
write " "
next_token_skip_space_or_newline
accept node.rescues.not_nil!.first.not_nil!.body
elsif @token.keyword?(:ensure)
write_keyword :ensure
write " "
next_token_skip_space_or_newline
accept node.ensure.not_nil!
else
raise "expected 'rescue' or 'ensure'"
end
return false
end
end

0 comments on commit b34aed4

Please sign in to comment.