Skip to content
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

Style/HashSyntax defaulting to Hash Rockets #9891

Closed
coding-bunny opened this issue Jun 24, 2021 · 6 comments
Closed

Style/HashSyntax defaulting to Hash Rockets #9891

coding-bunny opened this issue Jun 24, 2021 · 6 comments
Assignees
Labels

Comments

@coding-bunny
Copy link

Expected behavior

I expect the Style/HashSyntax to default to the Ruby 1.9 notation for hashes, using { a: 'something' } as the default setting.

Actual behavior

The Cop is recommending the hash rockets syntax by default.

 bundle exec rubocop --debug
For /home/coding-bunny/RubymineProjects/deployment_history: configuration from /home/coding-bunny/RubymineProjects/deployment_history/.rubocop.yml
Inheriting configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/bundler/gems/inkycop-9f511b517abc/config/full.yml
Inheriting configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/bundler/gems/inkycop-9f511b517abc/config/expert.yml
Inheriting configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/bundler/gems/inkycop-9f511b517abc/config/advanced.yml
Inheriting configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/bundler/gems/inkycop-9f511b517abc/config/intermediate.yml
Inheriting configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/bundler/gems/inkycop-9f511b517abc/config/novice.yml
Inheriting configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/bundler/gems/inkycop-9f511b517abc/config/light.yml
Inheriting configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/bundler/gems/inkycop-9f511b517abc/config/base.yml
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-performance-1.11.3/config/default.yml
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-performance-1.11.3/config/default.yml
Default configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-1.17.0/config/default.yml
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-thread_safety-0.4.2/config/default.yml
configuration from #{path}
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-rake-0.5.1/config/default.yml
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-rake-0.5.1/config/default.yml
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-rails-2.11.0/config/default.yml
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-rails-2.11.0/config/default.yml
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-rspec-2.4.0/config/default.yml
configuration from /home/coding-bunny/.rvm/gems/ruby-2.7.3/gems/rubocop-rspec-2.4.0/config/default.yml
/home/coding-bunny/.rvm/gems/ruby-2.7.3/bundler/gems/inkycop-9f511b517abc/config/full.yml: AllCops:DisabledByDefault overrides the same parameter in expert.yml
Inheriting configuration from /home/coding-bunny/RubymineProjects/deployment_history/.rubocop_todo.yml
.rubocop.yml: Layout/LineLength:Max overrides the same parameter in .rubocop_todo.yml
Inspecting 55 files

Steps to reproduce the problem

Project with existing code, and add RuboCop, latest version

RuboCop version

➜  deployment_history git:(coding-bunny/set_up_inkycop) ✗ bundle exec rubocop -V
1.17.0 (using Parser 3.0.1.1, rubocop-ast 1.7.0, running on ruby 2.7.3 x86_64-linux)
  - rubocop-performance 1.11.3
  - rubocop-rails 2.11.0
  - rubocop-rake 0.5.1
  - rubocop-rspec 2.4.0
@Drenmi
Copy link
Collaborator

Drenmi commented Jun 24, 2021

The default is already 1.9. Maybe a configuration file somewhere in your stack is overriding this?

@coding-bunny
Copy link
Author

I checked, and it isn't.
Not a single configuration overwrites those settings.

@coding-bunny
Copy link
Author

Oh wow....
I looked at the generated .rubocop_todo.yml

# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
Style/HashSyntax:
  EnforcedStyle: hash_rockets

Why would RuboCop generate this for 3 offenses found?
This file was generated today with the bundle exec rubocop --auto-gen-config.
This feels....wrong :/

@rrosenblum
Copy link
Contributor

If I remember correctly, during --auto-gen-config some cops will try to determine the "best" configuration to use based on what is found in your project. I think this mainly occurs with opposite configurations.

The generated config is saying that there are 3 offenses based on wanting hash rockets, so 3 places where ruby19 syntax is used. I'm willing to venture a guess that your project happens to have a "high" usage of hash rockets already so ruby19 syntax was deemed as going against your project's standards.

How many offenses are reported if you update the config to

Style/HashSyntax:
  EnforcedStyle: ruby19

I'm willing to guess that there are at least 4 offense (usage of hash rockets) that is causing the opposite config to be chosen. This functionality makes sense for a lot of cops that have an inverse config. It also made sense back when code bases were closer to Ruby 1.9 and it was more common to find mixed syntax or projects that wanted to maintain hash rockets. This functionality doesn't make a whole lot of sense anymore since by this point, the ruby19 syntax should be considered the default syntax

@coding-bunny
Copy link
Author

No, only those 3 were reported.
Because with that changes I had 15 offenses to fix actually.
So it didn't go for the "optimal" case

@jonas054
Copy link
Collaborator

jonas054 commented Jul 1, 2021

Looks like a bug. The algorithm for generating EnforcedStyle in .rubocop_todo.yml is supposed to do so only if that resolves all offenses. If there are different styles used in the code base, it should generate Exclude or Enabled: false. It's not trying to find "the best enforced style".

@jonas054 jonas054 added the bug label Jul 1, 2021
@jonas054 jonas054 self-assigned this Jul 1, 2021
@koic koic closed this as completed in 3d57dc5 Jul 5, 2021
koic added a commit that referenced this issue Jul 5, 2021
[Fix #9891] Fix --auto-gen-config bug for Style/HashSyntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants