Skip to content

Commit

Permalink
fix bug for constant pattern
Browse files Browse the repository at this point in the history
ref: #325
  • Loading branch information
kzkn committed Sep 1, 2024
1 parent a1dd6c5 commit 5a35acc
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 32 deletions.
65 changes: 33 additions & 32 deletions lib/rufo/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3084,30 +3084,52 @@ def visit_array_pattern(node)
_, const_ref, pre_rest, rest, post_rest = node

if const_ref
return visit_constant_pattern(node)
visit const_ref
end

paren_type = if current_token_kind == :on_lparen
:paren
elsif current_token_kind == :on_lbracket
:bracket
end

# pattern is [*]
if !pre_rest && !post_rest && rest == [:var_field, nil]
consume_token :on_lbracket
case paren_type
when :paren
consume_token :on_lparen
when :bracket
consume_token :on_lbracket
end

skip_space_or_newline
consume_op "*"
skip_space_or_newline
consume_token :on_rbracket

case paren_type
when :paren
consume_token :on_rparen
when :bracket
consume_token :on_rbracket
end
return
end

token_column = current_token_column

has_bracket = current_token_kind == :on_lbracket
if has_bracket
consume_token :on_lbracket
if paren_type
case paren_type
when :paren
consume_token :on_lparen
when :bracket
consume_token :on_lbracket
end
skip_space
end

write_comma = false
if pre_rest
visit_literal_elements pre_rest, inside_array: true, token_column: token_column, keep_final_newline: !has_bracket
visit_literal_elements pre_rest, inside_array: true, token_column: token_column, keep_final_newline: !paren_type
write_comma = true
end

Expand All @@ -3133,35 +3155,14 @@ def visit_array_pattern(node)
consume_space
next_token

visit_literal_elements post_rest, inside_array: true, token_column: token_column, keep_final_newline: !has_bracket
end

skip_space
if has_bracket
consume_token :on_rbracket
end
end

def visit_constant_pattern(node)
# [:aryptn, const_ref, args]
_, const_ref, args = node

visit const_ref

parens = current_token_kind == :on_lparen
if parens
consume_token :on_lparen
else
consume_token :on_lbracket
visit_literal_elements post_rest, inside_array: true, token_column: token_column, keep_final_newline: !paren_type
end

skip_space
visit_comma_separated_list args

skip_space
if parens
case paren_type
when :paren
consume_token :on_rparen
else
when :bracket
consume_token :on_rbracket
end
end
Expand Down
112 changes: 112 additions & 0 deletions spec/lib/rufo/formatter_source_specs/pattern_matching.rb.spec
Original file line number Diff line number Diff line change
Expand Up @@ -796,3 +796,115 @@ case x
in { "a": 1 }
1
end

#~# ORIGINAL issue_325
case "baz"
in Foo[*data] # This splat is what causes the crash
puts "foo"
else
puts "bar"
end

#~# EXPECTED
case "baz"
in Foo[*data] # This splat is what causes the crash
puts "foo"
else
puts "bar"
end

#~# ORIGINAL
case x
in Foo[a, ]
1
end

#~# EXPECTED
case x
in Foo[a, ]
1
end

#~# ORIGINAL
case x
in Foo[ a, * ]
1
end

#~# EXPECTED
case x
in Foo[a, *]
1
end

#~# ORIGINAL
case x
in Foo[ a, *b ]
1
end

#~# EXPECTED
case x
in Foo[a, *b]
1
end

#~# ORIGINAL
case x
in Foo[ *a,b,*c ]
1
end

#~# EXPECTED
case x
in Foo[*a, b, *c]
1
end

#~# ORIGINAL
case x
in Foo[ *,a,* ]
1
end

#~# EXPECTED
case x
in Foo[*, a, *]
1
end

#~# ORIGINAL
case x
in Foo[ a,*b,c ]
1
end

#~# EXPECTED
case x
in Foo[a, *b, c]
1
end

#~# ORIGINAL
case x
in Foo[ * ]
1
end

#~# EXPECTED
case x
in Foo[*]
1
end

#~# ORIGINAL
case x
in Foo( *)
1
end

#~# EXPECTED
case x
in Foo(*)
1
end

0 comments on commit 5a35acc

Please sign in to comment.