summaryrefslogtreecommitdiff
path: root/rubocop/cop
diff options
context:
space:
mode:
authorJacopo <beschi.jacopo@gmail.com>2018-08-25 00:08:34 +0200
committerJacopo <beschi.jacopo@gmail.com>2018-08-29 16:56:34 +0200
commit2f0327b7d8ac46fcb30d957d54fab32b27f8625f (patch)
tree206bcb41c8a437a2245a14fa2ca1c5f3d17323d8 /rubocop/cop
parentdad73a77843cb89e8e6d0069fe9a1956e6b304d6 (diff)
downloadgitlab-ce-2f0327b7d8ac46fcb30d957d54fab32b27f8625f.tar.gz
Adds Rubocop rule to enforce class_methods over module ClassMethods
Diffstat (limited to 'rubocop/cop')
-rw-r--r--rubocop/cop/prefer_class_methods_over_module.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/rubocop/cop/prefer_class_methods_over_module.rb b/rubocop/cop/prefer_class_methods_over_module.rb
new file mode 100644
index 00000000000..bd91c663e58
--- /dev/null
+++ b/rubocop/cop/prefer_class_methods_over_module.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ # Enforces the use of 'class_methods' instead of 'module ClassMethods'
+ # For more information see: https://gitlab.com/gitlab-org/gitlab-ce/issues/50414
+ #
+ # @example
+ # # bad
+ # module Foo
+ # module ClassMethods
+ # def a_class_method
+ # end
+ # end
+ # end
+ #
+ # # good
+ # module Foo
+ # class_methods do
+ # def a_class_method
+ # end
+ # end
+ # end
+ #
+ class PreferClassMethodsOverModule < RuboCop::Cop::Cop
+ include RangeHelp
+
+ MSG = 'Do not use module ClassMethods, use class_methods block instead.'
+
+ def on_module(node)
+ add_offense(node) if node.defined_module_name == 'ClassMethods'
+ end
+
+ def autocorrect(node)
+ lambda do |corrector|
+ corrector.replace(module_range(node), 'class_methods do')
+ end
+ end
+
+ private
+
+ def module_range(node)
+ module_node, _ = *node
+ range_between(node.loc.keyword.begin_pos, module_node.source_range.end_pos)
+ end
+ end
+ end
+end