diff options
author | Jacopo <beschi.jacopo@gmail.com> | 2018-08-25 00:08:34 +0200 |
---|---|---|
committer | Jacopo <beschi.jacopo@gmail.com> | 2018-08-29 16:56:34 +0200 |
commit | 2f0327b7d8ac46fcb30d957d54fab32b27f8625f (patch) | |
tree | 206bcb41c8a437a2245a14fa2ca1c5f3d17323d8 | |
parent | dad73a77843cb89e8e6d0069fe9a1956e6b304d6 (diff) | |
download | gitlab-ce-2f0327b7d8ac46fcb30d957d54fab32b27f8625f.tar.gz |
Adds Rubocop rule to enforce class_methods over module ClassMethods
-rw-r--r-- | rubocop/cop/prefer_class_methods_over_module.rb | 48 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 1 | ||||
-rw-r--r-- | spec/rubocop/cop/prefer_class_methods_over_module_spec.rb | 66 |
3 files changed, 115 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' diff --git a/spec/rubocop/cop/prefer_class_methods_over_module_spec.rb b/spec/rubocop/cop/prefer_class_methods_over_module_spec.rb new file mode 100644 index 00000000000..64b1f1b948d --- /dev/null +++ b/spec/rubocop/cop/prefer_class_methods_over_module_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'rubocop' +require 'rubocop/rspec/support' +require_relative '../../../rubocop/cop/prefer_class_methods_over_module' + +describe RuboCop::Cop::PreferClassMethodsOverModule do + include CopHelper + + subject(:cop) { described_class.new } + + it 'flags violation when using ClassMethods' do + expect_offense(<<~RUBY) + module Foo + module ClassMethods + ^^^^^^^^^^^^^^^^^^^ Do not use module ClassMethods, use class_methods block instead. + def a_class_method + end + end + end + RUBY + end + + it "doesn't flag violation when using class_methods" do + expect_no_offenses(<<~RUBY) + module Foo + class_methods do + 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 + def a_method + end + end + RUBY + end + + it 'autocorrects ClassMethods into class_methods' do + source = <<~RUBY + module Foo + module ClassMethods + def a_class_method + end + end + end + RUBY + autocorrected = autocorrect_source(source) + + expected_source = <<~RUBY + module Foo + class_methods do + def a_class_method + end + end + end + RUBY + expect(autocorrected).to eql(expected_source) + end +end |