rubocop-fourshark 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/rubocop/cop/style/disallow_delegate.rb +37 -12
- data/lib/rubocop/fourshark/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a3715ce918bf2499ae19801f42db4966430bc7a2ae33aae165bc7676865bdfc3
|
|
4
|
+
data.tar.gz: 48d1eac2b33b6971b80463057f859d4f2cbc5efa5d03d298484a9819cb1c1989
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2cb23a767a11947a89a3792c32cb9a3da0e888c83ebd377447b71e9ec21c4643a3105bf361d445fdd1d942b3f92894e68989efcceed7ba4946077f882cdb36b4
|
|
7
|
+
data.tar.gz: c9f15b6c4a0a3340ead5387783a0585423c44a3e299143910c145d3bf086d15924a2d0e93a87213ff2da8f059c6cd897fd05a7f04fe9d6ff29b0517e9d00885b
|
data/CHANGELOG.md
CHANGED
|
@@ -5,31 +5,56 @@ require 'rubocop'
|
|
|
5
5
|
module RuboCop
|
|
6
6
|
module Cop
|
|
7
7
|
module Style
|
|
8
|
-
# Forbids
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
8
|
+
# Forbids delegation — a class answering a question that belongs to a
|
|
9
|
+
# collaborator. The community name for the smell is Fowler's Middle Man,
|
|
10
|
+
# and the prescribed refactoring is Remove Middle Man: delete the
|
|
11
|
+
# forwarder and let the caller navigate to the object that knows.
|
|
12
|
+
#
|
|
13
|
+
# Two shapes are flagged. The macro family (`delegate`,
|
|
14
|
+
# `delegate_missing_to`, `def_delegator`/`def_delegators`,
|
|
15
|
+
# `DelegateClass`) and an instance method whose entire body forwards the
|
|
16
|
+
# same message to a collaborator. The second is the same defect: writing
|
|
17
|
+
# the forwarder by hand changes what `grep` finds, not what the class
|
|
18
|
+
# claims to be responsible for.
|
|
14
19
|
#
|
|
15
20
|
# @example
|
|
16
21
|
# # bad
|
|
17
|
-
# delegate :
|
|
22
|
+
# delegate :name, to: :commission
|
|
18
23
|
#
|
|
19
|
-
# #
|
|
20
|
-
# def
|
|
21
|
-
#
|
|
24
|
+
# # bad — same promise, written by hand
|
|
25
|
+
# def name
|
|
26
|
+
# commission.name
|
|
22
27
|
# end
|
|
23
28
|
#
|
|
29
|
+
# # good — the caller navigates
|
|
30
|
+
# statement.commission.name
|
|
31
|
+
#
|
|
24
32
|
class DisallowDelegate < ::RuboCop::Cop::Base
|
|
25
|
-
|
|
33
|
+
MACRO_MSG = 'Do not use automatic delegation. Delete the forwarder and let the caller navigate.'
|
|
34
|
+
FORWARDER_MSG = 'Do not forward a collaborator\'s message. Delete the forwarder and let the caller navigate.'
|
|
26
35
|
|
|
27
36
|
RESTRICT_ON_SEND = %i[delegate delegate_missing_to def_delegator def_delegators DelegateClass].freeze
|
|
28
37
|
|
|
29
38
|
def on_send(node)
|
|
30
39
|
return unless node.receiver.nil?
|
|
31
40
|
|
|
32
|
-
add_offense(node)
|
|
41
|
+
add_offense(node, message: MACRO_MSG)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def on_def(node)
|
|
45
|
+
return unless forwards_own_message?(node.body, node.method_name)
|
|
46
|
+
|
|
47
|
+
add_offense(node.loc.name, message: FORWARDER_MSG)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def forwards_own_message?(body, method_name)
|
|
53
|
+
return false if body.nil?
|
|
54
|
+
return false unless body.send_type?
|
|
55
|
+
return false if body.receiver.nil?
|
|
56
|
+
|
|
57
|
+
body.method?(method_name)
|
|
33
58
|
end
|
|
34
59
|
end
|
|
35
60
|
end
|