-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add Style/MultilineAssignmentLayout Cop #2361
Add Style/MultilineAssignmentLayout Cop #2361
Conversation
9a24ea4
to
f01daea
Compare
end | ||
|
||
it 'auto-corrects offenses' do | ||
new_source = autocorrect_source(cop, "blarg = if true\nend") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if you used an array of lines instead of a single string with newlines, as we do in most specs.
This cop is useful only if |
@jonas054 It seems to me that enabling this cop only when The reason I am providing If you think this causes too much confusion I could also enable more types by default or just allow all types by default and allow users to pair the list down on their own. |
f01daea
to
feb16ec
Compare
@jonas054 I adjusted the specs to use an array of lines as well. |
feb16ec
to
1753e43
Compare
Having looked a bit closer now, I see that this cop checks more than is covered by the linked rule in the style guide. Since we're linking to the rule in the configuration, I think the best way is to hard-code the list of supported types. They should be And since the style guide says # good - it's apparent what's going on
kind = case year
when 1850..1889 then 'Blues'
when 1890..1909 then 'Ragtime'
when 1910..1929 then 'New Orleans Jazz'
when 1930..1939 then 'Swing'
when 1940..1950 then 'Bebop'
else 'Jazz'
end We must allow code like that in the cop. Looking at the configuration of |
I'm not objected to hard-coding the values at all. It sounds like I should use Do you think it's best to leave the cop disabled by default; and just allow users to enable if they want to use this style? |
Nodes of type I believe that we can enable the cop by default, if we limit it a bit as per our discussion above. |
@jonas054 The list sounds good. I'll get working on that. I am still a bit confused about when to enable/disable the cop though and if it belongs in |
Put it in |
b7c5525
to
5d07c13
Compare
@jonas054 I've updated w/ the pre-set list of https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/config_store.rb#L31 |
Perhaps it should have configurable styles. My main concern about this cop is its interplay with the end alignment cop, as you've already discussed a bit. I see the point of the cop, but I'm worried we'll make a complicated setup even more complex. |
I am open to any suggestions to make this cop fit into the existing RuboCop ecosystem. It seems that having a pre-set list of "things that return" - excluding arrays and hashes - is a good starting point. That at least covers most use cases. The list could still be configurable to allow for the removal/addition of some types for advanced usage. It does seem reasonable to me to not enable/disable functionality based on another cop's configurations. My perception of this is that it would lead to unnecessary complexity and additional trouble in debugging issues. One beauty of the RuboCop system is how modularized each cop can be. Of course, I am not as familiar with the overall architecture of RuboCop or it's uses, so I'm happy to implement this in the way you think works best. |
I was perhaps wrong in suggesting hard-coding the list. If you want to bring back the config option, it's fine with me. If we don't want to introduce a dependency towards the configuration of There is one problem. If you run the cop with |
@jonas054 I'll add the config option back. It sounds like maybe a Should I add that to this PR or create a separate PR? |
@jonas054 Just a little more guidance regarding the |
@panthomakos Sorry for the late reply. Add the new cop in a separate PR. An |
5d07c13
to
e105f8f
Compare
# foo = | ||
# begin | ||
# compute | ||
# resucue => e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: spelling rescue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will fix.
👍 Looks good to me. |
e105f8f
to
214496d
Compare
Fixed spelling of |
include CheckAssignment | ||
|
||
MSG = 'Right hand side of multi-line assignment is on the same ' \ | ||
'line as the assignment.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"assignment operator =
" will be clearer.
Apart from my small comments, the code looks good. My main concern is that this is not configurable - I can imagine some people would want to make sure the thing on the right side is on the same line as
|
@bbatsov I'll take a look at making this configurable and I'll incorporate your other comments. |
This cop checks for a new-line after the assignment operator in multi-line assignments. When `EnforcedStyle` is set to `new_line`, this cop enforces the width efficient style of multi-line assignments. It guarantees that an assignment in which the right-hand-side spans multiple lines includes a newline after the assignment operator. When `EnforcedStyle` is set to `same_line`, this cop guarantees that an assignment in which the right-hand-side spans multiple lines begins on the same line as the assignment operator. * A configurable set of node types, like `if` and `case` are supported. * Indentation can further by corrected by other cops such as `Style/IndentationConsistency` and `Style/IndentAssignment`. https://github.com/bbatsov/ruby-style-guide#indent-conditional-assignment
214496d
to
353c1eb
Compare
👍 Looks good to me. |
Add Style/MultilineAssignmentLayout Cop
This cop enforces the width efficient style of multi-line assignments.
It guarantees that an assignment in which the right-hand-side spans
multiple lines includes a newline after the assignment operator.
if
andcase
nodes are supported.Style/IndentationConsistency
.https://github.com/bbatsov/ruby-style-guide#indent-conditional-assignment