Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
2f0327b7
Commit
2f0327b7
authored
Aug 25, 2018
by
Jacopo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds Rubocop rule to enforce class_methods over module ClassMethods
parent
dad73a77
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
115 additions
and
0 deletions
+115
-0
rubocop/cop/prefer_class_methods_over_module.rb
rubocop/cop/prefer_class_methods_over_module.rb
+48
-0
rubocop/rubocop.rb
rubocop/rubocop.rb
+1
-0
spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
+66
-0
No files found.
rubocop/cop/prefer_class_methods_over_module.rb
0 → 100644
View file @
2f0327b7
# 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
rubocop/rubocop.rb
View file @
2f0327b7
...
...
@@ -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'
...
...
spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
0 → 100644
View file @
2f0327b7
# 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment