forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rubocop#486. This PR adds new `Rails/ExpandedDateRange` cop that checks for expanded date range. It only compatible `..` range is targeted. Incompatible `...` range is ignored. ```ruby # bad date.beginning_of_day..date.end_of_day date.beginning_of_week..date.end_of_week date.beginning_of_month..date.end_of_month date.beginning_of_quarter..date.end_of_quarter date.beginning_of_year..date.end_of_year # good date.all_day date.all_week date.all_month date.all_quarter date.all_year ```
- Loading branch information
Showing
7 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks for expanded date range. It only compatible `..` range is targeted. | ||
# Incompatible `...` range is ignored. | ||
# | ||
# @example | ||
# # bad | ||
# date.beginning_of_day..date.end_of_day | ||
# date.beginning_of_week..date.end_of_week | ||
# date.beginning_of_month..date.end_of_month | ||
# date.beginning_of_quarter..date.end_of_quarter | ||
# date.beginning_of_year..date.end_of_year | ||
# | ||
# # good | ||
# date.all_day | ||
# date.all_week | ||
# date.all_month | ||
# date.all_quarter | ||
# date.all_year | ||
# | ||
class ExpandedDateRange < Base | ||
extend TargetRailsVersion | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `%<preferred_method>s` instead.' | ||
|
||
minimum_target_rails_version 5.1 | ||
|
||
def_node_matcher :expanded_date_range, <<~PATTERN | ||
(irange | ||
(send | ||
$_ {:beginning_of_day :beginning_of_week :beginning_of_month :beginning_of_quarter :beginning_of_year}) | ||
(send | ||
$_ {:end_of_day :end_of_week :end_of_month :end_of_quarter :end_of_year})) | ||
PATTERN | ||
|
||
PREFERRED_METHODS = { | ||
beginning_of_day: 'all_day', | ||
beginning_of_week: 'all_week', | ||
beginning_of_month: 'all_month', | ||
beginning_of_quarter: 'all_quarter', | ||
beginning_of_year: 'all_year' | ||
}.freeze | ||
|
||
MAPPED_DATE_RANGE_METHODS = { | ||
beginning_of_day: :end_of_day, | ||
beginning_of_week: :end_of_week, | ||
beginning_of_month: :end_of_month, | ||
beginning_of_quarter: :end_of_quarter, | ||
beginning_of_year: :end_of_year | ||
}.freeze | ||
|
||
def on_irange(node) | ||
return unless expanded_date_range(node) | ||
|
||
begin_node = node.begin | ||
end_node = node.end | ||
return unless same_receiver?(begin_node, end_node) | ||
|
||
beginning_method = begin_node.method_name | ||
end_method = end_node.method_name | ||
return unless use_mapped_methods?(beginning_method, end_method) | ||
|
||
preferred_method = "#{begin_node.receiver.source}.#{PREFERRED_METHODS[beginning_method]}" | ||
|
||
add_offense(node, message: format(MSG, preferred_method: preferred_method)) do |corrector| | ||
corrector.replace(node, preferred_method) | ||
end | ||
end | ||
|
||
private | ||
|
||
def same_receiver?(begin_node, end_node) | ||
begin_node.receiver.source == end_node.receiver.source | ||
end | ||
|
||
def use_mapped_methods?(beginning_method, end_method) | ||
MAPPED_DATE_RANGE_METHODS[beginning_method] == end_method | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ExpandedDateRange, :config do | ||
context 'Rails >= 5.1', :rails51 do | ||
it 'registers and corrects an offense when using `date.beginning_of_day..date.end_of_day`' do | ||
expect_offense(<<~RUBY) | ||
date.beginning_of_day..date.end_of_day | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date.all_day` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
date.all_day | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when using `date.beginning_of_week..date.end_of_week`' do | ||
expect_offense(<<~RUBY) | ||
date.beginning_of_week..date.end_of_week | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date.all_week` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
date.all_week | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when using `date.beginning_of_month..date.end_of_month`' do | ||
expect_offense(<<~RUBY) | ||
date.beginning_of_month..date.end_of_month | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date.all_month` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
date.all_month | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when using `date.beginning_of_quarter..date.end_of_quarter`' do | ||
expect_offense(<<~RUBY) | ||
date.beginning_of_quarter..date.end_of_quarter | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date.all_quarter` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
date.all_quarter | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when using `date.beginning_of_year..date.end_of_year`' do | ||
expect_offense(<<~RUBY) | ||
date.beginning_of_year..date.end_of_year | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date.all_year` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
date.all_year | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when using an expanded date range as a method argument' do | ||
expect_offense(<<~RUBY) | ||
Model.do_something(foo_at: date.beginning_of_day..date.end_of_day) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date.all_day` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.do_something(foo_at: date.all_day) | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when assigning expanded date range' do | ||
expect_offense(<<~RUBY) | ||
foo_at = date.beginning_of_day..date.end_of_day | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date.all_day` instead. | ||
Model.find_by(foo_at: foo_at) | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo_at = date.all_day | ||
Model.find_by(foo_at: foo_at) | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when using multiple expanded date range conditions' do | ||
expect_offense(<<~RUBY) | ||
Model.where(foo_at: date1.beginning_of_week..date1.end_of_week, bar_at: date2.beginning_of_year..date2.end_of_year) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date1.all_week` instead. | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `date2.all_year` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(foo_at: date1.all_week, bar_at: date2.all_year) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `...` range syntax' do | ||
expect_no_offenses(<<~RUBY) | ||
date.beginning_of_day...date.end_of_day | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when variables are different at the beginning and end of the range' do | ||
expect_no_offenses(<<~RUBY) | ||
date1.beginning_of_day..date2.end_of_day | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when unmapped methods are at the beginning and end of the range' do | ||
expect_no_offenses(<<~RUBY) | ||
date.beginning_of_day..date.end_of_year | ||
RUBY | ||
end | ||
end | ||
|
||
context 'Rails <= 5.0', :rails50 do | ||
it 'does not register an offense when using `date.beginning_of_day..date.end_of_day`' do | ||
expect_no_offenses(<<~RUBY) | ||
date.beginning_of_day..date.end_of_day | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `date.beginning_of_week..date.end_of_week`' do | ||
expect_no_offenses(<<~RUBY) | ||
date.beginning_of_week..date.end_of_week | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `date.beginning_of_month..date.end_of_month`' do | ||
expect_no_offenses(<<~RUBY) | ||
date.beginning_of_month..date.end_of_month | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `date.beginning_of_quarter..date.end_of_quarter`' do | ||
expect_no_offenses(<<~RUBY) | ||
date.beginning_of_quarter..date.end_of_quarter | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `date.beginning_of_year..date.end_of_year`' do | ||
expect_no_offenses(<<~RUBY) | ||
date.beginning_of_year..date.end_of_year | ||
RUBY | ||
end | ||
end | ||
end |