Skip to content

Commit ce6056d

Browse files
committed
[Fix #1108] Fix an incorrect autocorrect for Rails/TimeZone
Fixes #1108. This PR fixes an incorrect autocorrect for `Rails/TimeZone` when using `String#to_time`.
1 parent fc9a15a commit ce6056d

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1108](https://github.com/rubocop/rubocop-rails/issues/1108): Fix an incorrect autocorrect for `Rails/TimeZone` when using `String#to_time`. ([@koic][])

lib/rubocop/cop/rails/time_zone.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ 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-
autocorrect(corrector, node)
72+
corrector.replace(node, "Time.zone.parse(#{node.receiver.source})")
7373
end
7474
end
7575

spec/rubocop/cop/rails/time_zone_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@
129129
"2012-03-02 16:05:37".to_time
130130
^^^^^^^ Do not use `String#to_time` without zone. Use `Time.zone.parse` instead.
131131
RUBY
132+
133+
expect_correction(<<~RUBY)
134+
Time.zone.parse("2012-03-02 16:05:37")
135+
RUBY
132136
end
133137

134138
it 'does not register an offense for `to_time` without receiver' do

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy