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

Add new Rails/RelativeDateConstant cop #4003

Merged
merged 7 commits into from
Feb 10, 2017

Conversation

sinsoku
Copy link
Contributor

@sinsoku sinsoku commented Feb 2, 2017

We probably do not use relative date from system startup time as constant. This cop checks it.


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).

@mikegee
Copy link
Contributor

mikegee commented Feb 2, 2017

.ago and .from_now are the relative date methods I usually see in code.

module RuboCop
module Cop
module Rails
# This cop checks whether constant are relative date.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The description of cop should be extended here.

# end
# end
class RelativeDateConstant < Cop
MSG = 'Do not use `%s` in constant variable, because they ' \
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not assign %s to constants as it will be evaluated only once.

MSG = 'Do not use `%s` in constant variable, because they ' \
'are evaluated only once at system running.'.freeze

BAD_METHODS = %i(ago from_now since until).freeze
Copy link
Collaborator

Choose a reason for hiding this comment

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

RELATIVE_DATE_METHODS

end
end

def bad_method_without_args?(node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

I'd drop without_args from the name of the method

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 3, 2017

Looks OK to me apart from my small remarks. I guess it can even have auto-correct.

@sinsoku
Copy link
Contributor Author

sinsoku commented Feb 9, 2017

Thanks for review comments. I fixed it.

@bbatsov bbatsov merged commit 36a9c8f into rubocop:master Feb 10, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Feb 10, 2017

👍

@sinsoku sinsoku deleted the relative_date_constant branch February 10, 2017 03:11
@jfelchner
Copy link
Contributor

@bbatsov @sinsoku one of my favorite things is getting a new cop that solves a problem I didn't even know was a problem. This is fantastic. Thank you.

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.

4 participants