-
-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Rails/ToSWithArgument
cop
#747
Conversation
0557ad9
to
dd41d92
Compare
Unfortunately, I think the gem cannot accept this cop because the risk of false positives is too high. For example, Rails doesn't support 42.to_s(16) # => "2a"
42.to_formatted_s(16) # => undefined method `to_formatted_s' for 42:Integer (NoMethodError) I also thought about this cop when implementing #691, but that's why I gave up. |
hmm, it seems to be present in Rails 7.0 or later, but Rails 6.1 does not. $ rails c
Loading development environment (Rails 7.0.3.1)
irb(main):001:0> 42.to_formatted_s(16)
=> "2a" If I specify |
Ah, OK 👍 It seems that there is no problem when Rails 7.0 or higher. I leave some review comments. |
config/default.yml
Outdated
Description: 'Identifies passing any argument to `#to_s`.' | ||
Enabled: pending | ||
Safe: false | ||
VersionAdded: '2.16' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VersionAdded: '2.16' | |
VersionAdded: '<<next>>' |
# @example | ||
# | ||
# # bad | ||
# to_s(:delimited) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# to_s(:delimited) | |
# obj.to_s(:delimited) |
# to_s(:delimited) | ||
# | ||
# # good | ||
# to_formatted_s(:delimited) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# to_formatted_s(:delimited) | |
# obj.to_formatted_s(:delimited) |
add_offense(node) do |rewriter| | ||
rewriter.replace( | ||
node.loc.selector, | ||
'to_formatted_s' | ||
) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's my two cents. Can you tweak it?
add_offense(node) do |rewriter| | |
rewriter.replace( | |
node.loc.selector, | |
'to_formatted_s' | |
) | |
end | |
add_offense(node) do |corrector| | |
corrector.replace(node.loc.selector, 'to_formatted_s') | |
end |
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
to_s(:delimited) | ||
^^^^^^^^^^^^^^^^ Use `to_formatted_s(...)` instead of `to_s(...)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to show a concrete value.
^^^^^^^^^^^^^^^^ Use `to_formatted_s(...)` instead of `to_s(...)`. | |
^^^^^^^^^^^^^^^^ Use `to_formatted_s(:delimited)` instead of `to_s(:delimited)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, this time I've decided to change it to the same format as Rails/ToFormattedS
.
1.to_s(:delimited)
^^^^ Use `to_formatted_s` instead.
1&.to_formatted_s(:delimited) | ||
RUBY | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests for :rails61
?
module Cop | ||
module Rails | ||
# Identifies passing any argument to `#to_s`. | ||
# |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add @safety
to explain why this cop is unsafe? e.g. #575
This looks good to me. Can you squash your commits into one? |
88abf84
to
4f08fc2
Compare
Thanks for the thoughtful review! By the way, I feel like if you set up the GitHub repository to always squash merge, we wouldn't have to squash every time on the contributor's side, and it would be easier to review, but don't you do that or something? |
I prefer "Create a merge commit" where commit (hash) is maintained. Development is transient, but maintenance is permanent as long as it lasts. |
to_s(format)
is deprecated in favor ofto_formatted_s(format)
at Rails 7.0.to_s(format)
in favor ofto_formatted_s(format)
rails/rails#43772To ease the migration from Rails 6.1 to 7.0, I added
Rails/ToSWithArgument
cop with autocorrect support in this pull request.Yes, I know
to_fs(format)
is the default replacement forto_fs(format)
from Rails 7.0,but
to_fs
is not implemented at Rails 6.1, and it will be taken care of byRails/ToFormattedS
cop.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.