Skip to content

Is there a way to force Style/HashExcept on Ruby 2.x such as in Rails app? #790

Closed
@r7kamura

Description

@r7kamura

Is your feature request related to a problem? Please describe.

I'm using Ruby 2.7 in my Rails application and I know there is Style/HashExcept cop which is useful where we can use Hash#except.

Hash#except was originally provided as a core extension to activesupport, and then it was ported to Ruby, right? Since Hash#except is already available for environments like Rails apps, I would really like to take advantage of this cop, regardless of the Ruby version.

So the question is, is there any good way to enable RuboCop to enable this cop in such case?

Describe the solution you'd like

I think the best way for users is that this cop is disabled by default, but can be enabled by writing the following setting:

Style/HashExcept:
  Enabled: true

though I understand that from an internal implementation point of view, that would be a bit of a challenge.

Describe alternatives you've considered

Monkey-patching minimum_target_ruby_version will work, so I'm doing this for now 😇

require:
  - ./monkey-patch-style-hash-except.rb
#  ./monkey-patch-style-hash-except.rb
RuboCop::Cop::Style::HashExcept.minimum_target_ruby_version(2.7)

Additional context

In this issue, I am targeting Style/HashExcept for example, but I think this kind of problem can actually occur in other cops. I hope some good way is provided in such cases.

The real problem we are having is that we are trying to upgrade Ruby from 2.7 to 3.0, but when we try to upgrade it to Ruby 3.0, Style/HashExcept starts reporting offenses. Well, we can temporarily disable this cop, but ideally we don't want to do that, right? Also we don't want to make changes about Hash#except at the same time with Ruby upgrade, if possible, because the diff would be larger and then the risk of conflict would be greater.

Besides Hash#except, I am sure there are other cases where, for example, you want to backport a new feature from new Ruby or Rails version and enable cop for this feature in advance in order to make upgrading easier. Another example is the deprecation of to_fs in Rails 7. That required a large change to our codebase, so we backported that feature in Rails 6.1, and then we had to monkey-patch Rails/ToSWithArgument cop on minimum_target_rails_version 7.0. I wish we had the ability to force enable it in this situation as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions