-
-
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 #747 from r7kamura/feature/to-s-with-argument
Add `Rails/ToSWithArgument` cop
- Loading branch information
Showing
5 changed files
with
107 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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Identifies passing any argument to `#to_s`. | ||
# | ||
# @safety | ||
# This cop is marked as unsafe because it may detect `#to_s` calls | ||
# that are not related to Active Support implementation. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# obj.to_s(:delimited) | ||
# | ||
# # good | ||
# obj.to_formatted_s(:delimited) | ||
# | ||
class ToSWithArgument < Base | ||
extend AutoCorrector | ||
extend TargetRailsVersion | ||
|
||
MSG = 'Use `to_formatted_s` instead.' | ||
|
||
RESTRICT_ON_SEND = %i[to_s].freeze | ||
|
||
minimum_target_rails_version 7.0 | ||
|
||
def on_send(node) | ||
return if node.arguments.empty? | ||
|
||
add_offense(node.loc.selector) do |corrector| | ||
corrector.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,58 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ToSWithArgument, :config, :rails70 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. | ||
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. | ||
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. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
1&.to_formatted_s(:delimited) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with argument on Rails 6.1', :rails61 do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
to_s | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
RUBY | ||
end | ||
end | ||
end |
@koic this line should be
to_s(:delimited)
, right? The context says "with argument", but there's no argument.