Class: RuboCop::Cop::Rails::I18nLazyLookup

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, VisibilityHelp
Defined in:
lib/rubocop/cop/rails/i18n_lazy_lookup.rb

Overview

Checks for places where I18n "lazy" lookup can be used.

This cop has two different enforcement modes. When the EnforcedStyle is lazy (the default), explicit lookups are added as offenses.

When the EnforcedStyle is explicit then lazy lookups are added as offenses.

Examples:

EnforcedStyle: lazy (default)

# en.yml
# en:
#   books:
#     create:
#       success: Book created!

# bad
class BooksController < ApplicationController
  def create
    # ...
    redirect_to books_url, notice: t('books.create.success')
  end
end

# good
class BooksController < ApplicationController
  def create
    # ...
    redirect_to books_url, notice: t('.success')
  end
end

EnforcedStyle: explicit

# bad
class BooksController < ApplicationController
  def create
    # ...
    redirect_to books_url, notice: t('.success')
  end
end

# good
class BooksController < ApplicationController
  def create
    # ...
    redirect_to books_url, notice: t('books.create.success')
  end
end

Constant Summary collapse

MSG =
'Use %<style>s lookup for the text used in controllers.'
RESTRICT_ON_SEND =
%i[translate t].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/rubocop/cop/rails/i18n_lazy_lookup.rb', line 67

def on_send(node)
  translate_call?(node) do |key_node|
    case style
    when :lazy
      handle_lazy_style(node, key_node)
    when :explicit
      handle_explicit_style(node, key_node)
    end
  end
end