From eb264abb152e559cf021ee93d5e3ab9bc60caa0a Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 May 2022 12:06:55 -0400 Subject: [PATCH] Always use [] with aryptn --- lib/syntax_tree/node.rb | 32 ++++++---------------------- test/fixtures/aryptn.rb | 47 ++++++++++++++++++++++++++++++++++++++--- test/fixtures/hshptn.rb | 5 +++++ test/fixtures/in.rb | 6 ++++-- 4 files changed, 60 insertions(+), 30 deletions(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 6c2617cc..1e8afa4c 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -1123,38 +1123,20 @@ def deconstruct_keys(_keys) end def format(q) - parts = [*requireds] - parts << RestFormatter.new(rest) if rest - parts += posts - - if constant - q.group do - q.format(constant) - q.text("[") - q.indent do - q.breakable("") - q.seplist(parts) { |part| q.format(part) } - end - q.breakable("") - q.text("]") - end - - return - end - - parent = q.parent - if parts.length == 1 || PATTERNS.include?(parent.class) + q.group do + q.format(constant) if constant q.text("[") q.indent do q.breakable("") + + parts = [*requireds] + parts << RestFormatter.new(rest) if rest + parts += posts + q.seplist(parts) { |part| q.format(part) } end q.breakable("") q.text("]") - elsif parts.empty? - q.text("[]") - else - q.group { q.seplist(parts) { |part| q.format(part) } } end end end diff --git a/test/fixtures/aryptn.rb b/test/fixtures/aryptn.rb index c5562305..eddd8e3f 100644 --- a/test/fixtures/aryptn.rb +++ b/test/fixtures/aryptn.rb @@ -6,51 +6,92 @@ case foo in _, _ end +- +case foo +in [_, _] +end % case foo in bar, baz end +- +case foo +in [bar, baz] +end % case foo in [bar] end % case foo -in [bar, baz] +in [bar] +in [baz] end -- +% case foo -in bar, baz +in [bar, baz] end % case foo in bar, *baz end +- +case foo +in [bar, *baz] +end % case foo in *bar, baz end +- +case foo +in [*bar, baz] +end % case foo in bar, *, baz end +- +case foo +in [bar, *, baz] +end % case foo in *, bar, baz end +- +case foo +in [*, bar, baz] +end % case foo in Constant[bar] end % case foo +in Constant(bar) +end +- +case foo +in Constant[bar] +end +% +case foo in Constant[bar, baz] end % case foo in bar, [baz, _] => qux end +- +case foo +in [bar, [baz, _] => qux] +end % case foo in bar, baz if bar == baz end +- +case foo +in [bar, baz] if bar == baz +end diff --git a/test/fixtures/hshptn.rb b/test/fixtures/hshptn.rb index 7a35b4d0..f8733170 100644 --- a/test/fixtures/hshptn.rb +++ b/test/fixtures/hshptn.rb @@ -71,3 +71,8 @@ in bar, { baz:, **nil } in qux: end +- +case foo +in [bar, { baz:, **nil }] +in qux: +end diff --git a/test/fixtures/in.rb b/test/fixtures/in.rb index 1e1b2282..59102505 100644 --- a/test/fixtures/in.rb +++ b/test/fixtures/in.rb @@ -14,8 +14,10 @@ end - case foo -in fooooooooooooooooooooooooooooooooooooo, - barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr +in [ + fooooooooooooooooooooooooooooooooooooo, + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr + ] baz end %