Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Dec 25, 2021
1 parent 5e874cf commit 4bfbea5
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 15 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@
* [#52](https://github.com/rubocop/rubocop-rails/issues/52): Add new `Rails/AfterCommitOverride` cop. ([@fatkodima][])
* [#323](https://github.com/rubocop/rubocop-rails/pull/323): Add new `Rails/OrderById` cop. ([@fatkodima][])
* [#274](https://github.com/rubocop/rubocop-rails/pull/274): Add new `Rails/WhereNot` cop. ([@fatkodima][])
* [#326](https://github.com/rubocop-hq/rubocop-rails/pull/326): Add new `Rails/I18nLazyLookup` cop. ([@fatkodima][])
* [#311](https://github.com/rubocop/rubocop-rails/issues/311): Make `Rails/HelperInstanceVariable` aware of memoization. ([@koic][])
* [#332](https://github.com/rubocop/rubocop-rails/issues/332): Fix `Rails/ReflectionClassName` cop false negative when relation had a scope parameter. ([@bubaflub][])

Expand Down
1 change: 1 addition & 0 deletions changelog/new_add_new_i18n_lazy_lookup_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#326](https://github.com/rubocop/rubocop-rails/pull/326): Add new `Rails/I18nLazyLookup` cop. ([@fatkodima][])
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Rails/I18nLazyLookup:
Description: 'Checks for places where I18n "lazy" lookup can be used.'
StyleGuide: 'https://rails.rubystyle.guide/#lazy-lookup'
Enabled: pending
VersionAdded: '2.8'
VersionAdded: '<<next>>'
Include:
- 'controllers/**/*'

Expand Down
1 change: 0 additions & 1 deletion docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ based on the https://rails.rubystyle.guide/[Rails Style Guide].
* xref:cops_rails.adoc#railshelperinstancevariable[Rails/HelperInstanceVariable]
* xref:cops_rails.adoc#railshttppositionalarguments[Rails/HttpPositionalArguments]
* xref:cops_rails.adoc#railshttpstatus[Rails/HttpStatus]
* xref:cops_rails.adoc#railsi18nlazylookup[Rails/I18nLazyLookup]
* xref:cops_rails.adoc#railsi18nlocaleassignment[Rails/I18nLocaleAssignment]
* xref:cops_rails.adoc#railsignoredskipactionfilteroption[Rails/IgnoredSkipActionFilterOption]
* xref:cops_rails.adoc#railsindexby[Rails/IndexBy]
Expand Down
17 changes: 8 additions & 9 deletions lib/rubocop/cop/rails/i18n_lazy_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ class I18nLazyLookup < Base

def on_send(node)
translate_call?(node) do |key_node|
key = key_node.value
return if key.to_s.start_with?('.')

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

Expand All @@ -57,16 +59,13 @@ def on_send(node)
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
action_node = node.each_ancestor(:def).first
return unless action_node && node_visibility(action_node) == :public

class_node = node.each_ancestor(:class).first
controller = class_node if class_node && class_node.identifier.source.end_with?('Controller')
controller_node = node.each_ancestor(:class).first
return unless controller_node && controller_node.identifier.source.end_with?('Controller')

[controller, action]
[controller_node, action_node]
end

def get_scoped_key(key_node, controller, action)
Expand Down
4 changes: 1 addition & 3 deletions spec/rubocop/cop/rails/i18n_lazy_lookup_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::I18nLazyLookup do
subject(:cop) { described_class.new }

RSpec.describe RuboCop::Cop::Rails::I18nLazyLookup, :config do
it 'registers an offense and corrects when using translation helpers with the key scoped to controller and action' do
expect_offense(<<~RUBY)
class FooController
Expand Down

0 comments on commit 4bfbea5

Please sign in to comment.