From d0b80782cce9dbe7bf4d0a27f6141293377465f4 Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Thu, 21 Apr 2022 21:15:38 -0400 Subject: [PATCH 1/6] Add return example to fixture --- test/fixtures/return.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/fixtures/return.rb b/test/fixtures/return.rb index e1989cb4..243f14d2 100644 --- a/test/fixtures/return.rb +++ b/test/fixtures/return.rb @@ -25,3 +25,7 @@ foo bar ) +% +return([1, 2, 3]) +- +return 1, 2, 3 From fd435eff2462ce431f97c5fcd51b661fdeb13d83 Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Thu, 21 Apr 2022 21:16:22 -0400 Subject: [PATCH 2/6] Handle Array arg in parens --- lib/syntax_tree/node.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index bd3f915f..390f8c9b 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -2068,7 +2068,9 @@ 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 From 30b22de3649832bb4b2f0f2477cb02b044aef629 Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Thu, 21 Apr 2022 21:16:53 -0400 Subject: [PATCH 3/6] Add return array w/ no parens examples to fixture --- test/fixtures/return.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/fixtures/return.rb b/test/fixtures/return.rb index 243f14d2..b57e5bad 100644 --- a/test/fixtures/return.rb +++ b/test/fixtures/return.rb @@ -29,3 +29,7 @@ return([1, 2, 3]) - return 1, 2, 3 +% +return [1, 2, 3] +- +return 1, 2, 3 From fe988e2b2646e30dbfd4c9a5abb0fc7456fec1ef Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Thu, 21 Apr 2022 21:17:25 -0400 Subject: [PATCH 4/6] Correctly handle `return Array` --- lib/syntax_tree/node.rb | 53 +++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 390f8c9b..6b43d28e 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -2076,7 +2076,7 @@ def format(q) end elsif part.is_a?(ArrayLiteral) q.text(" ") - q.format(arguments) + q.format(part.contents) else format_arguments(q, "(", ")") end @@ -2089,6 +2089,7 @@ def format(q) private + def format_arguments(q, opening, closing) q.if_break { q.text(opening) } q.indent do @@ -2101,7 +2102,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 @@ -2525,7 +2526,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -3810,7 +3811,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -3840,7 +3841,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -3872,7 +3873,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -5313,7 +5314,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -6438,7 +6439,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -6538,7 +6539,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -6599,7 +6600,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -6624,7 +6625,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -6695,7 +6696,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { beginning: beginning, parts: parts, location: location } end @@ -6728,7 +6729,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -6762,7 +6763,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -7248,7 +7249,7 @@ def initialize(value:, location:) @value = value @location = location end - + def accept(visitor) visitor.visit_rparen(self) end @@ -7258,7 +7259,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -7513,7 +7514,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { parts: parts, location: location } end @@ -7810,7 +7811,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -7840,7 +7841,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -7971,7 +7972,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -8000,7 +8001,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -8030,7 +8031,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -8141,7 +8142,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -8219,7 +8220,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -9244,7 +9245,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { value: value, location: location } end @@ -9273,7 +9274,7 @@ def child_nodes end alias deconstruct child_nodes - + def deconstruct_keys(keys) { parts: parts, location: location } end From aeee4d059ff8d443c9fb03c6d282c7ecc499f080 Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Thu, 21 Apr 2022 21:37:19 -0400 Subject: [PATCH 5/6] Add more examples to return fixture --- test/fixtures/return.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/fixtures/return.rb b/test/fixtures/return.rb index b57e5bad..8f7d0aa3 100644 --- a/test/fixtures/return.rb +++ b/test/fixtures/return.rb @@ -33,3 +33,7 @@ return [1, 2, 3] - return 1, 2, 3 +% +return [] +% +return [1] From a12a74a9020117048e3336320b2afeea404d727f Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Thu, 21 Apr 2022 21:37:46 -0400 Subject: [PATCH 6/6] Handle empty and single element Arrays --- lib/syntax_tree/node.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 6b43d28e..af7ac42e 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -2076,7 +2076,11 @@ def format(q) end elsif part.is_a?(ArrayLiteral) q.text(" ") - q.format(part.contents) + if part.contents && part.contents.parts.length > 1 + q.format(part.contents) + else + q.format(arguments) + end else format_arguments(q, "(", ")") end