rubocop: Align the operands of an expression in an assignment spanning multiple lines bug (or me not understanding the config)

I’m running: 0.28.0 (using Parser 2.2.0.2, running on ruby 2.0.0 x86_64-darwin14.0.0)

With the code:

users = User.where(email: 'user@example.com').
             update_all(user_parameters)

I get the rubocop warning:

Align the operands of an expression in an assignment spanning multiple lines

It passes with this which I feel isn’t correct indentation:

users = User.where(email: 'user@example.com').
        update(user_parameters)

I’m not sure if this is a bug or my error.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 26

Commits related to this issue

Most upvoted comments

@rafalchmiel Why not simply use one method call per line?

a = [1, 3, 5]
    .map { |i| i + 1 }
    .map { |i| i + 2 }

You should keep in mind this is expression continuation and the expression starts with the first receiver. I understand your reasoning, but it seems flawed to me. There’s also the fact few editors support the indentation you suggest.

Style/MultilineOperationIndentation can be configured with the aligned style (default), which enforces

users = User.where(email: 'user@example.com').
        update(user_parameters)

and indented, which enforces

users = User.where(email: 'user@example.com').
  update(user_parameters)

The aligned style is supported by ruby-mode 1.1 in Emacs and indented is another common style.

So I wouldn’t say that there’s a bug in RuboCop here. The behavior is intended. To support your style, which I wouldn’t rule out at this point, we’d have to add a third alternative. Maybe call is semantic since it tries to convey a meaning relating to a call chain? I have a suspicion there might be a few corner cases with this new style that I can’t describe in detail right now, but I don’t know.

Being able to verticaly align method calls when they are related can ease the understanding, in my opinion.

For instance with this dummy exemple:

a = [1, 3, 5].map { |i| i + 1 }
             .map { |i| i + 2 }

It seems to me more self explanatory than the rubocop compliant:

a = [1, 3, 5].map { |i| i + 1 }
    .map { |i| i + 2 }

In rails, this situation happen when you have severals query/scope:

@users = User.active
             .visible
             .where(is_admin: true)