Skip to content

Block arguments in if statements uses {, triggering Rubocop #122

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

Open
renchap opened this issue Jul 26, 2022 · 3 comments
Open

Block arguments in if statements uses {, triggering Rubocop #122

renchap opened this issue Jul 26, 2022 · 3 comments

Comments

@renchap
Copy link

renchap commented Jul 26, 2022

Sample code after formatting using @prettier/plugin-ruby 3:

    if current_user&.admin? ||
         Rails.application.config.x.team_ips.any? { |i|
           i.include?(request.remote_ip)
         }
      Rack::MiniProfiler.authorize_request
    end

This is triggering Rubocop, even after including this gem's config:

app/controllers/application_controller.rb:62:51: C: [Correctable] Style/BlockDelimiters: Avoid using {...} for multi-line blocks.
         Rails.application.config.x.team_ips.any? { |i|

The original code (formatted by the Prettier plugin v2) cas using do … end, which did not trigger Rubocop.

I am not sure what is the correct fix here:

@kddnewton
Copy link
Member

I'm pretty sure the fix here is to check for Binary nodes as well here: https://github.com/ruby-syntax-tree/syntax_tree/pull/121/files#diff-4a62a987e00853ea4da04a58594e31564bc9afc77a1cbdf95f95493784360659R1981. Want to take a crack at it?

@varg90
Copy link

varg90 commented Jul 6, 2023

For someone who faces this issue and don't want to use # stree-ignore in their code, as a workaround just put the block into a variable and check for if variable instead.
I'd actually say, that this would make your code cleaner.

@maxnotarangelo
Copy link

I'm also running into do...end to switching to braces on multiple lines when formatting. Adding it here in case it's useful.

Original:

def example
  return if array_of_things.any? do |complicated_thing|
    complicated_thing == another_thing
  end

  do_stuff
end

Formatted:

def example
  if array_of_things.any? { |complicated_thing|
       complicated_thing == another_thing
     }
    return
  end

  do_stuff
end

I'm not 100% sure the formatting isn't correct; something like the below formatting isn't great either.

def example
  if array_of_things.any? do |complicated_thing|
       complicated_thing == another_thing
     end
    return
  end

  do_stuff
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants