2020-04-21 11:21:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2022-06-14 08:08:53 -04:00
|
|
|
# This cop flags translation definitions in static scopes because changing
|
|
|
|
# locales has no effect and won't translate this text again.
|
|
|
|
#
|
|
|
|
# See https://docs.gitlab.com/ee/development/i18n/externalization.html#keep-translations-dynamic
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
#
|
|
|
|
# # bad
|
|
|
|
# class MyExample
|
|
|
|
# # Constant
|
|
|
|
# Translation = _('A translation.')
|
|
|
|
#
|
|
|
|
# # Class scope
|
|
|
|
# field :foo, title: _('A title')
|
|
|
|
#
|
|
|
|
# validates :title, :presence, message: _('is missing')
|
|
|
|
#
|
|
|
|
# # Memoized
|
|
|
|
# def self.translations
|
|
|
|
# @cached ||= { text: _('A translation.') }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# included do # or prepended or class_methods
|
|
|
|
# self.error_message = _('Something went wrong.')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # good
|
|
|
|
# class MyExample
|
|
|
|
# # Keep translations dynamic.
|
|
|
|
# Translation = -> { _('A translation.') }
|
|
|
|
# # OR
|
|
|
|
# def translation
|
|
|
|
# _('A translation.')
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# field :foo, title: -> { _('A title') }
|
|
|
|
#
|
|
|
|
# validates :title, :presence, message: -> { _('is missing') }
|
|
|
|
#
|
|
|
|
# def self.translations
|
|
|
|
# { text: _('A translation.') }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# included do # or prepended or class_methods
|
|
|
|
# self.error_message = -> { _('Something went wrong.') }
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2020-04-21 11:21:10 -04:00
|
|
|
class StaticTranslationDefinition < RuboCop::Cop::Cop
|
2022-06-14 08:08:53 -04:00
|
|
|
MSG = <<~TEXT.tr("\n", ' ')
|
|
|
|
Translation is defined in static scope.
|
|
|
|
Keep translations dynamic. See https://docs.gitlab.com/ee/development/i18n/externalization.html#keep-translations-dynamic
|
|
|
|
TEXT
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
RESTRICT_ON_SEND = %i[_ s_ n_].freeze
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
# List of method names which are not considered real method definitions.
|
|
|
|
# See https://api.rubyonrails.org/classes/ActiveSupport/Concern.html
|
|
|
|
NON_METHOD_DEFINITIONS = %i[class_methods included prepended].to_set.freeze
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
def_node_matcher :translation_method?, <<~PATTERN
|
|
|
|
(send _ {#{RESTRICT_ON_SEND.map(&:inspect).join(' ')}} str*)
|
2020-04-21 11:21:10 -04:00
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless translation_method?(node)
|
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
static = true
|
|
|
|
memoized = false
|
2020-09-11 05:08:44 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
node.each_ancestor do |ancestor|
|
2022-06-14 08:08:53 -04:00
|
|
|
memoized = true if memoized?(ancestor)
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
if dynamic?(ancestor, memoized)
|
|
|
|
static = false
|
2020-04-21 11:21:10 -04:00
|
|
|
break
|
|
|
|
end
|
2022-06-14 08:08:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
add_offense(node) if static
|
|
|
|
end
|
2020-09-11 05:08:44 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
private
|
2020-09-11 05:08:44 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
def memoized?(node)
|
|
|
|
node.type == :or_asgn
|
|
|
|
end
|
2020-09-11 05:08:44 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
def dynamic?(node, memoized)
|
|
|
|
lambda_or_proc?(node) ||
|
|
|
|
named_block?(node) ||
|
|
|
|
instance_method_definition?(node) ||
|
|
|
|
unmemoized_class_method_definition?(node, memoized)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
def lambda_or_proc?(node)
|
|
|
|
node.lambda_or_proc?
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
def named_block?(node)
|
|
|
|
return unless node.block_type?
|
|
|
|
|
|
|
|
!NON_METHOD_DEFINITIONS.include?(node.method_name) # rubocop:disable Rails/NegateInclude
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2020-09-11 05:08:44 -04:00
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
def instance_method_definition?(node)
|
|
|
|
node.type == :def
|
2020-09-11 05:08:44 -04:00
|
|
|
end
|
|
|
|
|
2022-06-14 08:08:53 -04:00
|
|
|
def unmemoized_class_method_definition?(node, memoized)
|
|
|
|
node.type == :defs && !memoized
|
2020-09-11 05:08:44 -04:00
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|