Skip to content

Commit c680d24

Browse files
authored
Merge pull request #866 from r7kamura/feature/fix-const-nil-cbase
Fix false-positives when constant is used with receiver on `Rails/DurationArithmetic`, `Rails/IndexBy`, `Rails/IndexWIth`, and `Rails/RequireDependency`
2 parents 3d7228e + 3191081 commit c680d24

File tree

9 files changed

+79
-10
lines changed

9 files changed

+79
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#866](https://github.com/rubocop/rubocop-rails/pull/866): Fix false-positives when constant is used with receiver on `Rails/DurationArithmetic`, `Rails/IndexBy`, `Rails/IndexWIth`, and `Rails/RequireDependency`. ([@r7kamura][])

lib/rubocop/cop/rails/duration_arithmetic.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class DurationArithmetic < Base
7070
# @return [Boolean] true if matches
7171
def_node_matcher :time_current?, <<~PATTERN
7272
{
73-
(send (const _ :Time) :current)
74-
(send (send (const _ :Time) :zone) :now)
73+
(send (const {nil? cbase} :Time) :current)
74+
(send (send (const {nil? cbase} :Time) :zone) :now)
7575
}
7676
PATTERN
7777

lib/rubocop/cop/rails/index_by.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class IndexBy < Base
4646

4747
def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
4848
(send
49-
(const _ :Hash)
49+
(const {nil? cbase} :Hash)
5050
:[]
5151
(block
5252
(call _ {:map :collect})

lib/rubocop/cop/rails/index_with.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class IndexWith < Base
4949

5050
def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
5151
(send
52-
(const _ :Hash)
52+
(const {nil? cbase} :Hash)
5353
:[]
5454
(block
5555
(call _ {:map :collect})

lib/rubocop/cop/rails/require_dependency.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class RequireDependency < Base
2626
RESTRICT_ON_SEND = %i[require_dependency].freeze
2727

2828
def_node_matcher :require_dependency_call?, <<~PATTERN
29-
(send {nil? (const _ :Kernel)} :require_dependency _)
29+
(send {nil? (const {nil? cbase} :Kernel)} :require_dependency _)
3030
PATTERN
3131

3232
def on_send(node)

spec/rubocop/cop/rails/duration_arithmetic_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,32 @@
9797
created_at - 1.minute
9898
RUBY
9999
end
100+
101+
it 'does not register an offense for `Foo::Time.current`' do
102+
expect_no_offenses(<<~RUBY)
103+
Foo::Time.current + 1.hour
104+
RUBY
105+
end
106+
107+
it 'registers and correct an offense for `::Time.current`' do
108+
expect_offense(<<~RUBY)
109+
::Time.current + 1.hour
110+
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
111+
RUBY
112+
113+
expect_correction(<<~RUBY)
114+
1.hour.from_now
115+
RUBY
116+
end
117+
118+
it 'registers and correct an offense for `::Time.zone.now`' do
119+
expect_offense(<<~RUBY)
120+
::Time.zone.now + 1.hour
121+
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
122+
RUBY
123+
124+
expect_correction(<<~RUBY)
125+
1.hour.from_now
126+
RUBY
127+
end
100128
end

spec/rubocop/cop/rails/index_by_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@
156156
RUBY
157157
end
158158

159+
it 'does not register an offense for `Foo::Hash[map { ... }]`' do
160+
expect_no_offenses(<<~RUBY)
161+
Foo::Hash[x.map { |el| [el.to_sym, el] }]
162+
RUBY
163+
end
164+
159165
context 'when using Ruby 2.6 or newer', :ruby26 do
160166
it 'registers an offense for `to_h { ... }`' do
161167
expect_offense(<<~RUBY)

spec/rubocop/cop/rails/index_with_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@
129129
RUBY
130130
end
131131

132+
it 'does not register an offense for `Foo::Hash[map { ... }]`' do
133+
expect_no_offenses(<<~RUBY)
134+
Foo::Hash[x.map { |el| [el, el.to_sym] }]
135+
RUBY
136+
end
137+
132138
context 'when using Ruby 2.6 or newer', :ruby26 do
133139
it 'registers an offense for `to_h { ... }`' do
134140
expect_offense(<<~RUBY)

spec/rubocop/cop/rails/require_dependency_spec.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,39 @@
1919

2020
context 'when require_dependency' do
2121
context 'when using Rails 6.0 or newer', :rails60 do
22-
it 'registers an offense' do
23-
expect_offense(<<~RUBY)
24-
require_dependency 'foo'
25-
^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `require_dependency` with Zeitwerk mode.
26-
RUBY
22+
context 'without receiver' do
23+
it 'registers an offense' do
24+
expect_offense(<<~RUBY)
25+
require_dependency 'foo'
26+
^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `require_dependency` with Zeitwerk mode.
27+
RUBY
28+
end
29+
end
30+
31+
context 'with `Kernel` as receiver' do
32+
it 'registers an offense' do
33+
expect_offense(<<~RUBY)
34+
Kernel.require_dependency 'foo'
35+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `require_dependency` with Zeitwerk mode.
36+
RUBY
37+
end
38+
end
39+
40+
context 'with `::Kernel` as receiver' do
41+
it 'registers an offense' do
42+
expect_offense(<<~RUBY)
43+
::Kernel.require_dependency 'foo'
44+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `require_dependency` with Zeitwerk mode.
45+
RUBY
46+
end
47+
end
48+
49+
context 'with `Foo::Kernel` as receiver' do
50+
it 'does not register an offense' do
51+
expect_no_offenses(<<~RUBY)
52+
Foo::Kernel.require_dependency 'foo'
53+
RUBY
54+
end
2755
end
2856
end
2957

0 commit comments

Comments
 (0)