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 months and years to Rails/DurationArithmetic cop duration method set #622

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#622](https://github.com/rubocop/rubocop-rails/pull/622): Add `month(s)` and `year(s)` to `Rails/DurationArithmetic` cop. ([@agrobbin][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/rails/duration_arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class DurationArithmetic < Base
RESTRICT_ON_SEND = %i[+ -].freeze

DURATIONS = Set[:second, :seconds, :minute, :minutes, :hour, :hours,
:day, :days, :week, :weeks, :fortnight, :fortnights]
:day, :days, :week, :weeks, :fortnight, :fortnights,
:month, :months, :year, :years]

# @!method duration_arithmetic_argument?(node)
# Match duration subtraction or addition with current time.
Expand Down
75 changes: 70 additions & 5 deletions spec/rubocop/cop/rails/duration_arithmetic_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,85 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::DurationArithmetic, :config do
it 'registers an offense and corrects' do
it 'registers an offense and corrects Time.zone.now instances' do
expect_offense(<<~RUBY)
Time.zone.now - 1.minute
Time.zone.now - 1.second
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.zone.now - 2.seconds
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.zone.now + 1.second
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.zone.now + 2.seconds
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
RUBY

expect_correction(<<~RUBY)
1.second.ago
2.seconds.ago
1.second.from_now
2.seconds.from_now
RUBY
end

it 'registers an offense and corrects all duration arithmetic methods' do
expect_offense(<<~RUBY)
Time.current - 1.second
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current - 2.seconds
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 1.second
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 2.seconds
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 1.minute
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 2.minutes
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 1.hour
^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 2.hours
^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 1.day
^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 2.days
^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
::Time.current + 1.hour
Time.current + 1.week
^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 2.weeks
^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 1.fortnight
^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 2.fortnights
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 1.month
^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 2.months
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 1.year
^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
Time.current + 2.years
^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
RUBY

expect_correction(<<~RUBY)
1.minute.ago
2.days.from_now
1.second.ago
2.seconds.ago
1.second.from_now
2.seconds.from_now
1.minute.from_now
2.minutes.from_now
1.hour.from_now
2.hours.from_now
1.day.from_now
2.days.from_now
1.week.from_now
2.weeks.from_now
1.fortnight.from_now
2.fortnights.from_now
1.month.from_now
2.months.from_now
1.year.from_now
2.years.from_now
RUBY
end

Expand Down