Skip to content

Commit a316c0b

Browse files
committed
[Fix #45] Make Rails/Delegate aware of self
Fixes #45. This PR makes `Rails/Delegate` aware of `self`.
1 parent b7ac06b commit a316c0b

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#45](https://github.com/rubocop/rubocop-rails/issues/45): Make `Rails/Delegate` aware of `self`. ([@koic][])

lib/rubocop/cop/rails/delegate.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ module Rails
2424
# # good
2525
# delegate :bar, to: :foo
2626
#
27+
# # bad
28+
# def bar
29+
# self.bar
30+
# end
31+
#
32+
# # good
33+
# delegate :bar, to: :self
34+
#
2735
# # good
2836
# def bar
2937
# foo&.bar
@@ -60,7 +68,7 @@ class Delegate < Base
6068

6169
def_node_matcher :delegate?, <<~PATTERN
6270
(def _method_name _args
63-
(send (send nil? _) _ ...))
71+
(send {(send nil? _) (self)} _ ...))
6472
PATTERN
6573

6674
def on_def(node)
@@ -74,7 +82,11 @@ def on_def(node)
7482

7583
def register_offense(node)
7684
add_offense(node.loc.keyword) do |corrector|
77-
delegation = ["delegate :#{node.body.method_name}", "to: :#{node.body.receiver.method_name}"]
85+
body = node.body
86+
87+
receiver = body.receiver.self_type? ? 'self' : ":#{body.receiver.method_name}"
88+
89+
delegation = ["delegate :#{body.method_name}", "to: #{receiver}"]
7890
delegation << ['prefix: true'] if node.method?(prefixed_method_name(node.body))
7991

8092
corrector.replace(node, delegation.join(', '))
@@ -106,6 +118,8 @@ def include_prefix_case?
106118
end
107119

108120
def prefixed_method_name(body)
121+
return '' if body.receiver.self_type?
122+
109123
[body.receiver.method_name, body.method_name].join('_').to_sym
110124
end
111125

spec/rubocop/cop/rails/delegate_spec.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
RSpec.describe RuboCop::Cop::Rails::Delegate, :config do
44
let(:cop_config) { { 'EnforceForPrefixed' => true } }
5-
let(:config) do
6-
merged = RuboCop::ConfigLoader.default_configuration['Rails/Delegate'].merge(cop_config)
7-
RuboCop::Config.new('Rails/Delegate' => merged)
8-
end
95

106
it 'finds trivial delegate' do
117
expect_offense(<<~RUBY)
@@ -33,6 +29,19 @@ def foo(baz)
3329
RUBY
3430
end
3531

32+
it 'finds trivial delegate to `self`' do
33+
expect_offense(<<~RUBY)
34+
def foo
35+
^^^ Use `delegate` to define delegations.
36+
self.foo
37+
end
38+
RUBY
39+
40+
expect_correction(<<~RUBY)
41+
delegate :foo, to: self
42+
RUBY
43+
end
44+
3645
it 'finds trivial delegate with prefix' do
3746
expect_offense(<<~RUBY)
3847
def bar_foo
@@ -46,6 +55,19 @@ def bar_foo
4655
RUBY
4756
end
4857

58+
it 'finds trivial delegate to `self` when underscored method' do
59+
expect_offense(<<~RUBY)
60+
def bar_foo
61+
^^^ Use `delegate` to define delegations.
62+
self.bar_foo
63+
end
64+
RUBY
65+
66+
expect_correction(<<~RUBY)
67+
delegate :bar_foo, to: self
68+
RUBY
69+
end
70+
4971
it 'ignores class methods' do
5072
expect_no_offenses(<<~RUBY)
5173
def self.fox

0 commit comments

Comments
 (0)