Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit style to Rails/I18nLazyLookup #1052

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_explicit_style_to_i18n_lazy_lookup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1052](https://github.com/rubocop/rubocop-rails/pull/1052): Add explicit style to `Rails/I18nLazyLookup`. ([@sunny][])
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ Rails/I18nLazyLookup:
Reference: 'https://guides.rubyonrails.org/i18n.html#lazy-lookup'
Enabled: pending
VersionAdded: '2.14'
EnforcedStyle: lazy
SupportedStyles:
- lazy
- explicit
Include:
- 'app/controllers/**/*.rb'

Expand Down
76 changes: 63 additions & 13 deletions lib/rubocop/cop/rails/i18n_lazy_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ module Cop
module Rails
# Checks for places where I18n "lazy" lookup can be used.
#
# @example
# 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.
#
# @example EnforcedStyle: lazy (default)
# # en.yml
# # en:
# # books:
Expand All @@ -28,11 +34,29 @@ module Rails
# end
# end
#
# @example 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
#
class I18nLazyLookup < Base
include ConfigurableEnforcedStyle
include VisibilityHelp
extend AutoCorrector

MSG = 'Use "lazy" lookup for the text used in controllers.'
MSG = 'Use %<style>s lookup for the text used in controllers.'

RESTRICT_ON_SEND = %i[translate t].freeze

Expand All @@ -42,23 +66,45 @@ class I18nLazyLookup < Base

def on_send(node)
translate_call?(node) do |key_node|
key = key_node.value
return if key.to_s.start_with?('.')
case style
when :lazy
handle_lazy_style(node, key_node)
when :explicit
handle_explicit_style(node, key_node)
end
end
end

private

controller, action = controller_and_action(node)
return unless controller && action
def handle_lazy_style(node, key_node)
key = key_node.value
return if key.to_s.start_with?('.')

scoped_key = get_scoped_key(key_node, controller, action)
return unless key == scoped_key
controller, action = controller_and_action(node)
return unless controller && action

add_offense(key_node) do |corrector|
unscoped_key = key_node.value.to_s.split('.').last
corrector.replace(key_node, "'.#{unscoped_key}'")
end
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

private
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 controller_and_action(node)
action_node = node.each_ancestor(:def).first
Expand Down Expand Up @@ -90,6 +136,10 @@ def controller_path(controller)

path.delete_suffix('Controller').underscore
end

def message(_range)
format(MSG, style: style)
end
end
end
end
Expand Down
Loading