Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6e6feaf

Browse files
committed
Assoc uses Identity formatter if any value is nil
The existing assoc formatter has logic to identify the case where avalue is nil (in a Ruby-3.1 style hash) and preserve the existingformatting. For example: `{ first:, "second" => "value" }` is correctlyleft as-is.However, this logic only worked if the first assoc in thecontainer had the nil value - if a later assoc had a nil value, theIdentity formatter might not be chosen which could cause the formatterto generate invalid Ruby code.As an example, this code: `{ "first" => "value", second: }` would beturned into `{ "first" => "value", :second => }`.This patch pulls the nil value check up to the top of`HashKeyFormatter.for` to ensure it takes precendence over any otherformatting selections. The fixtures have been updated to cover bothcases (nil value in first position, nil value in last position).Fixes#446
1 parent8dfae19 commit6e6feaf

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

‎lib/syntax_tree/node.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,9 @@ def ===(other)
15531553

15541554
defformat_contents(q)
15551555
(q.parent ||HashKeyFormatter::Identity.new).format_key(q,key)
1556+
1557+
# TODO: if value is +nil+ but we have chosen Rocket formatting, we need to format key as a value
1558+
15561559
returnunlessvalue
15571560

15581561
ifkey.comments.empty? &&AssignFormatting.skip_indent?(value)
@@ -1784,17 +1787,21 @@ def format_key(q, key)
17841787
end
17851788

17861789
defself.for(container)
1790+
# First check for assocs where the value is nil; that means it has been
1791+
# omitted. In this case we have to match the existing formatting because
1792+
# standardizing would potentially break the code. For example:
1793+
#
1794+
# { first:, "second" => "value" }
1795+
#
1796+
container.assocs.eachdo |assoc|
1797+
ifassoc.value.nil?
1798+
returnIdentity.new
1799+
end
1800+
end
1801+
17871802
container.assocs.eachdo |assoc|
17881803
ifassoc.is_a?(AssocSplat)
17891804
# Splat nodes do not impact the formatting choice.
1790-
elsifassoc.value.nil?
1791-
# If the value is nil, then it has been omitted. In this case we have
1792-
# to match the existing formatting because standardizing would
1793-
# potentially break the code. For example:
1794-
#
1795-
# { first:, "second" => "value" }
1796-
#
1797-
returnIdentity.new
17981805
else
17991806
# Otherwise, we need to check the type of the key. If it's a label or
18001807
# dynamic symbol, we can use labels. If it's a symbol literal then it

‎test/fixtures/assoc.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@
4848
{ "foo#{bar}": "baz" }
4949
%
5050
{ "foo=": "baz" }
51+
%
52+
{ bar => 1, baz: }
53+
%
54+
{ baz:, bar => 1 }

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp