rubocop-obsession 0.1.6 → 0.1.7
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/lib/rubocop/cop/obsession/method_order.rb +44 -10
- data/lib/rubocop/obsession/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53d3c586b21811a0a13b55a84253df3c26febb690ea9585b960c9c93d08a982e
|
4
|
+
data.tar.gz: 00e2f95178bd563b816a8b961b25470115ad2d25249c5b0c9f2921cd23d6df74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c5f2748fc138701c397c0952250f3a42a7f810e2830ba2421b7cfc526511bd31ad1f788ab7863ef86546848c8c18586d9e45feef025d14bdec12b232304a401
|
7
|
+
data.tar.gz: 07c03a46b82ee6038f9ed5cdc7cb1358c58334a183827bb4b8908c2634a819aec88cc938dd451fc2b8182d734765dc284ce4059ea70d4e87a8b8b8e8ed3b98b6
|
@@ -11,18 +11,21 @@ module RuboCop
|
|
11
11
|
# Private/protected methods should follow that rule.
|
12
12
|
#
|
13
13
|
# Note 1: public methods do not have to follow that rule, and can be
|
14
|
-
# defined in any order the developer wants, like by order of
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
14
|
+
# defined in any order the developer wants, like by order of importance.
|
15
|
+
# This is because they are usually called outside of the class and often
|
16
|
+
# not called within the class at all. If possible though, developers
|
17
|
+
# should still try to order their public methods from top to bottom when
|
18
|
+
# it makes sense.
|
19
19
|
#
|
20
20
|
# Note 2: method order cannot be computed for methods called by `send`,
|
21
21
|
# metaprogramming, private methods called by superclasses or modules,
|
22
22
|
# etc. This cop's suggestions and autocorrections may be slightly off for
|
23
23
|
# these kinds of edge cases.
|
24
24
|
#
|
25
|
-
# Note 3: for
|
25
|
+
# Note 3: for simplicity, protected methods do not have to follow that
|
26
|
+
# rule if there are both a protected section and a private section.
|
27
|
+
#
|
28
|
+
# Note 4: for more information on this style of method ordering, see
|
26
29
|
# Robert C. Martin's "Clean Code" book > "Chapter 3: Functions" > "One
|
27
30
|
# level of abstraction per function" > "Reading Code from Top to Bottom:
|
28
31
|
# The Stepdown Rule" chapter.
|
@@ -62,7 +65,7 @@ module RuboCop
|
|
62
65
|
|
63
66
|
MSG = 'Method `%<after>s` should appear below `%<previous>s`.'
|
64
67
|
|
65
|
-
def_node_search :
|
68
|
+
def_node_search :private_nodes, <<~PATTERN
|
66
69
|
(send nil? {:private :protected})
|
67
70
|
PATTERN
|
68
71
|
|
@@ -103,7 +106,24 @@ module RuboCop
|
|
103
106
|
private
|
104
107
|
|
105
108
|
def find_private_node
|
106
|
-
|
109
|
+
private_nodes = private_nodes(@class_node).to_a
|
110
|
+
return nil if private_nodes.empty?
|
111
|
+
|
112
|
+
visibilities = private_nodes.map(&:method_name)
|
113
|
+
@ignore_protected = visibilities.include?(:protected) && visibilities.include?(:private)
|
114
|
+
|
115
|
+
@private_node = private_nodes.find { |node| !ignore_visibility?(node.method_name) }
|
116
|
+
end
|
117
|
+
|
118
|
+
def ignore_visibility?(visibility)
|
119
|
+
case visibility
|
120
|
+
when :public
|
121
|
+
true
|
122
|
+
when :protected
|
123
|
+
@ignore_protected
|
124
|
+
when :private
|
125
|
+
false
|
126
|
+
end
|
107
127
|
end
|
108
128
|
|
109
129
|
def build_methods
|
@@ -174,7 +194,7 @@ module RuboCop
|
|
174
194
|
end
|
175
195
|
|
176
196
|
def should_ignore?(ast_node)
|
177
|
-
ast_node.nil? || node_visibility(ast_node)
|
197
|
+
ast_node.nil? || ignore_visibility?(node_visibility(ast_node)) ||
|
178
198
|
!@called_methods.include?(ast_node)
|
179
199
|
end
|
180
200
|
|
@@ -213,7 +233,7 @@ module RuboCop
|
|
213
233
|
previous_method_range = previous_method_range.adjust(end_pos: 1)
|
214
234
|
end
|
215
235
|
|
216
|
-
method_range =
|
236
|
+
method_range = source_range_with_signature(method)
|
217
237
|
if buffer.source[method_range.begin_pos - 1] == "\n"
|
218
238
|
method_range = method_range.adjust(end_pos: 1)
|
219
239
|
end
|
@@ -221,6 +241,20 @@ module RuboCop
|
|
221
241
|
corrector.insert_after(previous_method_range, method_range.source)
|
222
242
|
corrector.remove(method_range)
|
223
243
|
end
|
244
|
+
|
245
|
+
def source_range_with_signature(method)
|
246
|
+
previous_node = method.left_sibling
|
247
|
+
begin_node = sorbet_signature?(previous_node) ? previous_node : method
|
248
|
+
|
249
|
+
begin_pos = begin_pos_with_comment(begin_node)
|
250
|
+
end_pos = end_position_for(method)
|
251
|
+
|
252
|
+
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
253
|
+
end
|
254
|
+
|
255
|
+
def sorbet_signature?(node)
|
256
|
+
node && node.method_name == :sig && node.type == :block
|
257
|
+
end
|
224
258
|
end
|
225
259
|
end
|
226
260
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-obsession
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jerome Dalbert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|