Skip to content

Commit c779b6c

Browse files
authored
Merge pull request #55 from DRBragg/drbragg/return_with_array
Handle `return` when arg is an `Array`
2 parents 9794a06 + a12a74a commit c779b6c

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

lib/syntax_tree/node.rb

+34-27
Original file line numberDiff line numberDiff line change
@@ -2068,13 +2068,19 @@ def format(q)
20682068
if part.is_a?(Paren)
20692069
if part.contents.body.length == 1 && skip_parens?(part.contents.body.first)
20702070
q.text(" ")
2071-
q.format(part.contents.body.first)
2071+
contents = part.contents.body.first
2072+
contents = contents.contents if contents.is_a?(ArrayLiteral)
2073+
q.format(contents)
20722074
else
20732075
q.format(arguments)
20742076
end
20752077
elsif part.is_a?(ArrayLiteral)
20762078
q.text(" ")
2077-
q.format(arguments)
2079+
if part.contents && part.contents.parts.length > 1
2080+
q.format(part.contents)
2081+
else
2082+
q.format(arguments)
2083+
end
20782084
else
20792085
format_arguments(q, "(", ")")
20802086
end
@@ -2087,6 +2093,7 @@ def format(q)
20872093

20882094
private
20892095

2096+
20902097
def format_arguments(q, opening, closing)
20912098
q.if_break { q.text(opening) }
20922099
q.indent do
@@ -2099,7 +2106,7 @@ def format_arguments(q, opening, closing)
20992106

21002107
def skip_parens?(node)
21012108
case node
2102-
in Int | FloatLiteral
2109+
in Int | FloatLiteral | ArrayLiteral
21032110
true
21042111
in VarRef[value: GVar | IVar | CVar | Kw | Const]
21052112
true
@@ -2523,7 +2530,7 @@ def child_nodes
25232530
end
25242531

25252532
alias deconstruct child_nodes
2526-
2533+
25272534
def deconstruct_keys(keys)
25282535
{ value: value, location: location }
25292536
end
@@ -3808,7 +3815,7 @@ def child_nodes
38083815
end
38093816

38103817
alias deconstruct child_nodes
3811-
3818+
38123819
def deconstruct_keys(keys)
38133820
{ value: value, location: location }
38143821
end
@@ -3838,7 +3845,7 @@ def child_nodes
38383845
end
38393846

38403847
alias deconstruct child_nodes
3841-
3848+
38423849
def deconstruct_keys(keys)
38433850
{ value: value, location: location }
38443851
end
@@ -3870,7 +3877,7 @@ def child_nodes
38703877
end
38713878

38723879
alias deconstruct child_nodes
3873-
3880+
38743881
def deconstruct_keys(keys)
38753882
{ value: value, location: location }
38763883
end
@@ -5311,7 +5318,7 @@ def child_nodes
53115318
end
53125319

53135320
alias deconstruct child_nodes
5314-
5321+
53155322
def deconstruct_keys(keys)
53165323
{ value: value, location: location }
53175324
end
@@ -6436,7 +6443,7 @@ def child_nodes
64366443
end
64376444

64386445
alias deconstruct child_nodes
6439-
6446+
64406447
def deconstruct_keys(keys)
64416448
{ value: value, location: location }
64426449
end
@@ -6536,7 +6543,7 @@ def child_nodes
65366543
end
65376544

65386545
alias deconstruct child_nodes
6539-
6546+
65406547
def deconstruct_keys(keys)
65416548
{ value: value, location: location }
65426549
end
@@ -6597,7 +6604,7 @@ def child_nodes
65976604
end
65986605

65996606
alias deconstruct child_nodes
6600-
6607+
66016608
def deconstruct_keys(keys)
66026609
{ value: value, location: location }
66036610
end
@@ -6622,7 +6629,7 @@ def child_nodes
66226629
end
66236630

66246631
alias deconstruct child_nodes
6625-
6632+
66266633
def deconstruct_keys(keys)
66276634
{ value: value, location: location }
66286635
end
@@ -6693,7 +6700,7 @@ def child_nodes
66936700
end
66946701

66956702
alias deconstruct child_nodes
6696-
6703+
66976704
def deconstruct_keys(keys)
66986705
{ beginning: beginning, parts: parts, location: location }
66996706
end
@@ -6726,7 +6733,7 @@ def child_nodes
67266733
end
67276734

67286735
alias deconstruct child_nodes
6729-
6736+
67306737
def deconstruct_keys(keys)
67316738
{ value: value, location: location }
67326739
end
@@ -6760,7 +6767,7 @@ def child_nodes
67606767
end
67616768

67626769
alias deconstruct child_nodes
6763-
6770+
67646771
def deconstruct_keys(keys)
67656772
{ value: value, location: location }
67666773
end
@@ -7246,7 +7253,7 @@ def initialize(value:, location:)
72467253
@value = value
72477254
@location = location
72487255
end
7249-
7256+
72507257
def accept(visitor)
72517258
visitor.visit_rparen(self)
72527259
end
@@ -7256,7 +7263,7 @@ def child_nodes
72567263
end
72577264

72587265
alias deconstruct child_nodes
7259-
7266+
72607267
def deconstruct_keys(keys)
72617268
{ value: value, location: location }
72627269
end
@@ -7511,7 +7518,7 @@ def child_nodes
75117518
end
75127519

75137520
alias deconstruct child_nodes
7514-
7521+
75157522
def deconstruct_keys(keys)
75167523
{ parts: parts, location: location }
75177524
end
@@ -7808,7 +7815,7 @@ def child_nodes
78087815
end
78097816

78107817
alias deconstruct child_nodes
7811-
7818+
78127819
def deconstruct_keys(keys)
78137820
{ value: value, location: location }
78147821
end
@@ -7838,7 +7845,7 @@ def child_nodes
78387845
end
78397846

78407847
alias deconstruct child_nodes
7841-
7848+
78427849
def deconstruct_keys(keys)
78437850
{ value: value, location: location }
78447851
end
@@ -7969,7 +7976,7 @@ def child_nodes
79697976
end
79707977

79717978
alias deconstruct child_nodes
7972-
7979+
79737980
def deconstruct_keys(keys)
79747981
{ value: value, location: location }
79757982
end
@@ -7998,7 +8005,7 @@ def child_nodes
79988005
end
79998006

80008007
alias deconstruct child_nodes
8001-
8008+
80028009
def deconstruct_keys(keys)
80038010
{ value: value, location: location }
80048011
end
@@ -8028,7 +8035,7 @@ def child_nodes
80288035
end
80298036

80308037
alias deconstruct child_nodes
8031-
8038+
80328039
def deconstruct_keys(keys)
80338040
{ value: value, location: location }
80348041
end
@@ -8139,7 +8146,7 @@ def child_nodes
81398146
end
81408147

81418148
alias deconstruct child_nodes
8142-
8149+
81438150
def deconstruct_keys(keys)
81448151
{ value: value, location: location }
81458152
end
@@ -8217,7 +8224,7 @@ def child_nodes
82178224
end
82188225

82198226
alias deconstruct child_nodes
8220-
8227+
82218228
def deconstruct_keys(keys)
82228229
{ value: value, location: location }
82238230
end
@@ -9242,7 +9249,7 @@ def child_nodes
92429249
end
92439250

92449251
alias deconstruct child_nodes
9245-
9252+
92469253
def deconstruct_keys(keys)
92479254
{ value: value, location: location }
92489255
end
@@ -9271,7 +9278,7 @@ def child_nodes
92719278
end
92729279

92739280
alias deconstruct child_nodes
9274-
9281+
92759282
def deconstruct_keys(keys)
92769283
{ parts: parts, location: location }
92779284
end

test/fixtures/return.rb

+12
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@
2525
foo
2626
bar
2727
)
28+
%
29+
return([1, 2, 3])
30+
-
31+
return 1, 2, 3
32+
%
33+
return [1, 2, 3]
34+
-
35+
return 1, 2, 3
36+
%
37+
return []
38+
%
39+
return [1]

0 commit comments

Comments
 (0)