debian-mirror-gitlab/rubocop/cop/gitlab/strong_memoize_attr.rb

79 lines
2.1 KiB
Ruby
Raw Normal View History

2023-03-04 22:38:38 +05:30
# frozen_string_literal: true
module RuboCop
module Cop
module Gitlab
# 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
2023-03-17 16:20:25 +05:30
MSG = 'Use `strong_memoize_attr`, instead of using `strong_memoize` directly.'
2023-03-04 22:38:38 +05:30
def_node_matcher :strong_memoize?, <<~PATTERN
(block
$(send nil? :strong_memoize
2023-03-17 16:20:25 +05:30
(sym _)
2023-03-04 22:38:38 +05:30
)
(args)
$_
)
PATTERN
def on_block(node)
2023-03-17 16:20:25 +05:30
send_node, body = strong_memoize?(node)
2023-03-04 22:38:38 +05:30
return unless send_node
2023-03-17 16:20:25 +05:30
# 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?
2023-03-04 22:38:38 +05:30
add_offense(send_node, &corrector)
end
private
2023-03-17 16:20:25 +05:30
def autocorrect_pure_definitions(def_node, body)
2023-03-04 22:38:38 +05:30
proc do |corrector|
method_name = def_node.method_name
2023-03-17 16:20:25 +05:30
replacement = "\n#{indent(def_node)}strong_memoize_attr :#{method_name}"
2023-03-04 22:38:38 +05:30
corrector.insert_after(def_node, replacement)
corrector.replace(def_node.body, body.source)
end
end
end
end
end
end