summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2017-09-19 01:25:23 +0800
committerLin Jen-Shin <godfat@godfat.org>2017-09-19 01:29:32 +0800
commit6a4ee9aa7140862075cafae1ddebd133eec52b5b (patch)
tree9890bb5c906a0d6e207149ae5fe1df84d213fa7c /rubocop
parent9ae92b8caa6c11d8860f86b7d6378062215d1b72 (diff)
downloadgitlab-ce-6a4ee9aa7140862075cafae1ddebd133eec52b5b.tar.gz
Allow simple ivar ||= form. Update accordingly
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/module_with_instance_variables.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/rubocop/cop/module_with_instance_variables.rb b/rubocop/cop/module_with_instance_variables.rb
index 6ed1b986fdd..95e612b58e8 100644
--- a/rubocop/cop/module_with_instance_variables.rb
+++ b/rubocop/cop/module_with_instance_variables.rb
@@ -45,11 +45,29 @@ module RuboCop
def check_method_definition(node)
node.each_child_node(:def) do |definition|
- definition.each_descendant(:ivar, :ivasgn) do |offense|
- add_offense(offense, :expression)
+ # We allow this pattern:
+ # def f
+ # @f ||= true
+ # end
+ if only_ivar_or_assignment?(definition)
+ # We don't allow if any other ivar is used
+ definition.each_descendant(:ivar) do |offense|
+ add_offense(offense, :expression)
+ end
+ else
+ definition.each_descendant(:ivar, :ivasgn) do |offense|
+ add_offense(offense, :expression)
+ end
end
end
end
+
+ def only_ivar_or_assignment?(definition)
+ node = definition.child_nodes.last
+
+ definition.child_nodes.size == 2 &&
+ node.or_asgn_type? && node.child_nodes.first.ivasgn_type?
+ end
end
end
end