class RuboCop::Cop::Rails::I18nLazyLookup


end
end
redirect_to books_url, notice: t(‘books.create.success’)
# …
def create
class BooksController < ApplicationController
# good
end
end
redirect_to books_url, notice: t(‘.success’)
# …
def create
class BooksController < ApplicationController
# bad
@example EnforcedStyle: explicit
end
end
redirect_to books_url, notice: t(‘.success’)
# …
def create
class BooksController < ApplicationController
# good
end
end
redirect_to books_url, notice: t(‘books.create.success’)
# …
def create
class BooksController < ApplicationController
# bad
# success: Book created!
# create:
# books:
# en:
# en.yml
@example EnforcedStyle: lazy (default)
offenses.
When the EnforcedStyle is ‘explicit` then lazy lookups are added as
is `lazy` (the default), explicit lookups are added as offenses.
This cop has two different enforcement modes. When the EnforcedStyle
Checks for places where I18n “lazy” lookup can be used.

def controller_and_action(node)

def controller_and_action(node)
  action_node = node.each_ancestor(:def).first
  return unless action_node && node_visibility(action_node) == :public
  controller_node = node.each_ancestor(:class).first
  return unless controller_node && controller_node.identifier.source.end_with?('Controller')
  [controller_node, action_node]
end

def controller_path(controller)

def controller_path(controller)
  module_name = controller.parent_module_name
  controller_name = controller.identifier.source
  path = if module_name == 'Object'
           controller_name
         else
           "#{module_name}::#{controller_name}"
         end
  path.delete_suffix('Controller').underscore
end

def get_scoped_key(key_node, controller, action)

def get_scoped_key(key_node, controller, action)
  path = controller_path(controller).tr('/', '.')
  action_name = action.method_name
  key = key_node.value.to_s.split('.').last
  "#{path}.#{action_name}.#{key}"
end

def handle_explicit_style(node, key_node)

def handle_explicit_style(node, key_node)
  key = key_node.value
  return unless key.to_s.start_with?('.')
  controller, action = controller_and_action(node)
  return unless controller && action
  scoped_key = get_scoped_key(key_node, controller, action)
  add_offense(key_node) do |corrector|
    corrector.replace(key_node, "'#{scoped_key}'")
  end
end

def handle_lazy_style(node, key_node)

def handle_lazy_style(node, key_node)
  key = key_node.value
  return if key.to_s.start_with?('.')
  controller, action = controller_and_action(node)
  return unless controller && action
  scoped_key = get_scoped_key(key_node, controller, action)
  return unless key == scoped_key
  add_offense(key_node) do |corrector|
    unscoped_key = key_node.value.to_s.split('.').last
    corrector.replace(key_node, "'.#{unscoped_key}'")
  end
end

def message(_range)

def message(_range)
  format(MSG, style: style)
end

def on_send(node)

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