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

Assoc uses Identity formatter if any value is nil #450

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1784,17 +1784,21 @@ def format_key(q, key)
end

def self.for(container)
# First check for assocs where the value is nil; that means it has been
# omitted. In this case we have to match the existing formatting because
# standardizing would potentially break the code. For example:
#
# { first:, "second" => "value" }
#
container.assocs.each do |assoc|
if assoc.value.nil?
return Identity.new
end
end

container.assocs.each do |assoc|
if assoc.is_a?(AssocSplat)
# Splat nodes do not impact the formatting choice.
elsif assoc.value.nil?
# If the value is nil, then it has been omitted. In this case we have
# to match the existing formatting because standardizing would
# potentially break the code. For example:
#
# { first:, "second" => "value" }
#
return Identity.new
else
# Otherwise, we need to check the type of the key. If it's a label or
# dynamic symbol, we can use labels. If it's a symbol literal then it
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/assoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@
{ "foo #{bar}": "baz" }
%
{ "foo=": "baz" }
%
{ bar => 1, baz: }
%
{ baz:, bar => 1 }