Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-11-30 18:07:02 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2022-11-30 18:07:02 +0300
commit826cf5293fb78029f76c5e769696e3b37e681207 (patch)
tree703bc997b5fa36c42e2bd7486f000ad41b01d252 /rubocop
parent7aa22e9a103b049dd2da70045a5822c51164f7db (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/gitlab/strong_memoize_attr.rb64
1 files changed, 49 insertions, 15 deletions
diff --git a/rubocop/cop/gitlab/strong_memoize_attr.rb b/rubocop/cop/gitlab/strong_memoize_attr.rb
index 2da7f71b920..c98aa4765fa 100644
--- a/rubocop/cop/gitlab/strong_memoize_attr.rb
+++ b/rubocop/cop/gitlab/strong_memoize_attr.rb
@@ -3,34 +3,68 @@
module RuboCop
module Cop
module Gitlab
- # Cop that disallows functions that contain only a call to `strong_memoize()`, in favor
- # of `strong_memoize_attr()`.
+ # Prefer using `.strong_memoize_attr()` over `#strong_memoize()`. See
+ # https://docs.gitlab.com/ee/development/utilities.html/#strongmemoize.
+ #
+ # Good:
+ #
+ # def memoized_method
+ # 'This is a memoized method'
+ # end
+ # strong_memoize_attr :memoized_method
+ #
+ # Bad, can be autocorrected:
+ #
+ # def memoized_method
+ # strong_memoize(:memoized_method) do
+ # 'This is a memoized method'
+ # end
+ # end
+ #
+ # Very bad, can't be autocorrected:
+ #
+ # def memoized_method
+ # return unless enabled?
+ #
+ # strong_memoize(:memoized_method) do
+ # 'This is a memoized method'
+ # end
+ # end
+ #
class StrongMemoizeAttr < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
MSG = 'Use `strong_memoize_attr`, instead of using `strong_memoize` directly'
def_node_matcher :strong_memoize?, <<~PATTERN
- (def $_ _
- (block
- $(send nil? :strong_memoize
- (sym $_)
- )
- (args)
- $_
+ (block
+ $(send nil? :strong_memoize
+ (sym $_)
)
+ (args)
+ $_
)
PATTERN
- def on_def(node)
- method_name, send_node, attr_name, body = strong_memoize?(node)
- return unless method_name
+ def on_block(node)
+ send_node, attr_name, body = strong_memoize?(node)
+ return unless send_node
- add_offense(send_node) do |corrector|
+ corrector = autocorrect_pure_definitions(node.parent, attr_name, body) if node.parent.def_type?
+
+ add_offense(send_node, &corrector)
+ end
+
+ private
+
+ def autocorrect_pure_definitions(def_node, attr_name, 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}"
- corrector.insert_after(node, "\n#{indent(node)}strong_memoize_attr :#{method_name}#{attr_suffix}")
- corrector.replace(node.body, body.source)
+ corrector.insert_after(def_node, replacement)
+ corrector.replace(def_node.body, body.source)
end
end
end