-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks for places where I18n "lazy" lookup can be used. | ||
# | ||
# @example | ||
# # 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 | ||
# | ||
class I18nLazyLookup < Base | ||
include VisibilityHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Use "lazy" lookup for the texts used in controllers.' | ||
|
||
def_node_matcher :translate_call?, <<~PATTERN | ||
(send nil? {:translate :t} ${sym_type? str_type?} ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
translate_call?(node) do |key_node| | ||
controller, action = controller_and_action(node) | ||
return unless controller && action | ||
|
||
key = key_node.value | ||
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 | ||
end | ||
|
||
private | ||
|
||
def controller_and_action(node) | ||
controller = nil | ||
action = nil | ||
|
||
def_node = node.each_ancestor(:def).first | ||
action = def_node if def_node && node_visibility(def_node) == :public | ||
|
||
class_node = node.each_ancestor(:class).first | ||
controller = class_node if class_node && class_node.identifier.source.end_with?('Controller') | ||
|
||
[controller, action] | ||
end | ||
|
||
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 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 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::I18nLazyLookup do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when using translation helpers with the key scoped to controller and action' do | ||
expect_offense(<<~RUBY) | ||
class FooController | ||
def action | ||
t 'foo.action.key' | ||
^^^^^^^^^^^^^^^^ Use "lazy" lookup for the texts used in controllers. | ||
translate 'foo.action.key' | ||
^^^^^^^^^^^^^^^^ Use "lazy" lookup for the texts used in controllers. | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class FooController | ||
def action | ||
t '.key' | ||
translate '.key' | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when translation methods scoped to `I18n`' do | ||
expect_no_offenses(<<~RUBY) | ||
class FooController | ||
def action | ||
I18n.t 'foo.action.key' | ||
I18n.translate 'foo.action.key' | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not inside controller' do | ||
expect_no_offenses(<<~RUBY) | ||
class FooService | ||
def do_something | ||
t 'foo_service.do_something.key' | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not inside controller action' do | ||
expect_no_offenses(<<~RUBY) | ||
class FooController | ||
private | ||
def action | ||
t 'foo.action.key' | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when translating key not scoped to controller and action' do | ||
expect_no_offenses(<<~RUBY) | ||
class FooController | ||
def action | ||
t 'one.two.key' | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using "lazy" translation' do | ||
expect_no_offenses(<<~RUBY) | ||
class FooController | ||
def action | ||
t '.key' | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when translation key is not a string nor a symbol' do | ||
expect_no_offenses(<<~RUBY) | ||
class FooController | ||
def action | ||
t ['foo.action.key'] | ||
t key | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'handles scoped controllers' do | ||
expect_offense(<<~RUBY) | ||
module Bar | ||
class FooController | ||
def action | ||
t 'bar.foo.action.key' | ||
^^^^^^^^^^^^^^^^^^^^ Use "lazy" lookup for the texts used in controllers. | ||
end | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
module Bar | ||
class FooController | ||
def action | ||
t '.key' | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end |