I'm currently using RuboCop in a Ruby on Rails project, and I have a question regarding code style enforcement for long lines with multiple arguments. We have set a line length maximum of 100 characters, but we're running into inconsistencies when dealing with method calls that have many arguments.
For example, both of the following code snippets are accepted by RuboCop:
create(:user, first_name: 'Bartolomeo', last_name: 'Ruperlstickly',
phone: '+1234455678', address: '1234 str.')
create(:user,
first_name: 'Bartolomeo',
last_name: 'Ruperlstickly',
phone: '+1234455678',
address: '1234 str.')
What we would like to enforce is that if the arguments can't fit on one line (due to our 100 character limit), they should be broken into one argument per line, as per the latter option.
I've tried adjusting RuboCop settings, but I haven't been able to enforce this style strictly. Is there a way to configure RuboCop to enforce this rule? Or do I need to use an additional tool or plugin to achieve this level of formatting consistency?
Here’s what we’ve tried in .rubocop.yml so far:
Layout/FirstParameterIndentation:
EnforcedStyle: consistent
IndentationWidth: 2
Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation
While this helps with indentation, it doesn’t enforce breaking the arguments into separate lines when they exceed the max line length.
I could write a custom cop to enforce this (I think?) but I was hoping there was already a Rubocop rule I'm overlooking or a different gem.
Overall, I would like it to work like prettier does in JS, where you unequivocally always have a single way of breaking a method into multiple lines, so that nobody in the project has to think about it. I'm not closed to using Rubocop; If there's another gem that would do this for us I'm all in, since it would save our team a lot of nitpick style comments on PRs.
Thank you!
This rule is targeted by the Layout/MultilineMethodArgumentLineBreaks cop and should be enforceable and auto-correctable by enabling it. This is supposing that you also already have the Layout/LineLength cop enabled with a a Max
attribute with a value of 100.