Commit 7441c7af authored by Lin Jen-Shin's avatar Lin Jen-Shin

Allow initialize method and single ivar

parent 9ac0c76b
......@@ -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
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment