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

Register an offence in the case of the string without zone #3951

Merged
merged 6 commits into from
Jan 24, 2017

Conversation

sinsoku
Copy link
Contributor

@sinsoku sinsoku commented Jan 22, 2017

Using the string without zone is dangerous. It doesn't use Rails time zone, use the system timezone.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

def safe_to_time?(node)
receiver, _method_name, *args = *node
if receiver.str_type?
zone_regexp = /[\+-]{1}[\d:]+$/
Copy link
Collaborator

@Drenmi Drenmi Jan 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching exactly one is the default behaviour, so you shouldn't need the {1} quantifier. 😀 Also + does not need to be escaped in a list, so the whole thing could be simplified to:

[+-][\d:]+$

(My personal preference is for regular expressions to be stored in constants, but I don't think there's a convention for this in the project.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you can probably do \z instead of $. $ means end of line. \z means end of string.

irb(main):001:0> "John\nJacob\nJingleheimer\nSchmidt" =~ /^Jacob$/
=> 5
irb(main):002:0> "John\nJacob\nJingleheimer\nSchmidt" =~ /^Jacob\z/
=> nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I fixed.

zone_regexp = /[\+-]{1}[\d:]+$/
receiver.str_content.match(zone_regexp)
else
args.length == 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be: args.one? 😀

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args.length == 1 and args.one? are not equal.

http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-one-3F

For example

[1] pry(main)> [].one?
=> false
[2] pry(main)> [1].one?
=> true
[3] pry(main)> [false].one?
=> false
[4] pry(main)> [1, nil, nil].one?
=> true
[5] pry(main)> 

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's even more robust in this case, as we only care about nodes. 😀

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 22, 2017

You should also add a changelog entry.

@@ -6,6 +6,7 @@

* [#3915](https://github.com/bbatsov/rubocop/issues/3915): Make configurable whitelist for `Lint/SafeNavigationChain` cop. ([@pocke][])
* [#3944](https://github.com/bbatsov/rubocop/issues/3944): Allow keyword arguments in `Style/RaiseArgs` cop. ([@mikegee][])
* [#3951](https://github.com/bbatsov/rubocop/pull/3951): Make `Rails/Date` cop to register an offence for the string without zone. ([@sinsoku][])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the string -> a string
zone -> timezone

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to add yourself to the end of this file (that's why the build is failing).

@@ -49,6 +49,20 @@
end
end

context 'when the string literal with zone' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a string literal with timezone

end
end

context 'when the string literal without zone' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a string literal

@sinsoku
Copy link
Contributor Author

sinsoku commented Jan 24, 2017

I fixed the grammar and CHANGELOG.

@bbatsov bbatsov merged commit 406b4ef into rubocop:master Jan 24, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Jan 24, 2017

👍

@sinsoku sinsoku deleted the rails_date_with_strings branch January 29, 2017 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants