Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 44 additions & 3 deletions test/fixtures/aryptn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions test/fixtures/hshptn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@
in bar, { baz:, **nil }
in qux:
end
-
case foo
in [bar, { baz:, **nil }]
in qux:
end
6 changes: 4 additions & 2 deletions test/fixtures/in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
end
-
case foo
in fooooooooooooooooooooooooooooooooooooo,
barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
in [
fooooooooooooooooooooooooooooooooooooo,
barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
]
baz
end
%
Expand Down