summaryrefslogtreecommitdiff
path: root/rubocop
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
parentdad73a77843cb89e8e6d0069fe9a1956e6b304d6 (diff)
downloadgitlab-ce-2f0327b7d8ac46fcb30d957d54fab32b27f8625f.tar.gz
Adds Rubocop rule to enforce class_methods over module ClassMethods
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/prefer_class_methods_over_module.rb48
-rw-r--r--rubocop/rubocop.rb1
2 files changed, 49 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
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index eaf421a7235..d823fa4edb1 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -7,6 +7,7 @@ require_relative 'cop/include_sidekiq_worker'
require_relative 'cop/avoid_return_from_blocks'
require_relative 'cop/avoid_break_from_strong_memoize'
require_relative 'cop/line_break_around_conditional_block'
+require_relative 'cop/prefer_class_methods_over_module'
require_relative 'cop/migration/add_column'
require_relative 'cop/migration/add_concurrent_foreign_key'
require_relative 'cop/migration/add_concurrent_index'