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

A few bugs/problems in IndexWith #1315

Open
zverok opened this issue Jul 21, 2024 · 0 comments
Open

A few bugs/problems in IndexWith #1315

zverok opened this issue Jul 21, 2024 · 0 comments

Comments

@zverok
Copy link

zverok commented Jul 21, 2024

A bunch of minor bugs/problems with IndexWith. (Maybe it would’ve been better to make a ticket for each, but I assume they all can be fixed together.)

Mostly related to autocorrect (other than the first one).

  1. The cop doesn’t detect problems with numbered block parameters collection.to_h { [_1, something(_1)] }. Probably affects IndexBy, too.
  2. Autocorrection: Produces invalid code when there is a braceless hash in to_h:
    a. code: collection.to_h { |x| [x, position: x, value: y] }.
    b. correction: index_with { |x| position: x, value: y }
    c. proper correction: index_with { |x| {position: x, value: y} }
  3. Autocorrection: Ignores the simplification when value doesn’t depend on the key:
    a. code: KEYS.to_h { |key| [key, true] } (frequently used in producing some initial/default value hashes)
    b. correction: KEYS.index_with { |key| true }
    c. better correction: KEYS.index_with(true)
  4. Incorrect correction when keys are only the first element of the array (probably shouldn’t suggest correction at all):
# sample data
SETTINGS = {validate: true, numeric: false, length: false}

# code that causes offense:
defaults = SETTINGS.to_h { |key,| [key, true] }
# => {validate: true, numeric: true, length: true}

# proposed correction:
defaults = SETTINGS.index_with { |key| [true] }
# => {[:validate, true] => true, [:numeric, false] => true, [:length, false] => true}

RuboCop version

$ bundle exec rubocop -V
1.64.1 (using Parser 3.3.3.0, rubocop-ast 1.31.3, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-graphql 1.1.1
  - rubocop-performance 1.21.0
  - rubocop-rails 2.25.0
  - rubocop-rake 0.6.0
  - rubocop-rspec 3.0.1
  - rubocop-rspec_rails 2.30.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant