ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

Enforce Multiline Method calls, Arrays, and Hashes to use one line per element

Open Korri opened this issue 2 years ago • 4 comments

This might be a personal pet peeve of mine, but I find all the examples inside the # bad to be hard to read, and even lead me to simply not realize that one of the elements is here at all on more complex cases.

Adding the rules on this PR will autocorrect all those cases to use one line per argument and/or element, leading to a much better readability!

Those are actually recommended when using Layout/LineLength, and it does seems to be where I see the most of cases of "autocorrect gone wrong".

bad (good before this Change)

foo(
a,
b,
c,
)

foo(a, b,
  c)

foo(a,
  b,
  c)

{
  a: 1, b: 2,
  c: 3,
}

{ a: 1,
  b: 2,
  c: 3, }

[
  a, b,
  c,
]

[a,
  b,
  c,]

Good (and enforced now)

foo(
  a,
  b,
  c
)

foo(
  a,
  b,
  c
)

foo(a, b, c)

{
  a: 1,
  b: 2,
  c: 3,
}

{ a: 1, b: 2, c: 3 }

[
  a,
  b,
  c,
]

[a, b, c]

Missing pieces

Method declarations are not included, I played around with adding Layout/FirstMethodParameterLineBreak, while it does make it a little better, it does not forces arguments to go in individual lines, unless I'm missing something Rubocop seems to be lacking the required Layout/MultilineMethodParameterLineBreaks rule for this. Because of that, I decided no to touch method declarations at all for now.

Korri avatar May 11 '22 15:05 Korri

Are there relevant sections in README.md that should be updated?

sambostock avatar May 25 '22 14:05 sambostock

What is the impact of this in Shopify core?

rafaelfranca avatar May 25 '22 18:05 rafaelfranca

@rafaelfranca Here is a draft PR: https://github.com/Shopify/shopify/pull/354457

45989 files inspected, 468482 offenses detected, 468296 offenses corrected

So: Significant impact!

Korri avatar May 25 '22 18:05 Korri

Are there relevant sections in README.md that should be updated?

Good point, does that update makes sense? https://github.com/Shopify/ruby-style-guide/pull/387/commits/e0082bb616852a0ff9395c4b51fd6fff2bbd9255

Korri avatar May 25 '22 18:05 Korri

Updated this to depend on the changes made in this PR, allowing us to keep a few exceptions to the new rules. This now will have to wait on the next version bump of Rubocop though!

Korri avatar Dec 13 '22 19:12 Korri

@rafaelfranca @sambostock

Rubocop just release a new version, so this PR is finally ready to go with new exceptions for some single/last elements!

Any objections ?

Korri avatar Dec 20 '22 16:12 Korri