diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-21 15:12:22 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-21 15:12:22 +0000 |
commit | 5dc70663c4ff1feb215428ce50673b5b646f9809 (patch) | |
tree | 944bde8a8350ac8ee64335cc5b98d1c60ba9c3c5 /rubocop | |
parent | fbc1f4ffc29ae99f438b07872c3c00fbe7651caa (diff) | |
download | gitlab-ce-5dc70663c4ff1feb215428ce50673b5b646f9809.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/gitlab/strong_memoize_attr.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/strong_memoize_attr.rb b/rubocop/cop/gitlab/strong_memoize_attr.rb new file mode 100644 index 00000000000..2da7f71b920 --- /dev/null +++ b/rubocop/cop/gitlab/strong_memoize_attr.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Gitlab + # Cop that disallows functions that contain only a call to `strong_memoize()`, in favor + # of `strong_memoize_attr()`. + 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) + $_ + ) + ) + PATTERN + + def on_def(node) + method_name, send_node, attr_name, body = strong_memoize?(node) + return unless method_name + + add_offense(send_node) do |corrector| + attr_suffix = ", :#{attr_name}" if attr_name != method_name + + corrector.insert_after(node, "\n#{indent(node)}strong_memoize_attr :#{method_name}#{attr_suffix}") + corrector.replace(node.body, body.source) + end + end + end + end + end +end |