summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacopo <beschi.jacopo@gmail.com>2018-08-28 10:58:36 +0200
committerJacopo <beschi.jacopo@gmail.com>2018-08-29 16:56:34 +0200
commite3e746623b2fa6c45dab66dd1618810a53cdecd8 (patch)
tree51d3a9922484cd0824b58482d3c731ed038180ca
parenta1cad13b923f44e56dc990c528e09c5c9750a8f4 (diff)
downloadgitlab-ce-e3e746623b2fa6c45dab66dd1618810a53cdecd8.tar.gz
Handles when ClassMethods is used inside a class
-rw-r--r--rubocop/cop/prefer_class_methods_over_module.rb6
-rw-r--r--spec/rubocop/cop/prefer_class_methods_over_module_spec.rb11
2 files changed, 15 insertions, 2 deletions
diff --git a/rubocop/cop/prefer_class_methods_over_module.rb b/rubocop/cop/prefer_class_methods_over_module.rb
index ed10229b69a..5097dc7829d 100644
--- a/rubocop/cop/prefer_class_methods_over_module.rb
+++ b/rubocop/cop/prefer_class_methods_over_module.rb
@@ -48,7 +48,7 @@ module RuboCop
private
def extends_activesupport_concern?(node)
- container_module = container_module_of(node.parent)
+ container_module = container_module_of(node)
return false unless container_module
container_module.descendants.any? do |descendant|
@@ -57,7 +57,9 @@ module RuboCop
end
def container_module_of(node)
- node = node.parent until node.type == :module
+ while node = node.parent
+ break if node.type == :module
+ end
node
end
diff --git a/spec/rubocop/cop/prefer_class_methods_over_module_spec.rb b/spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
index 527c236eecf..ec60a651f31 100644
--- a/spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
+++ b/spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
@@ -48,6 +48,17 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
RUBY
end
+ it "doesn't flag violation when ClassMethods is used inside a class" do
+ expect_no_offenses(<<~RUBY)
+ class Foo
+ module ClassMethods
+ def a_class_method
+ end
+ end
+ end
+ RUBY
+ end
+
it "doesn't flag violation when not using either class_methods or ClassMethods" do
expect_no_offenses(<<~RUBY)
module Foo