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

Add MethodDefineMacros option to Naming/PredicateName cop #4862

Merged

Conversation

koic
Copy link
Member

@koic koic commented Oct 14, 2017

Follow up for #4855 (comment).
Cc @pocke who suggested this feature.

Feature

This PR also add the MethodDefineMacros option to check on methods dynamically defined to Naming/PredicateName cop.

The target that RuboCop checks by default is define_method. This also applies to end user application code. The following is config/default.yml.

Naming/PredicateName:
  # Method define macros for dynamically generated method.
  MethodDefineMacros:
    - define_method

def_node_matcher is also used for internal affairs of RuboCop itself. The following is config/.rubocop.yml.

Naming/PredicateName:
  # Method define macros for dynamically generated method.
  MethodDefineMacros:
    - define_method
    - def_node_matcher

The following is the effect applied to the the before master branch (995315f) .

% bundle exec rake internal_investigation
Running RuboCop...
Inspecting 1010 files

(snip)

Offenses:

lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:26:26: C: Rename has_many_or_has_one_without_options? to many_or_has_one_without_options?.
        def_node_matcher :has_many_or_has_one_without_options?, <<-PATTERN
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:30:26: C: Rename has_many_or_has_one_with_options? to many_or_has_one_with_options?.
        def_node_matcher :has_many_or_has_one_with_options?, <<-PATTERN
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:34:26: C: Rename has_dependent? to dependent?.
        def_node_matcher :has_dependent?, <<-PATTERN
                         ^^^^^^^^^^^^^^^
lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:38:26: C: Rename has_through? to through?.
        def_node_matcher :has_through?, <<-PATTERN
                         ^^^^^^^^^^^^^
lib/rubocop/cop/rails/not_null_column.rb:34:26: C: Rename has_default? to default?.
        def_node_matcher :has_default?, <<-PATTERN
                         ^^^^^^^^^^^^^

1010 files inspected, 5 offenses detected
RuboCop failed!

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@@ -73,4 +73,57 @@ def is_a?
RUBY
end
end

context 'with method define macros' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

define -> definition

end
end

context 'without method define macros' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

Copy link
Collaborator

Choose a reason for hiding this comment

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

Btw, it seems you copy/pasted the previous context here (or I'm missing something).

Copy link
Member Author

@koic koic Oct 16, 2017

Choose a reason for hiding this comment

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

It is written as with previous context, without here context.

@@ -638,6 +638,9 @@ Naming/PredicateName:
# should still be accepted
NameWhitelist:
- is_a?
# Method define macros for dynamically generated method.
MethodDefineMacros:
- define_method
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ruby also has #define_singleton_method. 🙂

PATTERN

def on_send(node)
dynamic_method_define(node) do |micro_name, method_name|
Copy link
Collaborator

Choose a reason for hiding this comment

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

micro -> macro 🙂

@@ -18,12 +18,35 @@ module Naming
# # good
# def value? ...
class PredicateName < Cop
def_node_matcher :dynamic_method_define, <<-PATTERN
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit fragmented. Use don it's own, this doesn't return what it says it does. It just returns an unfiltered list of methods. This could probably use a funcall (see the top level comment in NorePattern) to filter according to the actual whitelist.

Copy link
Member Author

@koic koic Oct 16, 2017

Choose a reason for hiding this comment

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

I made a fix using funcall with the following commit 🙏
8263598

@@ -35,6 +58,12 @@ def on_def(node)

private

def valid_method_name?(method_name, prefix)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since Ruby has its own notion about "valid" method names, perhaps we can use other nomenclature here? Maybe something like #allowed_method_name?

@koic koic force-pushed the add_method_define_macros_to_naming_predicate_name branch 4 times, most recently from e4dca40 to 8263598 Compare October 16, 2017 06:02
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 16, 2017

Squash and rebase and we're good to go.

@koic koic force-pushed the add_method_define_macros_to_naming_predicate_name branch from 8263598 to 8d68579 Compare October 16, 2017 14:39
@koic
Copy link
Member Author

koic commented Oct 16, 2017

Thanks for the review. I rebased with squash.

Follow up for rubocop#4855 (comment).

Feature
=======

This PR also add the `MethodDefineMacros` option to check on methods
dynamically defined to `Naming/PredicateName` cop.

The target that RuboCop checks by default is `define_method`. This also
applies to end user application code. The following is config/default.yml.

```yaml
Naming/PredicateName:
  MethodDefineMacros:
    - define_method
    - define_singleton_method
```

`def_node_matcher` is also used for internal affairs of RuboCop
itself. The following is config/.rubocop.yml.

```yaml
Naming/PredicateName:
  MethodDefineMacros:
    - define_method
    - define_singleton_method
    - def_node_matcher
```

The following is the effect applied to the before master branch (995315f) .

```console
% bundle exec rake internal_investigation
Running RuboCop...
Inspecting 1010 files

(snip)

Offenses:

lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:26:26: C: Rename
has_many_or_has_one_without_options? to many_or_has_one_without_options?.
        def_node_matcher :has_many_or_has_one_without_options?, <<-PATTERN
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:30:26: C: Rename
has_many_or_has_one_with_options? to many_or_has_one_with_options?.
        def_node_matcher :has_many_or_has_one_with_options?, <<-PATTERN
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:34:26: C: Rename
has_dependent? to dependent?.
        def_node_matcher :has_dependent?, <<-PATTERN
                         ^^^^^^^^^^^^^^^
lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:38:26: C: Rename
has_through? to through?.
        def_node_matcher :has_through?, <<-PATTERN
                         ^^^^^^^^^^^^^
lib/rubocop/cop/rails/not_null_column.rb:34:26: C: Rename has_default?
to default?.
        def_node_matcher :has_default?, <<-PATTERN
                         ^^^^^^^^^^^^^

1010 files inspected, 5 offenses detected
RuboCop failed!
```
@koic koic force-pushed the add_method_define_macros_to_naming_predicate_name branch from 8d68579 to ae2c1ed Compare October 16, 2017 15:41
@bbatsov bbatsov merged commit afbb06f into rubocop:master Oct 17, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 17, 2017

👍

@koic koic deleted the add_method_define_macros_to_naming_predicate_name branch October 17, 2017 07:43
koic added a commit to koic/rubocop that referenced this pull request Oct 18, 2017
Follow up for rubocop#4862.

As with `def_node_matcher`, this commit added `def_node_search` method
used for internal affair. The following are the use cases.

```console
% git grep def_node_search
.rubocop.yml:    - def_node_search
lib/rubocop/cop/bundler/duplicated_gem.rb:        def_node_search: gem_declarations, '(send nil? :gem str ...)'
lib/rubocop/cop/bundler/ordered_gems.rb:        def_node_search :gem_declarations, <<-PATTERN
lib/rubocop/cop/internal_affairs/useless_message_assertion.rb: def_node_search :described_class_msg, <<-PATTERN
lib/rubocop/cop/mixin/unused_argument.rb:        def_node_search :uses_var?, '(lvar %)'
lib/rubocop/cop/performance/redundant_block_call.rb: def_node_search :blockarg_calls, <<-PATTERN
lib/rubocop/cop/performance/redundant_block_call.rb: def_node_search :blockarg_assigned?, <<-PATTERN
lib/rubocop/cop/performance/regexp_match.rb:        def_node_search :search_match_nodes, MATCH_NODE_PATTERN
lib/rubocop/cop/performance/regexp_match.rb:        def_node_search :last_matches, <<-PATTERN
lib/rubocop/cop/rails/file_path.rb:        def_node_search :rails_root_nodes?, <<-PATTERN
lib/rubocop/cop/style/parallel_assignment.rb:          def_node_search :uses_var?, '{({lvar ivar cvar gvar} %) (const _ %)}'
lib/rubocop/cop/style/parallel_assignment.rb:          def_node_search :matching_calls, '(send %1 %2 $...)'
lib/rubocop/cop/style/signal_exception.rb:        def_node_search :custom_fail_methods,
lib/rubocop/node_pattern.rb:      def def_node_search(method_name, pattern_str)
```

There are no offenses now, it will be set for the future.
bbatsov pushed a commit that referenced this pull request Oct 18, 2017
Follow up for #4862.

As with `def_node_matcher`, this commit added `def_node_search` method
used for internal affair. The following are the use cases.

```console
% git grep def_node_search
.rubocop.yml:    - def_node_search
lib/rubocop/cop/bundler/duplicated_gem.rb:        def_node_search: gem_declarations, '(send nil? :gem str ...)'
lib/rubocop/cop/bundler/ordered_gems.rb:        def_node_search :gem_declarations, <<-PATTERN
lib/rubocop/cop/internal_affairs/useless_message_assertion.rb: def_node_search :described_class_msg, <<-PATTERN
lib/rubocop/cop/mixin/unused_argument.rb:        def_node_search :uses_var?, '(lvar %)'
lib/rubocop/cop/performance/redundant_block_call.rb: def_node_search :blockarg_calls, <<-PATTERN
lib/rubocop/cop/performance/redundant_block_call.rb: def_node_search :blockarg_assigned?, <<-PATTERN
lib/rubocop/cop/performance/regexp_match.rb:        def_node_search :search_match_nodes, MATCH_NODE_PATTERN
lib/rubocop/cop/performance/regexp_match.rb:        def_node_search :last_matches, <<-PATTERN
lib/rubocop/cop/rails/file_path.rb:        def_node_search :rails_root_nodes?, <<-PATTERN
lib/rubocop/cop/style/parallel_assignment.rb:          def_node_search :uses_var?, '{({lvar ivar cvar gvar} %) (const _ %)}'
lib/rubocop/cop/style/parallel_assignment.rb:          def_node_search :matching_calls, '(send %1 %2 $...)'
lib/rubocop/cop/style/signal_exception.rb:        def_node_search :custom_fail_methods,
lib/rubocop/node_pattern.rb:      def def_node_search(method_name, pattern_str)
```

There are no offenses now, it will be set for the future.
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