Skip to content

Multiline blocks sometimes use braces, sometimes do/end #379

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

Closed
etiennebarrie opened this issue May 31, 2023 · 0 comments · Fixed by #380
Closed

Multiline blocks sometimes use braces, sometimes do/end #379

etiennebarrie opened this issue May 31, 2023 · 0 comments · Fixed by #380

Comments

@etiennebarrie
Copy link
Contributor

This shows the issue with fixtures:

diff --git i/test/fixtures/lambda.rb w/test/fixtures/lambda.rb
index 5dba3be..079c082 100644
--- i/test/fixtures/lambda.rb
+++ w/test/fixtures/lambda.rb
@@ -80,3 +80,83 @@
 -> do # comment1
   # comment2
 end
+% # multiline lambda
+-> {
+  multi
+  line
+}
+-
+-> do
+  multi
+  line
+end
+% # multiline lambda inside a method
+def foo
+  -> {
+    multi
+    line
+  }
+end
+-
+def foo
+  -> do
+    multi
+    line
+  end
+end
+% # nested multiline lambda
+-> {
+  -> {
+    multi
+    line
+  }
+}
+-
+-> do
+  -> do
+    multi
+    line
+  end
+end
+% # multiline lambda in a method_add_block
+method_add_block {
+  -> {
+    multi
+    line
+  }
+}
+-
+method_add_block do
+  -> do
+    multi
+    line
+  end
+end
+% # multiline lambda in a method_add_block with arguments
+method_add_block("arg") {
+  -> {
+    multi
+    line
+  }
+}
+-
+method_add_block("arg") do
+  -> do
+    multi
+    line
+  end
+end
+% # multiline lamda in a command
+command "arg" do
+  -> {
+    multi
+    line
+  }
+end
+-
+command "arg" do
+  -> do
+    multi
+    line
+  end
+end
  • The first case shows that multiline lambdas get formatted with do/end
  • The second case shows that multiline lambdas get formatted with do/end inside methods
  • The third case shows that multiline lambdas get formatted with do/end when nested
  • The fourth case shows that multiline lambdas get formatted with do/end when passed as a block in a method call without arguments (parsed as a method_add_block)
  • The fifth case shows that multiline lambdas get formatted with do/end when passed as a block in a method call with arguments but with parentheses (parsed as a method_add_block)
  • The sixth case is failing, shows that multiline lambdas get formatted with braces when passed as a block in a method call with arguments but no parentheses (which is parsed as a command)

Additionally, Rubocop is fine with braces for multiline lambdas because method calls to lambda (including stabby lambdas), proc and it are special-cased: https://github.com/rubocop/rubocop/blob/1884a46d9284b47a07763e2506276662c7c1ae6e/config/default.yml#L3212-L3214

If we remove that special handling, Rubocop will always enforce using do/end for multiline blocks, but stree write will force braces, when called in a method call with arguments but no parentheses, typically in tests in Rails with ActiveSupport::Testing::Declarative:

class SomeTest < ActiveSupport::TestCase
  test "something" do
    _ = -> do
      multi
      line
    end
  end
end

That makes rubocop and stree incompatible for that rule. Once the formatting is fixed to always generate do/end for multiline lambdas, we could add this to config/rubocop.yml to make them compatible:

Style/BlockDelimiters:
  AllowedMethods: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant