2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2022-07-23 23:45:48 +05:30
|
|
|
# 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-22 19:07:51 +05:30
|
|
|
class StaticTranslationDefinition < RuboCop::Cop::Cop
|
2022-07-23 23:45:48 +05:30
|
|
|
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-22 19:07:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
RESTRICT_ON_SEND = %i[_ s_ n_].freeze
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
# 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-22 19:07:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def_node_matcher :translation_method?, <<~PATTERN
|
|
|
|
(send _ {#{RESTRICT_ON_SEND.map(&:inspect).join(' ')}} str*)
|
2020-04-22 19:07:51 +05:30
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless translation_method?(node)
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
static = true
|
|
|
|
memoized = false
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
node.each_ancestor do |ancestor|
|
2022-07-23 23:45:48 +05:30
|
|
|
memoized = true if memoized?(ancestor)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
if dynamic?(ancestor, memoized)
|
|
|
|
static = false
|
2020-04-22 19:07:51 +05:30
|
|
|
break
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
add_offense(node) if static
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
private
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def memoized?(node)
|
|
|
|
node.type == :or_asgn
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def dynamic?(node, memoized)
|
|
|
|
lambda_or_proc?(node) ||
|
|
|
|
named_block?(node) ||
|
|
|
|
instance_method_definition?(node) ||
|
|
|
|
unmemoized_class_method_definition?(node, memoized)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def lambda_or_proc?(node)
|
|
|
|
node.lambda_or_proc?
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def named_block?(node)
|
|
|
|
return unless node.block_type?
|
|
|
|
|
|
|
|
!NON_METHOD_DEFINITIONS.include?(node.method_name) # rubocop:disable Rails/NegateInclude
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def instance_method_definition?(node)
|
|
|
|
node.type == :def
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def unmemoized_class_method_definition?(node, memoized)
|
|
|
|
node.type == :defs && !memoized
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|