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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
cf826fac
Commit
cf826fac
authored
Aug 27, 2018
by
Jacopo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Applies rule only when extending ActiveSupport::Concern
parent
90dfa836
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
3 deletions
+44
-3
rubocop/cop/prefer_class_methods_over_module.rb
rubocop/cop/prefer_class_methods_over_module.rb
+22
-2
spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
+22
-1
No files found.
rubocop/cop/prefer_class_methods_over_module.rb
View file @
cf826fac
...
@@ -2,12 +2,14 @@
...
@@ -2,12 +2,14 @@
module
RuboCop
module
RuboCop
module
Cop
module
Cop
# Enforces the use of 'class_methods' instead of 'module ClassMethods'
# 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
# For more information see: https://gitlab.com/gitlab-org/gitlab-ce/issues/50414
#
#
# @example
# @example
# # bad
# # bad
# module Foo
# module Foo
# extend ActiveSupport::Concern
#
# module ClassMethods
# module ClassMethods
# def a_class_method
# def a_class_method
# end
# end
...
@@ -16,6 +18,8 @@ module RuboCop
...
@@ -16,6 +18,8 @@ module RuboCop
#
#
# # good
# # good
# module Foo
# module Foo
# extend ActiveSupport::Concern
#
# class_methods do
# class_methods do
# def a_class_method
# def a_class_method
# end
# end
...
@@ -27,8 +31,12 @@ module RuboCop
...
@@ -27,8 +31,12 @@ module RuboCop
MSG
=
'Do not use module ClassMethods, use class_methods block instead.'
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
)
def
on_module
(
node
)
add_offense
(
node
)
if
node
.
defined_module_name
==
'ClassMethods'
add_offense
(
node
)
if
node
.
defined_module_name
==
'ClassMethods'
&&
extends_activesupport_concern?
(
node
)
end
end
def
autocorrect
(
node
)
def
autocorrect
(
node
)
...
@@ -39,6 +47,18 @@ module RuboCop
...
@@ -39,6 +47,18 @@ module RuboCop
private
private
def
extends_activesupport_concern?
(
node
)
container_module
(
node
.
parent
)
&
.
descendants
.
any?
do
|
descendant
|
extend_activesupport_concern?
(
descendant
)
end
end
def
container_module
(
node
)
node
=
node
.
parent
until
node
.
type
==
:module
node
end
def
module_range
(
node
)
def
module_range
(
node
)
module_node
,
_
=
*
node
module_node
,
_
=
*
node
range_between
(
node
.
loc
.
keyword
.
begin_pos
,
module_node
.
source_range
.
end_pos
)
range_between
(
node
.
loc
.
keyword
.
begin_pos
,
module_node
.
source_range
.
end_pos
)
...
...
spec/rubocop/cop/prefer_class_methods_over_module_spec.rb
View file @
cf826fac
...
@@ -10,9 +10,11 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
...
@@ -10,9 +10,11 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
subject
(
:cop
)
{
described_class
.
new
}
subject
(
:cop
)
{
described_class
.
new
}
it
'flags violation when using ClassMethods'
do
it
'flags violation when using
module
ClassMethods'
do
expect_offense
(
<<~
RUBY
)
expect_offense
(
<<~
RUBY
)
module Foo
module Foo
extend ActiveSupport::Concern
module ClassMethods
module ClassMethods
^^^^^^^^^^^^^^^^^^^ Do not use module ClassMethods, use class_methods block instead.
^^^^^^^^^^^^^^^^^^^ Do not use module ClassMethods, use class_methods block instead.
def a_class_method
def a_class_method
...
@@ -25,6 +27,8 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
...
@@ -25,6 +27,8 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
it
"doesn't flag violation when using class_methods"
do
it
"doesn't flag violation when using class_methods"
do
expect_no_offenses
(
<<~
RUBY
)
expect_no_offenses
(
<<~
RUBY
)
module Foo
module Foo
extend ActiveSupport::Concern
class_methods do
class_methods do
def a_class_method
def a_class_method
end
end
...
@@ -33,9 +37,22 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
...
@@ -33,9 +37,22 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
RUBY
RUBY
end
end
it
"doesn't flag violation when module is not extending ActiveSupport::Concern"
do
expect_no_offenses
(
<<~
RUBY
)
module Foo
module ClassMethods
def a_class_method
end
end
end
RUBY
end
it
"doesn't flag violation when not using either class_methods or ClassMethods"
do
it
"doesn't flag violation when not using either class_methods or ClassMethods"
do
expect_no_offenses
(
<<~
RUBY
)
expect_no_offenses
(
<<~
RUBY
)
module Foo
module Foo
extend ActiveSupport::Concern
def a_method
def a_method
end
end
end
end
...
@@ -45,6 +62,8 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
...
@@ -45,6 +62,8 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
it
'autocorrects ClassMethods into class_methods'
do
it
'autocorrects ClassMethods into class_methods'
do
source
=
<<~
RUBY
source
=
<<~
RUBY
module Foo
module Foo
extend ActiveSupport::Concern
module ClassMethods
module ClassMethods
def a_class_method
def a_class_method
end
end
...
@@ -55,6 +74,8 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
...
@@ -55,6 +74,8 @@ describe RuboCop::Cop::PreferClassMethodsOverModule do
expected_source
=
<<~
RUBY
expected_source
=
<<~
RUBY
module Foo
module Foo
extend ActiveSupport::Concern
class_methods do
class_methods do
def a_class_method
def a_class_method
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