Skip to content

Commit 5f38b74

Browse files
authored
Merge pull request #720 from koic/fix_a_false_negative_for_rails_to_formatted_s
[Fix #719] Fix a false negative for `Rails/FormattedS`
2 parents 5190a68 + 016968f commit 5f38b74

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#719](https://github.com/rubocop/rubocop-rails/issues/719): Fix a false negative for `Rails/FormattedS` when using safe navigation operator. ([@fatkodima][])

lib/rubocop/cop/rails/to_formatted_s.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def on_send(node)
3939
corrector.replace(node.loc.selector, style)
4040
end
4141
end
42+
alias on_csend on_send
4243
end
4344
end
4445
end

spec/rubocop/cop/rails/to_formatted_s_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
RUBY
1717
end
1818

19+
it 'registers and corrects an offense when using `to_formatted_s` with safe navigation operator' do
20+
expect_offense(<<~RUBY)
21+
time&.to_formatted_s(:db)
22+
^^^^^^^^^^^^^^ Use `to_fs` instead.
23+
RUBY
24+
25+
expect_correction(<<~RUBY)
26+
time&.to_fs(:db)
27+
RUBY
28+
end
29+
1930
it 'does not register an offense when using `to_fs`' do
2031
expect_no_offenses(<<~RUBY)
2132
time.to_fs(:db)
@@ -37,6 +48,17 @@
3748
RUBY
3849
end
3950

51+
it 'registers and corrects an offense when using `to_fs` with safe navigation operator' do
52+
expect_offense(<<~RUBY)
53+
time&.to_fs(:db)
54+
^^^^^ Use `to_formatted_s` instead.
55+
RUBY
56+
57+
expect_correction(<<~RUBY)
58+
time&.to_formatted_s(:db)
59+
RUBY
60+
end
61+
4062
it 'does not register an offense when using `to_formatted_s`' do
4163
expect_no_offenses(<<~RUBY)
4264
time.to_formatted_s(:db)

0 commit comments

Comments
 (0)