Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ruby_interpolation_in_translation.rb « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fec550bf7c61ee7e6023f34dd9ed31d7505b9684 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

module RuboCop
  module Cop
    class RubyInterpolationInTranslation < RuboCop::Cop::Base
      MSG = "Don't use ruby interpolation \#{} inside translated strings, instead use \%{}"

      TRANSLATION_METHODS = ':_ :s_ :N_ :n_'

      def_node_matcher :translation_method?, <<~PATTERN
        (send nil? {#{TRANSLATION_METHODS}} $dstr ...)
      PATTERN

      def_node_matcher :plural_translation_method?, <<~PATTERN
        (send nil? :n_ str $dstr ...)
      PATTERN

      def on_send(node)
        interpolation = translation_method?(node) || plural_translation_method?(node)
        return unless interpolation

        interpolation.descendants.each do |possible_violation|
          add_offense(possible_violation, message: MSG) if possible_violation.type != :str
        end
      end
    end
  end
end