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
Layout/AlignHash allow two options #6649
Conversation
751a7a5
to
6473ec8
Compare
I should add some spec to this. |
6473ec8
to
02e11c1
Compare
I have written spec. I would very much like some feedback on this. |
Hi, @pocke |
I will rebase after it is review so I don't wast to much time rebasing and force pushing |
Sorry about the slow turnaround on this. I'll take a look a closer look soon. In general I'm wary of adding 3 more styles to this cop, if we probably need just one. From what I gathered most people want something that allows a combination of table or key, right? |
Thank you for taking a look at it. |
Sorry for the late reply. How about making EnforcedStyle receiving multiple options? EnforcedStyle:
- table
- key I think it is easy to use, but it is inconsistent with other cops because other cops do not support multiple options. If you and many users need to allow |
I think
That's a pretty interesting idea in general - make it possible to accept several styles where it makes sense. It would probably be reasonable simple to do this, and it might be my preference. @rubocop-hq/rubocop-core What do you think about this idea? |
I think the idea of supporting either a string or a list would be very cool and flexible. And when you enter all thee formats, any format is valid but only one format in one hash. I did one attempt at making it a list in the beginning but faced some dificulities. I would be happy diving it a second time, with some mentoring and pointing me where to look. |
The only small problem is which style to suggest in case a hash doesn't match any of them. Probably the one that was specified first. |
@bbatsov, EDIT: now it is implemented by the one woth least offences. Since if you add one more element to an element to an existing hash correct as least as possible. |
I think thats better becuase If you have {
ab: 123,
assdf: 12332,
asf: 12332,
} And you add one element {
ab: 123,
assdf: 12332,
asf: 12332,
b: true,
} You probably want to format it to table if you allow both key and table |
So, what are doing here? Do we agree to try the multiple enforced styles approach? |
I can use the name |
I actually prefer @pocke's idea and autocorrecting using whatever is first in the |
7125db2
to
e5dc67c
Compare
Did you se my comment about what happens if you add something to an existing hash but without 100% match?
I just want to say, you have more experence maintaining rubocop so I will do just want to say :) |
Yeah, I did. I don't consider this a problem as in your editor you'd know what to do - you see some warning, but you also know it will disappear once you change it. The auto-correct should just pick the first option:
In this case - |
@stoivo Ping :-) |
2ae82cb
to
e0212f4
Compare
e0212f4
to
0306ae9
Compare
@bbatsov, thanks for beein patient with me. Could to review it now? I am facing a minor issue with rubocop
def validate_enforced_styles(valid_cop_names)
valid_cop_names.each do |name|
styles = self[name].select { |key, _| key.start_with?('Enforced') }
styles.each do |style_name, style|
supported_key = RuboCop::Cop::Util.to_supported_styles(style_name)
valid = ConfigLoader.default_configuration[name][supported_key]
next unless valid
next if valid.include?(style)
next if ConfigLoader.default_configuration[name]['AllowListStyles'] &&
style.is_a?(Array) &&
style.all? { |a_style| valid.include?(a_style) }
msg = "invalid #{style_name} '#{style}' for #{name} found in " \
"#{smart_loaded_path}\n" \
"Valid choices are: #{valid.join(', ')}"
raise ValidationError, msg
end
end
end How do you want me to solve this? |
I think you should extract either the contents of the inner ConfigLoader.default_configuration[name]['AllowListStyles'] &&
style.is_a?(Array) &&
style.all? { |a_style| valid.include?(a_style) } into a new method. Come up with a name for the new method and you're good to go. 🙂 |
@@ -174,6 +174,37 @@ | |||
end | |||
end | |||
|
|||
context 'when the configuration includes multiple valid EnforcedStyle' do |
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.
Should we raise an error if the cop doesn't support multiple style explicitly?
@stoivo Added a bit of small remarks. Generally the code looks pretty good and we're very close to finally merging this. |
Should we consider reverting 807b362 as part of this PR? |
This will fix #6410, right? |
Yes. |
You can specify table_or_key if you want to allow both table and key When multiple hash formats is accept the hash need to be either one or the other.
You can specify table or key or separator or a combination of any of these. If you allow both table and key any hash might be either.
This reverts commit da8c541.
Ready for an onther review. I reverted the commit and did some small modifications to resolve conflicts. |
Good job! There are some small things that'd I'd like us to polish down the road and we have to document somewhere the ability to have cops that support multiple styles, but I'll merge the PR in its current state as it's pretty big and you've been wasting a lot of efforts keeping it up to date. Thanks! 🙇 |
Thank you too. I wm happy to help out a more but I think is is better if you do the polish and add the documentation where it belongs. Very happy with submitting a patch to rubocop. Love the tool ❤️ |
Follow up rubocop@8a7ee84. This PR fixes the following CI errors. ```console bundle exec rake (snip) 1) RuboCop Project changelog entry after version 0.14.0 has a link to the contributors at the end Failure/Error: expect(entries).to all(match(/\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/)) expected ["* [rubocop#6649](rubocop#6649): `Layout/AlignHash` supports list of opt...552): `RaiseArgs` allows exception constructor calls with more than one 1 argument. ([@bbatsov][])"] to all match /\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/ object at index 3 failed to match: expected "* [rubocop#7052](rubocop#7052): Add `AllowComments` option to ` Lint/HandleExceptions`. ([@tejasbubane][]) " to match /\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/ # ./spec/project_spec.rb:128:in `block (5 levels) in <top (required)>' (snip) 2) RuboCop Project changelog entry body ends with a punctuation Failure/Error: expect(bodies).to all(match(/[\.\!]$/)) expected ["`` supports list of options.", "Add `` config option to ``.", "Add `` to ``.", "Add `` option to ``...nclosed in braces are not noticed.", "Received malformed format string ArgumentError from rubocop."] to all match /[\.\!]$/ object at index 3 failed to match: expected "Add `` option to ``. ([@tejasbubane][]) " to match /[\.\!]$/ # ./spec/project_spec.rb:187:in `block (5 levels) in <top (required)>' ``` https://circleci.com/gh/rubocop-hq/rubocop/55378
I my company we like to use Hashes in both table and key format. We would like to have rubucop allow both so I make a path an it seams to work for me.
What I want is
I wanted to open the PR to check if it would be accepted if I can submitt a nice PR for it. I can add more spec. I wil do so if you think you want this feature
Right now I made it an option not_<any_of_the_setting>. I think it would be nicer if you could spesifi it as a list in .
rubocop.yml
Thoughts? Do you have an example where you do this?
If I can change it to a list the first one can be the format to autofix to.
Edit: after a little more testing is should autofix to the the format how as the least amoint of offences.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.