File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change
1
+ * [ #869 ] ( https://github.com/rubocop/rubocop-rails/pull/869 ) : Fix false-positives that non Rails formats are offended on ` Rails/ToSWithArgument ` . ([ @r7kamura ] [ ] )
Original file line number Diff line number Diff line change @@ -21,20 +21,57 @@ class ToSWithArgument < Base
21
21
extend AutoCorrector
22
22
extend TargetRailsVersion
23
23
24
+ # These types are defined by the following files in ActiveSupport:
25
+ # lib/active_support/core_ext/array/conversions.rb
26
+ # lib/active_support/core_ext/date/conversions.rb
27
+ # lib/active_support/core_ext/date_time/conversions.rb
28
+ # lib/active_support/core_ext/numeric/conversions.rb
29
+ # lib/active_support/core_ext/range/conversions.rb
30
+ # lib/active_support/core_ext/time/conversions.rb
31
+ # lib/active_support/time_with_zone.rb
32
+ EXTENDED_FORMAT_TYPES = Set . new (
33
+ %i[
34
+ currency
35
+ db
36
+ delimited
37
+ human
38
+ human_size
39
+ inspect
40
+ iso8601
41
+ long
42
+ long_ordinal
43
+ nsec
44
+ number
45
+ percentage
46
+ phone
47
+ rfc822
48
+ rounded
49
+ short
50
+ time
51
+ usec
52
+ ]
53
+ )
54
+
24
55
MSG = 'Use `to_formatted_s` instead.'
25
56
26
57
RESTRICT_ON_SEND = %i[ to_s ] . freeze
27
58
28
59
minimum_target_rails_version 7.0
29
60
30
61
def on_send ( node )
31
- return if node . arguments . empty?
62
+ return unless rails_extended_to_s? ( node )
32
63
33
64
add_offense ( node . loc . selector ) do |corrector |
34
65
corrector . replace ( node . loc . selector , 'to_formatted_s' )
35
66
end
36
67
end
37
68
alias on_csend on_send
69
+
70
+ private
71
+
72
+ def rails_extended_to_s? ( node )
73
+ node . first_argument &.sym_type? && EXTENDED_FORMAT_TYPES . include? ( node . first_argument . value )
74
+ end
38
75
end
39
76
end
40
77
end
Original file line number Diff line number Diff line change 9
9
end
10
10
end
11
11
12
+ context 'with unrelated argument' do
13
+ it 'does not register an offense' do
14
+ expect_no_offenses ( <<~RUBY )
15
+ 10.to_s(2)
16
+ RUBY
17
+ end
18
+ end
19
+
12
20
context 'with argument' do
13
21
it 'registers an offense' do
14
22
expect_offense ( <<~RUBY )
You can’t perform that action at this time.
0 commit comments