Skip to content

Commit

Permalink
Fix heredoc start + comma formatter (#6222)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and RX14 committed Jun 25, 2018
1 parent e4a01b5 commit 3e794db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 7 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Expand Up @@ -1140,4 +1140,11 @@ describe Crystal::Formatter do
assert_format "foo do\n bar do\n foo <<-X\n bar\n X\n end\nend"
assert_format "call(foo <<-X\nbar\nX\n)"
assert_format "bar do\n call(foo <<-X\n bar\n X\n )\nend"

assert_format "[\n <<-EOF,\n foo\n EOF\n]"
assert_format "[\n <<-EOF,\n foo\n EOF\n <<-BAR,\n bar\n BAR\n]"
assert_format "Hash{\n foo => <<-EOF,\n foo\n EOF\n}"
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}"
end
14 changes: 10 additions & 4 deletions src/compiler/crystal/tools/formatter.cr
Expand Up @@ -623,7 +623,7 @@ module Crystal
next_token

@string_continuation = old_string_continuation
@indent = old_indent
@indent = old_indent unless is_heredoc

false
end
Expand Down Expand Up @@ -828,17 +828,23 @@ module Crystal
accept element
end

has_heredoc_in_line = !@lexer.heredocs.empty?

last = last?(i, elements)

found_comment = skip_space(offset, write_comma: last && has_newlines)
found_comment = skip_space(offset, write_comma: (last || has_heredoc_in_line) && has_newlines)

if @token.type == :","
write "," unless last || found_comment
if !found_comment && (!last || has_heredoc_in_line)
write ","
wrote_comma = true
end

slash_is_regex!
next_token
found_comment = skip_space(offset, write_comma: last && has_newlines)
if @token.type == :NEWLINE
if last && !found_comment
if last && !found_comment && !wrote_comma
write ","
found_comment = true
end
Expand Down

0 comments on commit 3e794db

Please sign in to comment.