-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add MethodDefineMacros
option to Naming/PredicateName
cop
#4862
Conversation
@@ -73,4 +73,57 @@ def is_a? | |||
RUBY | |||
end | |||
end | |||
|
|||
context 'with method define macros' do |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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| |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
e4dca40
to
8263598
Compare
Squash and rebase and we're good to go. |
8263598
to
8d68579
Compare
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! ```
8d68579
to
ae2c1ed
Compare
👍 |
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.
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.
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 toNaming/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.def_node_matcher
is also used for internal affairs of RuboCop itself. The following is config/.rubocop.yml.The following is the effect applied to the the before master branch (995315f) .
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).rake spec
) are passing.rake internal_investigation
.and description in grammatically correct, complete sentences.
rake generate_cops_documentation
(required only when you've added a new cop or changed the configuration/documentation of an existing cop).