diff --git a/lib/rufo/formatter.rb b/lib/rufo/formatter.rb index ccd24755..31e9268c 100644 --- a/lib/rufo/formatter.rb +++ b/lib/rufo/formatter.rb @@ -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 @@ -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 diff --git a/spec/lib/rufo/formatter_source_specs/pattern_matching.rb.spec b/spec/lib/rufo/formatter_source_specs/pattern_matching.rb.spec index f0169bc2..89f02a45 100644 --- a/spec/lib/rufo/formatter_source_specs/pattern_matching.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/pattern_matching.rb.spec @@ -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