forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
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
114 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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks for the use of `I18n.locale=` method. | ||
# | ||
# The `locale` attribute persists for the rest of the Ruby runtime, potentially causing | ||
# unexpected behaviour at a later time. | ||
# Using `I18n.with_locale` ensures the code passed in block is the only place `I18n.locale` is affected. | ||
# It eliminates the possibility of a `locale` sticking around longer than intended. | ||
# | ||
# @example | ||
# # bad | ||
# I18n.locale = :fr | ||
# | ||
# # good | ||
# I18n.with_locale(:fr) do | ||
# end | ||
# | ||
class I18nLocaleAssignment < Base | ||
MSG = 'Use `I18n.with_locale` with blocks instead of `I18n.locale=`.' | ||
RESTRICT_ON_SEND = %i[locale=].freeze | ||
|
||
def_node_matcher :i18n_locale_assignement?, <<~PATTERN | ||
(send (const {nil? cbase} :I18n) :locale= ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless i18n_locale_assignement?(node) | ||
|
||
add_offense(node) | ||
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::I18nLocaleAssignment, :config do | ||
it 'registers an offense for `I18n.locale=`' do | ||
expect_offense(<<~RUBY) | ||
I18n.locale = :fr | ||
^^^^^^^^^^^^^^^^^ Use `I18n.with_locale` with blocks instead of `I18n.locale=`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for `::I18n.locale=`' do | ||
expect_offense(<<~RUBY) | ||
::I18n.locale = :fr | ||
^^^^^^^^^^^^^^^^^^^ Use `I18n.with_locale` with blocks instead of `I18n.locale=`. | ||
RUBY | ||
end | ||
|
||
it 'accepts `I18n.with_locale`' do | ||
expect_no_offenses(<<~RUBY) | ||
I18n.with_locale(:fr) do | ||
do_something | ||
end | ||
RUBY | ||
end | ||
end |