Class: RuboCop::Cop::Asjer::NoDefaultTranslation

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/asjer/no_default_translation.rb

Overview

Checks for translation calls that use the default: option.

Using default: bypasses the locale system and can lead to inconsistencies. Define all translations in the locale files instead.

Examples:

# bad
t('some.key', default: 'fallback text')
I18n.t('some.key', default: 'fallback')
I18n.translate('some.key', default: t('other.key'))

# good
t('some.key')
I18n.t('some.key')

Constant Summary collapse

MSG =
'Define translations in locale files instead of using `default:`.'
RESTRICT_ON_SEND =
%i[t translate].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rubocop/cop/asjer/no_default_translation.rb', line 34

def on_send(node)
  translation_with_default?(node) do |default_pair|
    add_offense(default_pair) do |corrector|
      corrector.remove(removal_range(default_pair))
    end
  end
end

#translation_with_default?(node) ⇒ Object



30
31
32
# File 'lib/rubocop/cop/asjer/no_default_translation.rb', line 30

def_node_matcher :translation_with_default?, <<~PATTERN
  (send {nil? (const {nil? cbase} :I18n)} {:t :translate} ... (hash <$(pair (sym :default) _) ...>))
PATTERN