Skip to content

Commit

Permalink
[Fix #725] Fix an incorrect autocorrect for Rails/DotSeparatedKeys
Browse files Browse the repository at this point in the history
Fixes #725.

This PR fixes an incorrect autocorrect for `Rails/DotSeparatedKeys`
when a key starts with dot.
  • Loading branch information
koic committed Jun 21, 2022
1 parent b671caa commit d085218
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#725](https://github.com/rubocop/rubocop-rails/issues/725): Fix an incorrect autocorrect for `Rails/DotSeparatedKeys` when a key starts with dot. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/dot_separated_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def should_convert_scope?(scope_node)
end

def new_key(key_node, scope_node)
"'#{scopes(scope_node).map(&:value).join('.')}.#{key_node.value}'"
"'#{scopes(scope_node).map(&:value).join('.')}.#{key_node.value}'".squeeze('.')
end

def scopes(scope_node)
Expand Down
3 changes: 3 additions & 0 deletions spec/rubocop/cop/rails/dot_separated_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
^^^^^^^^^^^^^^^^^^^ Use the dot-separated keys instead of specifying the `:scope` option.
I18n.t :key, scope: :one
^^^^^^^^^^^ Use the dot-separated keys instead of specifying the `:scope` option.
I18n.t '.key', scope: :one
^^^^^^^^^^^ Use the dot-separated keys instead of specifying the `:scope` option.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -30,6 +32,7 @@
I18n.t 'one.two.key'
I18n.t 'one.two.key'
I18n.t 'one.key'
I18n.t 'one.key'
RUBY
end

Expand Down

0 comments on commit d085218

Please sign in to comment.