Skip to content

Commit

Permalink
Merge pull request #329 from ruby-formatter/fix-327
Browse files Browse the repository at this point in the history
Fix #327
  • Loading branch information
kzkn authored Feb 8, 2025
2 parents c1da4f2 + 31a41c2 commit a85215e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/rufo/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2209,14 +2209,14 @@ def visit_array(node)

_, elements = node

token_column = current_token_column
base_column = @column

check :on_lbracket
write "["
next_token

if elements
visit_literal_elements to_ary(elements), inside_array: true, token_column: token_column
visit_literal_elements to_ary(elements), inside_array: true, token_column: base_column
else
skip_space_or_newline
end
Expand Down Expand Up @@ -2326,7 +2326,7 @@ def visit_q_or_i_array(node)
def visit_hash(node)
# [:hash, elements]
_, elements = node
token_column = current_token_column
base_column = @column

closing_brace_token, _ = find_closing_brace_token
need_space = need_space_for_hash?(node, node[1], closing_brace_token)
Expand All @@ -2340,7 +2340,7 @@ def visit_hash(node)
if elements
# [:assoclist_from_args, elements]
push_hash(node) do
visit_literal_elements(elements[1], inside_hash: true, token_column: token_column)
visit_literal_elements(elements[1], inside_hash: true, token_column: base_column)
end
char_after_brace = @output[brace_position + 1]
# Check that need_space is set correctly.
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/rufo/formatter_source_specs/array_literal.rb.spec
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,15 @@ x = [{
*[a],
*[b],
]

#~# ORIGINAL
[
"bar",
"foo",
]

#~# EXPECTED
[
"bar",
"foo",
]
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@

#~# ORIGINAL

a = 1 ||
a = 1 ||
2

#~# EXPECTED
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/rufo/formatter_source_specs/hash_literal.rb.spec
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,15 @@
{
:'' => 5,
}

#~# ORIGINAL issue_327
{
foo: "bar",
bar: "foo",
}

#~# EXPECTED
{
foo: "bar",
bar: "foo",
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ x 1, "foo" \
#~# EXPECTED
x 1, "foo" \
"bar"

#~# ORIGINAL

"foo" \
"bar"

#~# EXPECTED
"foo" \
"bar"

0 comments on commit a85215e

Please sign in to comment.