summaryrefslogtreecommitdiff
path: root/rubocop/cop/gitlab/strong_memoize_attr.rb
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop/cop/gitlab/strong_memoize_attr.rb')
-rw-r--r--rubocop/cop/gitlab/strong_memoize_attr.rb19
1 files changed, 12 insertions, 7 deletions
diff --git a/rubocop/cop/gitlab/strong_memoize_attr.rb b/rubocop/cop/gitlab/strong_memoize_attr.rb
index c98aa4765fa..0b3de9d7863 100644
--- a/rubocop/cop/gitlab/strong_memoize_attr.rb
+++ b/rubocop/cop/gitlab/strong_memoize_attr.rb
@@ -34,12 +34,12 @@ module RuboCop
class StrongMemoizeAttr < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
- MSG = 'Use `strong_memoize_attr`, instead of using `strong_memoize` directly'
+ MSG = 'Use `strong_memoize_attr`, instead of using `strong_memoize` directly.'
def_node_matcher :strong_memoize?, <<~PATTERN
(block
$(send nil? :strong_memoize
- (sym $_)
+ (sym _)
)
(args)
$_
@@ -47,21 +47,26 @@ module RuboCop
PATTERN
def on_block(node)
- send_node, attr_name, body = strong_memoize?(node)
+ send_node, body = strong_memoize?(node)
return unless send_node
- corrector = autocorrect_pure_definitions(node.parent, attr_name, body) if node.parent.def_type?
+ # Don't flag methods with parameters.
+ return if send_node.each_ancestor(:def).first&.arguments&.any?
+
+ # Don't flag singleton methods.
+ return if send_node.each_ancestor(:defs).any?
+
+ corrector = autocorrect_pure_definitions(node.parent, body) if node.parent.def_type?
add_offense(send_node, &corrector)
end
private
- def autocorrect_pure_definitions(def_node, attr_name, body)
+ def autocorrect_pure_definitions(def_node, body)
proc do |corrector|
method_name = def_node.method_name
- attr_suffix = ", :#{attr_name}" if attr_name != method_name
- replacement = "\n#{indent(def_node)}strong_memoize_attr :#{method_name}#{attr_suffix}"
+ replacement = "\n#{indent(def_node)}strong_memoize_attr :#{method_name}"
corrector.insert_after(def_node, replacement)
corrector.replace(def_node.body, body.source)