Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow up rubocop/rails-style-guide#314. This cop checks for consistent uses of `to_fs` or `to_formatted_s`, depending on the cop's configuration. ## EnforcedStyle: to_fs (default) ```ruby # bad time.to_formatted_s(:db) # good time.to_fs(:db) ``` ## EnforcedStyle: to_formatted_s ``` # bad time.to_fs(:db) # good time.to_formatted_s(:db) ```
- Loading branch information
Showing
6 changed files
with
130 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 @@ | ||
* [#691](https://github.com/rubocop/rubocop-rails/pull/691): Add new `Rails/ToFormattedS` cop. ([@koic][]) |
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks for consistent uses of `to_fs` or `to_formatted_s`, | ||
# depending on the cop's configuration. | ||
# | ||
# @example EnforcedStyle: to_fs (default) | ||
# | ||
# # bad | ||
# time.to_formatted_s(:db) | ||
# | ||
# # good | ||
# time.to_fs(:db) | ||
# | ||
# @example EnforcedStyle: to_formatted_s | ||
# | ||
# # bad | ||
# time.to_fs(:db) | ||
# | ||
# # good | ||
# time.to_formatted_s(:db) | ||
# | ||
class ToFormattedS < Base | ||
include ConfigurableEnforcedStyle | ||
extend AutoCorrector | ||
extend TargetRailsVersion | ||
|
||
minimum_target_rails_version 7.0 | ||
|
||
MSG = 'Use `%<prefer>s` instead.' | ||
RESTRICT_ON_SEND = %i[to_formatted_s to_fs].freeze | ||
|
||
def on_send(node) | ||
return if node.method?(style) | ||
|
||
add_offense(node.loc.selector, message: format(MSG, prefer: style)) do |corrector| | ||
corrector.replace(node.loc.selector, style) | ||
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,69 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ToFormattedS, :config do | ||
context 'Rails >= 7.0', :rails70 do | ||
context 'EnforcedStyle: to_fs' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'to_fs' } } | ||
|
||
it 'registers and corrects an offense when using `to_formatted_s`' do | ||
expect_offense(<<~RUBY) | ||
time.to_formatted_s(:db) | ||
^^^^^^^^^^^^^^ Use `to_fs` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
time.to_fs(:db) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `to_fs`' do | ||
expect_no_offenses(<<~RUBY) | ||
time.to_fs(:db) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'EnforcedStyle: to_formatted_s' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'to_formatted_s' } } | ||
|
||
it 'registers and corrects an offense when using `to_fs`' do | ||
expect_offense(<<~RUBY) | ||
time.to_fs(:db) | ||
^^^^^ Use `to_formatted_s` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
time.to_formatted_s(:db) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `to_formatted_s`' do | ||
expect_no_offenses(<<~RUBY) | ||
time.to_formatted_s(:db) | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
context 'Rails <= 6.1', :rails61 do | ||
context 'EnforcedStyle: to_fs' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'to_fs' } } | ||
|
||
it 'does not register an offense when using `to_formatted_s`' do | ||
expect_no_offenses(<<~RUBY) | ||
time.to_formatted_s(:db) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'EnforcedStyle: to_formatted_s' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'to_formatted_s' } } | ||
|
||
it 'does not register an offense when using `to_fs`' do | ||
expect_no_offenses(<<~RUBY) | ||
time.to_fs(:db) | ||
RUBY | ||
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