Skip to content

Conversation

koic
Copy link
Member

@koic koic commented Apr 1, 2025

This PR adds support for RuboCop::AST::Node#any_def_type? method, which abstracts over the def and defs node types.

@koic koic force-pushed the support_any_def_type_predicate branch from 8d5da93 to dbf3ced Compare April 1, 2025 17:10
This PR adds support for `RuboCop::AST::Node#any_def_type?` method,
which abstracts over the `def` and `defs` node types.
@koic koic force-pushed the support_any_def_type_predicate branch from dbf3ced to 4502517 Compare April 1, 2025 17:11
@Earlopain
Copy link
Contributor

👍 I also think this makes sense. I though about adding this myself in the past but couldn't find a good name. any_def looks good to me.

There are many places in RuboCop than can make use of this, from just a basic search for rubocop:
grafik

Copy link
Contributor

@bbatsov bbatsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

@koic koic merged commit 4409273 into rubocop:master Apr 3, 2025
22 checks passed
@koic koic deleted the support_any_def_type_predicate branch April 3, 2025 05:11
koic added a commit to rubocop/rubocop that referenced this pull request Apr 3, 2025
koic added a commit to rubocop/rubocop-rails that referenced this pull request Apr 3, 2025
Follow-up to rubocop/rubocop-ast#377

This PR suppresses the following new offenses:

```console
$ bundle exec rubocop
(snip)

lib/rubocop/cop/rails/three_state_boolean_column.rb:48:43:
C: [Correctable] InternalAffairs/NodeTypeGroup: Use :any_def instead of individually listing group types.
            def_node = node.each_ancestor(:def, :defs).first
                                          ^^^^^^^^^^^

301 files inspected, 1 offense detected, 1 offense autocorrectable
```
koic added a commit to rubocop/rubocop-performance that referenced this pull request Apr 3, 2025
Follow-up to rubocop/rubocop-ast#377

This PR suppresses the following new offenses:

```console
$ bundle exec rubocop -a
(snip)

Offenses:

lib/rubocop/cop/performance/block_given_with_explicit_block.rb:39:41:
C: [Correctable] InternalAffairs/NodeTypeGroup: Use :any_def instead of individually listing group types.
          def_node = node.each_ancestor(:def, :defs).first
                                        ^^^^^^^^^^^
lib/rubocop/cop/performance/regexp_match.rb:242:28:
C: [Correctable] InternalAffairs/NodeTypeGroup: Use :any_def instead of individually listing group types.
            ancestor.type?(:def, :defs, :class, :module)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

122 files inspected, 2 offenses detected, 2 offenses autocorrectable
```
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

Successfully merging this pull request may close these issues.

3 participants