Repository: ruby-syntax-tree/syntax_tree (Ruby)

Issue:
Transform between braces & do/end when reflowing blocks?
I was rather surprised to see Syntax Tree make the following choice:
```
          expect(
            resulting_items.map { |i|
              { amount: i.amount }
            },
          ).to match(
            existing_model.fund.ownership_positions.current.map { |i|
              { amount: Float(i.id) }
            },
          )
```

It was preserving my choice of `{}` but when it reflowed the block to multiple lines, our convention normally would have been to switch to a `do/end` pair (and vice-versa).

As a workaround, I can Rubocop to auto-fix after formatting; it's a minor pain however. Is this a behavior that would be amenable to a Syntax Tree plugin? If so, perhaps I can work on contributing one to the core.


Discussion:
So it's doing that because it's trying to be careful. Normally it would happily convert to a `do..end` block. However, since you're inside of a `Command` node, it keeps the braces. 

Here's an example of what I mean: 

```ruby
expect foo, bar { baz }
```

That works fine, but if `baz` is actually very long:

```ruby
expect foo, bar do
  bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
end
```

this actually changes the meaning to the block being given to the `expect` method instead of the `bar` method. So instead it detects that it's inside a command and keeps the braces. It does that here:

https://github.com/ruby-syntax-tree/syntax_tree/blob/3f5b736b9c81587382192385421358dc5209ea62/lib/syntax_tree/node.rb#L1974-L1985

To fix this, as it's walking up the tree is should notice that it's inside of a method call with parentheses (like it notices it's inside a statement list) and _stop_ walking up the tree. That would fix this.

Feel like giving this one a shot?
I'll give it a try; thanks for the advice.



Patch:
diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb
index b633a1c9..d7b6d6cf 100644
--- a/lib/syntax_tree/node.rb
+++ b/lib/syntax_tree/node.rb
@@ -1978,7 +1978,7 @@ def unchangeable_bounds?(q)
         # If we hit a statements, then we're safe to use whatever since we
         # know for certain we're going to get split over multiple lines
         # anyway.
-        break false if parent.is_a?(Statements)
+        break false if parent.is_a?(Statements) || parent.is_a?(ArgParen)
 
         [Command, CommandCall].include?(parent.class)
       end


Test patch:
diff --git a/test/fixtures/command_call.rb b/test/fixtures/command_call.rb
index 4a0f60f0..7c055e8d 100644
--- a/test/fixtures/command_call.rb
+++ b/test/fixtures/command_call.rb
@@ -34,3 +34,43 @@
   bar baz
 %
 foo.bar baz ? qux : qaz
+%
+expect foo, bar.map { |i| { quux: bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } }
+-
+expect foo,
+       bar.map { |i|
+         {
+           quux:
+             bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
+         }
+       }
+%
+expect(foo, bar.map { |i| {quux: bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz} })
+-
+expect(
+  foo,
+  bar.map do |i|
+    {
+      quux:
+        bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
+    }
+  end
+)
+%
+expect(foo.map { |i| { bar: i.bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } } ).to match(baz.map { |i| { bar: i.bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } })
+-
+expect(
+  foo.map do |i|
+    {
+      bar:
+        i.bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
+    }
+  end
+).to match(
+  baz.map do |i|
+    {
+      bar:
+        i.bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
+    }
+  end
+)