Skip to content

Handle return when arg is an Array #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 22, 2022
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
61 changes: 34 additions & 27 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2068,13 +2068,19 @@ def format(q)
if part.is_a?(Paren)
if part.contents.body.length == 1 && skip_parens?(part.contents.body.first)
q.text(" ")
q.format(part.contents.body.first)
contents = part.contents.body.first
contents = contents.contents if contents.is_a?(ArrayLiteral)
q.format(contents)
else
q.format(arguments)
end
elsif part.is_a?(ArrayLiteral)
q.text(" ")
q.format(arguments)
if part.contents && part.contents.parts.length > 1
q.format(part.contents)
else
q.format(arguments)
end
else
format_arguments(q, "(", ")")
end
Expand All @@ -2087,6 +2093,7 @@ def format(q)

private


def format_arguments(q, opening, closing)
q.if_break { q.text(opening) }
q.indent do
Expand All @@ -2099,7 +2106,7 @@ def format_arguments(q, opening, closing)

def skip_parens?(node)
case node
in Int | FloatLiteral
in Int | FloatLiteral | ArrayLiteral
true
in VarRef[value: GVar | IVar | CVar | Kw | Const]
true
Expand Down Expand Up @@ -2523,7 +2530,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -3808,7 +3815,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -3838,7 +3845,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -3870,7 +3877,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -5311,7 +5318,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -6436,7 +6443,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -6536,7 +6543,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -6597,7 +6604,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand All @@ -6622,7 +6629,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -6693,7 +6700,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ beginning: beginning, parts: parts, location: location }
end
Expand Down Expand Up @@ -6726,7 +6733,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -6760,7 +6767,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -7246,7 +7253,7 @@ def initialize(value:, location:)
@value = value
@location = location
end

def accept(visitor)
visitor.visit_rparen(self)
end
Expand All @@ -7256,7 +7263,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -7511,7 +7518,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ parts: parts, location: location }
end
Expand Down Expand Up @@ -7808,7 +7815,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -7838,7 +7845,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -7969,7 +7976,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -7998,7 +8005,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -8028,7 +8035,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -8139,7 +8146,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -8217,7 +8224,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -9242,7 +9249,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ value: value, location: location }
end
Expand Down Expand Up @@ -9271,7 +9278,7 @@ def child_nodes
end

alias deconstruct child_nodes

def deconstruct_keys(keys)
{ parts: parts, location: location }
end
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@
foo
bar
)
%
return([1, 2, 3])
-
return 1, 2, 3
%
return [1, 2, 3]
-
return 1, 2, 3
%
return []
%
return [1]