Skip to content

Commit 650e783

Browse files
authored
Merge pull request #1251 from Earlopain/make-time-zone-aware-of-safe-navigation
[Fix #1200] Make `Rails/TimeZone` aware of safe navigation
2 parents 2f893da + 4fd02a1 commit 650e783

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1200](https://github.com/rubocop/rubocop-rails/issues/1200): Make `Rails/TimeZone` aware of safe navigation. ([@earlopain][])

lib/rubocop/cop/rails/time_zone.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ def on_send(node)
6969
return if !node.receiver&.str_type? || !node.method?(:to_time)
7070

7171
add_offense(node.loc.selector, message: MSG_STRING_TO_TIME) do |corrector|
72-
corrector.replace(node, "Time.zone.parse(#{node.receiver.source})")
72+
corrector.replace(node, "Time.zone.parse(#{node.receiver.source})") unless node.csend_type?
7373
end
7474
end
75+
alias on_csend on_send
7576

7677
private
7778

spec/rubocop/cop/rails/time_zone_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@
135135
RUBY
136136
end
137137

138+
it 'registers an offense for `to_time` with safe navigation' do
139+
expect_offense(<<~RUBY)
140+
"2012-03-02 16:05:37"&.to_time
141+
^^^^^^^ Do not use `String#to_time` without zone. Use `Time.zone.parse` instead.
142+
RUBY
143+
144+
expect_no_corrections
145+
end
146+
138147
it 'does not register an offense for `to_time` without receiver' do
139148
expect_no_offenses(<<~RUBY)
140149
to_time

0 commit comments

Comments
 (0)