2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module RuboCop
module Cop
class StaticTranslationDefinition < RuboCop :: Cop :: Cop
2021-04-29 21:17:54 +05:30
MSG = " The text you're translating will be already in the translated form when it's assigned to the constant. When a users changes the locale, these texts won't be translated again. Consider moving the translation logic to a method. "
2020-04-22 19:07:51 +05:30
TRANSLATION_METHODS = % i [ _ s_ n_ ] . freeze
def_node_matcher :translation_method? , << ~ PATTERN
2022-01-26 12:08:38 +05:30
( send _ _ str * )
2020-04-22 19:07:51 +05:30
PATTERN
def_node_matcher :lambda_node? , << ~ PATTERN
2022-01-26 12:08:38 +05:30
( send _ :lambda )
PATTERN
def_node_matcher :struct_constant_assignment? , << ~ PATTERN
( casgn _ _ ` (const _ :Struct))
2020-04-22 19:07:51 +05:30
PATTERN
def on_send ( node )
return unless translation_method? ( node )
method_name = node . children [ 1 ]
return unless TRANSLATION_METHODS . include? ( method_name )
2020-11-24 15:15:51 +05:30
translation_memoized = false
2020-04-22 19:07:51 +05:30
node . each_ancestor do | ancestor |
receiver , _ = * ancestor
break if lambda_node? ( receiver ) # translations defined in lambda nodes should be allowed
2022-01-26 12:08:38 +05:30
if constant_assignment? ( ancestor ) && ! struct_constant_assignment? ( ancestor )
2020-04-22 19:07:51 +05:30
add_offense ( node , location : :expression )
break
end
2020-11-24 15:15:51 +05:30
translation_memoized = true if memoization? ( ancestor )
if translation_memoized && class_method_definition? ( ancestor )
add_offense ( node , location : :expression )
break
end
2020-04-22 19:07:51 +05:30
end
end
private
def constant_assignment? ( node )
node . type == :casgn
end
2020-11-24 15:15:51 +05:30
def memoization? ( node )
node . type == :or_asgn
end
def class_method_definition? ( node )
node . type == :defs
end
2020-04-22 19:07:51 +05:30
end
end
end