diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 3b676552..ca4efddb 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -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 diff --git a/test/fixtures/assoc.rb b/test/fixtures/assoc.rb index 0fc60e6f..df8bb938 100644 --- a/test/fixtures/assoc.rb +++ b/test/fixtures/assoc.rb @@ -48,3 +48,7 @@ { "foo #{bar}": "baz" } % { "foo=": "baz" } +% +{ bar => 1, baz: } +% +{ baz:, bar => 1 }