-
-
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.
Merge pull request #280 from fatkodima/short-i18n-cop
Add new `Rails/ShortI18n` cop
- Loading branch information
Showing
7 changed files
with
253 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,76 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop enforces that short forms of `I18n` methods are used: | ||
# `t` instead of `translate` and `l` instead of `localize`. | ||
# | ||
# This cop has two different enforcement modes. When the EnforcedStyle | ||
# is conservative (the default) then only `I18n.translate` and `I18n.localize` | ||
# calls are added as offenses. | ||
# | ||
# When the EnforcedStyle is aggressive then all `translate` and `localize` calls | ||
# without a receiver are added as offenses. | ||
# | ||
# @example | ||
# # bad | ||
# I18n.translate :key | ||
# I18n.localize Time.now | ||
# | ||
# # good | ||
# I18n.t :key | ||
# I18n.l Time.now | ||
# | ||
# @example EnforcedStyle: conservative (default) | ||
# # good | ||
# translate :key | ||
# localize Time.now | ||
# t :key | ||
# l Time.now | ||
# | ||
# @example EnforcedStyle: aggressive | ||
# # bad | ||
# translate :key | ||
# localize Time.now | ||
# | ||
# # good | ||
# t :key | ||
# l Time.now | ||
# | ||
class ShortI18n < Cop | ||
include ConfigurableEnforcedStyle | ||
|
||
MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.' | ||
|
||
PREFERRED_METHODS = { | ||
translate: :t, | ||
localize: :l | ||
}.freeze | ||
|
||
def_node_matcher :long_i18n?, <<~PATTERN | ||
(send {nil? (const nil? :I18n)} ${:translate :localize} ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return if style == :conservative && !node.receiver | ||
|
||
long_i18n?(node) do |method_name| | ||
good_method = PREFERRED_METHODS[method_name] | ||
message = format(MSG, good_method: good_method, bad_method: method_name) | ||
|
||
add_offense(node, location: :selector, message: message) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
long_i18n?(node) do |method_name| | ||
lambda do |corrector| | ||
corrector.replace(node.loc.selector, PREFERRED_METHODS[method_name]) | ||
end | ||
end | ||
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,91 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ShortI18n, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
shared_examples_for 'ShortI18n cop' do | ||
it 'registers an offense and corrects when using `I18n.translate`' do | ||
expect_offense(<<~RUBY) | ||
I18n.translate :key | ||
^^^^^^^^^ Use `t` instead of `translate`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
I18n.t :key | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `I18n.localize`' do | ||
expect_offense(<<~RUBY) | ||
I18n.localize Time.now | ||
^^^^^^^^ Use `l` instead of `localize`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
I18n.l Time.now | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `I18n.t`' do | ||
expect_no_offenses('I18n.t :key') | ||
end | ||
|
||
it 'does not register an offense when using `I18n.l`' do | ||
expect_no_offenses('I18n.l Time.now') | ||
end | ||
|
||
it 'does not register an offense when using `t`' do | ||
expect_no_offenses('t :key') | ||
end | ||
|
||
it 'does not register an offense when using `l`' do | ||
expect_no_offenses('l Time.now') | ||
end | ||
end | ||
|
||
context 'when EnforcedStyle set to conservative' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'conservative' } | ||
end | ||
|
||
it_behaves_like 'ShortI18n cop' | ||
|
||
it 'does not register an offense when using `translate`' do | ||
expect_no_offenses('translate :key') | ||
end | ||
|
||
it 'does not register an offense when using `localize`' do | ||
expect_no_offenses('localize Time.now') | ||
end | ||
end | ||
|
||
context 'when EnforcedStyle set to aggressive' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'aggressive' } | ||
end | ||
|
||
it_behaves_like 'ShortI18n cop' | ||
|
||
it 'registers an offense and corrects when using `translate`' do | ||
expect_offense(<<~RUBY) | ||
translate :key | ||
^^^^^^^^^ Use `t` instead of `translate`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
t :key | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `localize`' do | ||
expect_offense(<<~RUBY) | ||
localize Time.now | ||
^^^^^^^^ Use `l` instead of `localize`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
l Time.now | ||
RUBY | ||
end | ||
end | ||
end |