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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow asking to avoid mixing hash shorthand styles in single hashes #10776
Allow asking to avoid mixing hash shorthand styles in single hashes #10776
Conversation
48dbeac
to
8f07125
Compare
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 don't have any problems with the dependency from HashSyntax to the mixin. It all looks good to me. But I'm missing the new supported style in default.yml
.
8f07125
to
21a4aa7
Compare
Thanks for looking - I'll leave it as-is then!
My bad, I didn't realise I needed to update that too - all done now though! Rebased again too. |
21a4aa7
to
68a5d91
Compare
I'm OK with the proposal, but I don't like the name of the config option. I'm think of something like |
We use |
I admit I'm not sold on
I'll keep pondering it 馃 |
Just keep in mind that's not really a supported style, but rather the value of |
68a5d91
to
bc6a236
Compare
I don't really have a better suggestion than |
it 'registers an offense when some hash values are omitted but they cannot all be omitted' do | ||
expect_offense(<<~RUBY) | ||
{foo:, bar: baz} | ||
^^^ Do not mix explicit and implicit hash values. Explicit the hash value. |
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.
Explicit the hash value
doesn't really make sense (I see now that it's used on the old messages too but I still don't think it makes sense).
^^^ Do not mix explicit and implicit hash values. Explicit the hash value. | |
^^^ Do not mix explicit and implicit hash values. Include the hash value. |
Also there's an extra space after the period.
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 agree that it doesn't make much sense, but I was reusing the existing error message for the never
option. Should I change that one too - are changes to existing error messages a breaking change?
We can agree to disagree on the period double space formatting though 馃槈. I will remove it though as it does seem more consistent with the rest of the error messages.
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.
We could change it as a separate PR if you'd prefer, but if you're willing, change it everywhere please! Changing messages aren't considered breaking.
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.
Added a commit to reword the existing message before adding the new feature.
bc6a236
to
bdc4689
Compare
It was "explicit the hash value", which on reflection, doesn't _quite_ make sense so we've changed it to "include the hash value", which does.
For those who want to use hash shorthand style, but don't want to mix shorthand and explicit styles in the same hash we provide a new option for `EnforcedShorthandSyntax` called `consistent` that raises an offence if we've mixed styles. This cop prefers the shorthand syntax only if the whole hash can be written that way, but if it can't it will prefer fully explicit values. E.g. # bad {foo: , bar: bar} # good {foo:, bar:} # bad {foo: , bar: baz} # good {foo: foo, bar: baz}
bdc4689
to
edb5399
Compare
Thanks! |
@dvandersluis From next time onwards, please wait a few days after a minor version release for new features to merge. If there is an urgent bug, it will be a bugfix release. For example, I will wait for @bbatsov to merge new features before merging PR like this. For future reference. Anyway thank you for your activities :-) |
`EnforcedShorthandSyntax: always` is the default setting in Rubocop even though Bozhidar himself [doesn't like the option](https://batsov.com/articles/2022/01/20/bad-ruby-hash-value-omission/). I suggest to change the default option to `consistent` because I can see the advantage of the shorthand syntax when passing on options to deeper levels. But in any other context I found myself to be confused by it. This change allows hashes to be in shorthand syntax only if the whole hash uses it consistently. See rubocop/rubocop#10776.
@h-lame Did you make your thoughts about hashes as method arguments as well? Is something like this consistent or not? FactoryBot.create(:customer_promotion, :claimed, customer:, store:) |
@schmijos - I haven't explicitly tested that example, but it should be consistent and not flag for change because the implementation only looks at hashes, and in the "hash" part of that method call ( |
* Enforce consistent hash syntax `EnforcedShorthandSyntax: always` is the default setting in Rubocop even though Bozhidar himself [doesn't like the option](https://batsov.com/articles/2022/01/20/bad-ruby-hash-value-omission/). I suggest to change the default option to `consistent` because I can see the advantage of the shorthand syntax when passing on options to deeper levels. But in any other context I found myself to be confused by it. This change allows hashes to be in shorthand syntax only if the whole hash uses it consistently. See rubocop/rubocop#10776. * Switch to either Co-authored-by: Alessandro Rodi <alessandro.rodi@renuo.ch>
For those who want to use hash shorthand style, but don't want to mix shorthand and explicit styles in the same hash we provide a new option for
EnforcedShorthandSyntax
calledconsistent
that raises an offence if we've mixed styles.E.g.
Requesting help 馃檵
I don't particularly like the implementation. Prior to this commit
HashSyntax
mixed inHashShorthandSyntax
but was otherwise unaware of it. With this change I've had to makeHashSyntax
aware ofHashShorthandSyntax
so it can callon_hash_for_mixed_shorthand
inon_hash
. I don't think there's a way to enforce a no mixing style at the pair level meaning we can't do this inon_pair
already present inHashShorthandSyntax
and have to useon_hash
to look at the whole hash in one go. Would love some advice on this.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists). (n/a)master
(if not - rebase it). (current rebase against - 0984886)bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.