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 Rails/DurationArithmetic cop #571

Merged
merged 1 commit into from
Oct 24, 2021
Merged

Conversation

pirj
Copy link
Member

@pirj pirj commented Oct 5, 2021

Backed by a proposed ✔️ guideline https://rails.rubystyle.guide/#duration-arithmetic
rubocop/rails-style-guide#282, rubocop/rails-style-guide#293

TODO:


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).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

# fair
Date.yesterday + 3.days
created_at - 1.minute
3.days - 1.hour
Copy link
Member Author

Choose a reason for hiding this comment

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

1.hour.until(3.days) results in an error.

Time.current + 2.days

# fair
Date.yesterday + 3.days
Copy link
Member Author

Choose a reason for hiding this comment

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

I personally prefer 3.days.since(Date.yesterday), but sometimes it's more than just two, and:

  • it gets unweildy 1.minute.until(3.days.since(Date.yesterday)) vs Date.yesterday + 3.days - 1.munute
  • pattern's complexity would skyrocket

config/default.yml Outdated Show resolved Hide resolved
@pirj
Copy link
Member Author

pirj commented Oct 5, 2021

Checked autocorrection on real-world-rails repo, worked fine.

23177 files inspected, 48 offenses detected, 48 offenses corrected 

@pirj pirj force-pushed the time-arithmetic branch 2 times, most recently from 1f49837 to bd409f6 Compare October 5, 2021 18:02
@pirj
Copy link
Member Author

pirj commented Oct 23, 2021

@andyw8 @koic ping

# Time.current - 1.hour
#
# @example source that matches
# ::Time.zone.now + 1.hour
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the :: for this example?

This comment was marked as outdated.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, my bad, I thought it's in the cop's examples.

For node pattern matcher, this doc shows that both Time and ::Time are matched.

@pirj
Copy link
Member Author

pirj commented Oct 23, 2021

Thanks for suggestions! Please have another look.

@koic koic merged commit 2e46a36 into rubocop:master Oct 24, 2021
@koic
Copy link
Member

koic commented Oct 24, 2021

Thanks!

@pirj pirj deleted the time-arithmetic branch October 24, 2021 19:54

RESTRICT_ON_SEND = %i[+ -].freeze

DURATIONS = Set[:second, :seconds, :minute, :minutes, :hour, :hours,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious if there was a specific reason that month, months, year, and years were excluded from these duration methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've only checked Numeric and completely forgot about Integer.

Would you like to send a PR to fix this?

A spec to check that all are covered would be nice to have, too.

Copy link
Contributor

Choose a reason for hiding this comment

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

@pirj done! #622

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