summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorJacopo <beschi.jacopo@gmail.com>2018-08-27 14:23:25 +0200
committerJacopo <beschi.jacopo@gmail.com>2018-08-29 16:56:34 +0200
commit8af40870e2208612a5af34e19f231032745a7f90 (patch)
tree3df7e9d25dd62a3dc027aeca4e968059cf7cdd57 /rubocop
parenta33fd596738cc3d6adc0d213ee05953a19008c4e (diff)
downloadgitlab-ce-8af40870e2208612a5af34e19f231032745a7f90.tar.gz
Applies rule only when extending ActiveSupport::Concern
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/prefer_class_methods_over_module.rb24
1 files changed, 22 insertions, 2 deletions
diff --git a/rubocop/cop/prefer_class_methods_over_module.rb b/rubocop/cop/prefer_class_methods_over_module.rb
index bd91c663e58..496d2ed249f 100644
--- a/rubocop/cop/prefer_class_methods_over_module.rb
+++ b/rubocop/cop/prefer_class_methods_over_module.rb
@@ -2,12 +2,14 @@
module RuboCop
module Cop
- # Enforces the use of 'class_methods' instead of 'module ClassMethods'
+ # Enforces the use of 'class_methods' instead of 'module ClassMethods' for activesupport concerns.
# For more information see: https://gitlab.com/gitlab-org/gitlab-ce/issues/50414
#
# @example
# # bad
# module Foo
+ # extend ActiveSupport::Concern
+ #
# module ClassMethods
# def a_class_method
# end
@@ -16,6 +18,8 @@ module RuboCop
#
# # good
# module Foo
+ # extend ActiveSupport::Concern
+ #
# class_methods do
# def a_class_method
# end
@@ -27,8 +31,12 @@ module RuboCop
MSG = 'Do not use module ClassMethods, use class_methods block instead.'
+ def_node_matcher :extend_activesupport_concern?, <<~PATTERN
+ (:send nil? :extend (:const (:const nil? :ActiveSupport) :Concern))
+ PATTERN
+
def on_module(node)
- add_offense(node) if node.defined_module_name == 'ClassMethods'
+ add_offense(node) if node.defined_module_name == 'ClassMethods' && extends_activesupport_concern?(node)
end
def autocorrect(node)
@@ -39,6 +47,18 @@ module RuboCop
private
+ def extends_activesupport_concern?(node)
+ container_module(node.parent)&.descendants.any? do |descendant|
+ extend_activesupport_concern?(descendant)
+ end
+ end
+
+ def container_module(node)
+ node = node.parent until node.type == :module
+
+ node
+ end
+
def module_range(node)
module_node, _ = *node
range_between(node.loc.keyword.begin_pos, module_node.source_range.end_pos)