Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/prefer_class_methods_over_module.rb73
-rw-r--r--rubocop/rubocop.rb1
2 files changed, 74 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..0dfa80ccfab
--- /dev/null
+++ b/rubocop/cop/prefer_class_methods_over_module.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ # 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
+ # end
+ # end
+ #
+ # # good
+ # module Foo
+ # extend ActiveSupport::Concern
+ #
+ # 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_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' && module_extends_activesupport_concern?(node)
+ end
+
+ def autocorrect(node)
+ lambda do |corrector|
+ corrector.replace(module_range(node), 'class_methods do')
+ end
+ end
+
+ private
+
+ def module_extends_activesupport_concern?(node)
+ container_module = container_module_of(node)
+ return false unless container_module
+
+ container_module.descendants.any? do |descendant|
+ extend_activesupport_concern?(descendant)
+ end
+ end
+
+ def container_module_of(node)
+ while node = node.parent
+ break if node.type == :module
+ end
+
+ node
+ end
+
+ 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'