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
7441c7af
Commit
7441c7af
authored
Nov 17, 2017
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow initialize method and single ivar
parent
9ac0c76b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
3 deletions
+46
-3
rubocop/cop/module_with_instance_variables.rb
rubocop/cop/module_with_instance_variables.rb
+17
-3
spec/rubocop/cop/module_with_instance_variables_spec.rb
spec/rubocop/cop/module_with_instance_variables_spec.rb
+29
-0
No files found.
rubocop/cop/module_with_instance_variables.rb
View file @
7441c7af
...
...
@@ -46,6 +46,7 @@ module RuboCop
def
check_method_definition
(
node
)
node
.
each_child_node
(
:def
)
do
|
definition
|
# We allow this pattern:
#
# def f
# @f ||= true
# end
...
...
@@ -54,6 +55,9 @@ module RuboCop
definition
.
each_descendant
(
:ivar
)
do
|
offense
|
add_offense
(
offense
,
:expression
)
end
# We allow initialize method and single ivar
elsif
initialize_method?
(
definition
)
||
single_ivar?
(
definition
)
next
else
definition
.
each_descendant
(
:ivar
,
:ivasgn
)
do
|
offense
|
add_offense
(
offense
,
:expression
)
...
...
@@ -68,6 +72,16 @@ module RuboCop
definition
.
child_nodes
.
size
==
2
&&
node
.
or_asgn_type?
&&
node
.
child_nodes
.
first
.
ivasgn_type?
end
def
single_ivar?
(
definition
)
node
=
definition
.
child_nodes
.
last
definition
.
child_nodes
.
size
==
2
&&
node
.
ivar_type?
end
def
initialize_method?
(
definition
)
definition
.
children
.
first
==
:initialize
end
end
end
end
spec/rubocop/cop/module_with_instance_variables_spec.rb
View file @
7441c7af
...
...
@@ -137,6 +137,35 @@ describe RuboCop::Cop::ModuleWithInstanceVariables do
it_behaves_like
'not registering offense'
end
context
'when source is using simple ivar'
do
let
(
:source
)
do
<<~
RUBY
module M
def f?
@f
end
end
RUBY
end
it_behaves_like
'not registering offense'
end
context
'when source is defining initialize'
do
let
(
:source
)
do
<<~
RUBY
module M
def initialize
@a = 1
@b = 2
end
end
RUBY
end
it_behaves_like
'not registering offense'
end
context
'when source is using simple or ivar assignment with other ivar'
do
let
(
:source
)
do
<<~
RUBY
...
...
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