Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails/TimeZone autofix broken for "DateTime.now" #81

Closed
kamilpp opened this issue Jun 26, 2019 · 1 comment
Closed

Rails/TimeZone autofix broken for "DateTime.now" #81

kamilpp opened this issue Jun 26, 2019 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@kamilpp
Copy link

kamilpp commented Jun 26, 2019

So I had somewhere in the code DateTime.now. When running the linter (rubocop command) I was getting this error:

Rails/TimeZone: Do not use DateTime.now without zone. Use one of Time.zone.now, DateTime.current, DateTime.now.in_time_zone, DateTime.now.utc, DateTime.now.getlocal, DateTime.now.xmlschema, DateTime.now.iso8601, DateTime.now.jisx0301, DateTime.now.rfc3339, DateTime.now.httpdate, DateTime.now.to_i, DateTime.now.to_f instead.

which is correct.

But, when I run rubocop --safe-auto-correct that string get changed to DateTime.zone.now which is incorrect – DateTime does not implement zone method:

$ DateTime.zone
NoMethodError: undefined method `zone' for DateTime:Class

Expected behavior

After running autofix, I expect DateTime.now changes to Time.zone.now or something similar.

RuboCop version

$ bundle exec rubocop -V                                                                              
0.72.0 (using Parser 2.6.3.0, running on ruby 2.4.1 x86_64-darwin18)
@koic koic transferred this issue from rubocop/rubocop Jun 26, 2019
@koic
Copy link
Member

koic commented Jun 26, 2019

Duplicate with #67. Also, Rails cops issue has been moved to rubocop-hq/rubocop-rails repository. Thank you.

@koic koic closed this as completed Jun 26, 2019
@koic koic added the duplicate This issue or pull request already exists label Jun 27, 2019
koic added a commit to koic/rubocop-rails that referenced this issue Jul 5, 2019
Fixes rubocop#67.
Closes rubocop#71.

This PR fixes an incorrect auto-correct for `Rails/TimeZone`
when using `DateTime`.

```diff
- DateTime.new
+ DateTime.zone.new
```

```ruby
DateTime.zone.new # NoMethodError: undefined method `zone' for
DateTime:Class
```

This PR changes to ignore `DateTime` with the following as
background.

`DateTime` is not mentioned in The Rails Style Guide and
`Rails/TimeZone` cop's examples.

- https://rails.rubystyle.guide/#time-now
- https://docs.rubocop.org/projects/rails/en/stable/cops_rails/#railstimezone

Recently, rubocop#81 and rubocop#82 have been feedback.
I think breaking by auto-correction should be fixed with
the highest priority in this case.

Also, `DateTime` and `Time` are not replaced from `DateTime` to `Time`
by auto-correct because they are different objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants