Skip to content
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

Format: fix to format NamedTuple with trailing comma #4697

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Expand Up @@ -97,6 +97,11 @@ describe Crystal::Formatter do
assert_format "Foo( x: Int32 )", "Foo(x: Int32)"
assert_format "Foo( x: Int32 , y: Float64 )", "Foo(x: Int32, y: Float64)"

assert_format "NamedTuple(a: Int32,)", "NamedTuple(a: Int32)"
assert_format "NamedTuple(\n a: Int32,\n)"
assert_format "NamedTuple(\n a: Int32,)", "NamedTuple(\n a: Int32,\n)"
assert_format "class Foo\n NamedTuple(\n a: Int32,\n )\nend"

assert_format "::Tuple(T)"
assert_format "::NamedTuple(T)"
assert_format "::Pointer(T)"
Expand Down
13 changes: 11 additions & 2 deletions src/compiler/crystal/tools/formatter.cr
Expand Up @@ -1075,13 +1075,22 @@ module Crystal
skip_space_or_newline

check_open_paren
skip_space_or_newline

paren_count = @paren_count

if named_args = node.named_args
format_named_args([] of ASTNode, named_args, @indent)
has_newlines, _, _ = format_named_args([] of ASTNode, named_args, @indent + 2)
# `format_named_args` doesn't skip trailing comma
if @paren_count == paren_count && @token.type == :","
next_token_skip_space_or_newline
if has_newlines
write ","
write_line
write_indent
end
end
else
skip_space_or_newline
node.type_vars.each_with_index do |type_var, i|
accept type_var
if @paren_count == paren_count
Expand Down