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
5 changed files
with
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#747](https://github.com/rubocop/rubocop-rails/pull/747): Add `Rails/ToSWithArgument` cop. ([@r7kamura][]) |
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 | ||
# Identifies passing any argument to `#to_s`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# to_s(:delimited) | ||
# | ||
# # good | ||
# to_formatted_s(:delimited) | ||
# | ||
class ToSWithArgument < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `to_formatted_s(...)` instead of `to_s(...)`.' | ||
|
||
RESTRICT_ON_SEND = %i[to_s].freeze | ||
|
||
def on_send(node) | ||
return if node.arguments.empty? | ||
|
||
add_offense(node) do |rewriter| | ||
rewriter.replace( | ||
node.loc.selector, | ||
'to_formatted_s' | ||
) | ||
end | ||
end | ||
alias on_csend on_send | ||
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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ToSWithArgument, :config do | ||
context 'without argument' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
to_s | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with argument' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
to_s(:delimited) | ||
^^^^^^^^^^^^^^^^ Use `to_formatted_s(...)` instead of `to_s(...)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
to_formatted_s(:delimited) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with argument and receiver' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
1.to_s(:delimited) | ||
^^^^^^^^^^^^^^^^^^ Use `to_formatted_s(...)` instead of `to_s(...)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
1.to_formatted_s(:delimited) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with argument and safe navigation operator' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
1&.to_s(:delimited) | ||
^^^^^^^^^^^^^^^^^^^ Use `to_formatted_s(...)` instead of `to_s(...)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
1&.to_formatted_s(:delimited) | ||
RUBY | ||
end | ||
end | ||
end |