-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix false negatives for Style/NumberedParameters and ItBlockParameter
#14527
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
Fix false negatives for Style/NumberedParameters and ItBlockParameter
#14527
Conversation
…ter` This PR fixes false negatives for `Style/NumberedParameters` and `ItBlockParameter` when using multiline method chain with `EnforcedStyle: allow_single_line`. `EnforcedStyle: allow_single_line` allows single-line usage, and numbered block parameters across multiple lines should be detected. This was an implementation bug, and the `same_line?` method should have been used instead of the `single_line?` or `multiline?` methods.
|
I don't agree with this change. The block is clearly only on one line, no? I would expect this to be ok: foo
.baz { transform(it) }
.bat { change(it) }What is the point of single-line if this is rejected? |
|
I think those cases make |
|
The offense message is specifically "Avoid using bar { do_something(it) }
foo
.bar { do_something(it) }It's only a little more indented and has a dot before it. I would consider method chaining like that one of the best use-cases for |
|
AKAIK, it was introduced as a shorthand. As stated in the main point, code that should be maintained ought to be explicitly named. What |
|
I kind of agree with @Earlopain on this point, as I also think the setting targets a single-line block, not the whole expression the block is part of. In my mind there's little difference between several chained blocks on the same line or multiple lines, so I think this will likely confuse some people. Guess I should have thought more about the PR before merging it. My bad! |
|
Hmm, I'm questioning whether it makes sense to evaluate this based on the blocks themselves. For example, in a case like: articles.
foo { ... }.
bar { ... }what matters for understanding what the object represents is not the method names like |
|
Hey. 👋 In case it helps, I'm a big fan of the more functional aspects of Ruby and was taken aback by this change. For example, the following is no longer valid (where, previously, it was): firmware.where { attachment_data.has_key "id" }
.select { attachment_data.get_text("id").as(:attachment_id) }
.map(:attachment_id)
.each { shrine_store.delete it }I understand the arguments made here but would be in favor of what I've posted above as being valid. Each line is a step which has it's own operation (so-to-speak). The readability isn't degraded because there are four steps here which read, nicely, from top to bottom. Each is self contained. The fact that I use # This would not work, functionally, but only showing for illustration purposes.
firmware.each { shrine_store.delete it }It's the same computation, in my mind, when reading this code (yes, the receiver is different, but you have to make that computation in your mind each time you use At the very least, could there be an option to allow for a functional pipeline flow like this? |
|
I'm also surprised by this very opinionated change. Wasn't the numbered parameters syntax introduced specifically to make multi-line block chains like the examples here be more convenient / more similar to pipe operators in other languages? |
|
I am glad that I am not the only one that is against this change. I openend #14565 to revert, let's hope for the best. |
|
@Earlopain Merged. As noted above - I acknowledge I misread the intention of this PR and we'll need to discuss further if any changes to the existing behavior are needed. |
This PR fixes false negatives for
Style/NumberedParametersandItBlockParameterwhen using multiline method chain withEnforcedStyle: allow_single_line.EnforcedStyle: allow_single_lineallows single-line usage, and numbered block parameters across multiple lines should be detected. This was an implementation bug, and thesame_line?method should have been used instead of thesingle_line?ormultiline?methods.Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).bundle exec rake default. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.