Skip to content

Commit

Permalink
Add new Rails/ToFormattedS cop
Browse files Browse the repository at this point in the history
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
koic committed Apr 20, 2022
1 parent 944394b commit b840311
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_new_rails_to_formatted_s_cop.md
@@ -0,0 +1 @@
* [#691](https://github.com/rubocop/rubocop-rails/pull/691): Add new `Rails/ToFormattedS` cop. ([@koic][])
9 changes: 9 additions & 0 deletions config/default.yml
Expand Up @@ -905,6 +905,15 @@ Rails/TimeZoneAssignment:
- spec/**/*.rb
- test/**/*.rb

Rails/ToFormattedS:
Description: 'Checks for consistent uses of `to_fs` or `to_formatted_s`.'
Enabled: pending
EnforcedStyle: to_fs
SupportedStyles:
- to_fs
- to_formatted_s
VersionAdded: '<<next>>'

Rails/TransactionExitStatement:
Description: 'Avoid the usage of `return`, `break` and `throw` in transaction blocks.'
Enabled: pending
Expand Down
45 changes: 45 additions & 0 deletions lib/rubocop/cop/rails/to_formatted_s.rb
@@ -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
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Expand Up @@ -107,6 +107,7 @@
require_relative 'rails/table_name_assignment'
require_relative 'rails/time_zone'
require_relative 'rails/time_zone_assignment'
require_relative 'rails/to_formatted_s'
require_relative 'rails/transaction_exit_statement'
require_relative 'rails/uniq_before_pluck'
require_relative 'rails/unique_validation_without_index'
Expand Down
69 changes: 69 additions & 0 deletions spec/rubocop/cop/rails/to_formatted_s_spec.rb
@@ -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 `formatted_s`' 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 `formatted_s`' do
expect_no_offenses(<<~RUBY)
time.to_fs(:db)
RUBY
end
end
end
end
4 changes: 4 additions & 0 deletions spec/support/shared_contexts.rb
Expand Up @@ -23,3 +23,7 @@
RSpec.shared_context 'with Rails 6.1', :rails61 do
let(:rails_version) { 6.1 }
end

RSpec.shared_context 'with Rails 7.0', :rails70 do
let(:rails_version) { 7.0 }
end

0 comments on commit b840311

Please sign in to comment.