diff options
Diffstat (limited to 'rubocop/cop/static_translation_definition.rb')
-rw-r--r-- | rubocop/cop/static_translation_definition.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/rubocop/cop/static_translation_definition.rb b/rubocop/cop/static_translation_definition.rb index 736d8767342..55956d9665b 100644 --- a/rubocop/cop/static_translation_definition.rb +++ b/rubocop/cop/static_translation_definition.rb @@ -21,6 +21,8 @@ module RuboCop method_name = node.children[1] return unless TRANSLATION_METHODS.include?(method_name) + translation_memoized = false + node.each_ancestor do |ancestor| receiver, _ = *ancestor break if lambda_node?(receiver) # translations defined in lambda nodes should be allowed @@ -30,6 +32,14 @@ module RuboCop break end + + translation_memoized = true if memoization?(ancestor) + + if translation_memoized && class_method_definition?(ancestor) + add_offense(node, location: :expression) + + break + end end end @@ -38,6 +48,14 @@ module RuboCop def constant_assignment?(node) node.type == :casgn end + + def memoization?(node) + node.type == :or_asgn + end + + def class_method_definition?(node) + node.type == :defs + end end end end |