diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2018-01-09 20:30:04 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2018-01-12 12:54:55 +0300 |
commit | 4f00a05152c105814f17a5f582d493435de96747 (patch) | |
tree | 76eab649b7c832afb47ad3e37ead6eb7a4bdaa93 /rubocop/cop | |
parent | 3fde958f36fa9c3bfa30ed1f73108e0640722926 (diff) |
Introduce PredicateMemoization cop and fix offenses
with StrongMemoize
Diffstat (limited to 'rubocop/cop')
-rw-r--r-- | rubocop/cop/gitlab/predicate_memoization.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/predicate_memoization.rb b/rubocop/cop/gitlab/predicate_memoization.rb new file mode 100644 index 00000000000..3c25d61d087 --- /dev/null +++ b/rubocop/cop/gitlab/predicate_memoization.rb @@ -0,0 +1,39 @@ +module RuboCop + module Cop + module Gitlab + class PredicateMemoization < RuboCop::Cop::Cop + MSG = <<~EOL.freeze + Avoid using `@value ||= query` inside predicate methods in order to + properly memoize `false` or `nil` values. + https://docs.gitlab.com/ee/development/utilities.html#strongmemoize + EOL + + def on_def(node) + return unless predicate_method?(node) + + select_offenses(node).each do |offense| + add_offense(offense, location: :expression) + end + end + + private + + def predicate_method?(node) + node.method_name.to_s.end_with?('?') + end + + def or_ivar_assignment?(or_assignment) + lhs = or_assignment.each_child_node.first + + lhs.ivasgn_type? + end + + def select_offenses(node) + node.each_descendant(:or_asgn).select do |or_assignment| + or_ivar_assignment?(or_assignment) + end + end + end + end + end +end |