rubocop-obsession 0.1.5 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/config/default.yml +0 -2
- data/lib/rubocop/cop/obsession/method_order.rb +46 -34
- 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
|
data/README.md
CHANGED
@@ -9,6 +9,9 @@ There are some lower-level cops as well.
|
|
9
9
|
Use the provided cops as is, or as inspiration to build custom cops aligned
|
10
10
|
with your project's best practices.
|
11
11
|
|
12
|
+
Does your project have any cool custom cops that you think others might benefit
|
13
|
+
from? Then please open a PR!
|
14
|
+
|
12
15
|
## Installation
|
13
16
|
|
14
17
|
Install the gem:
|
data/config/default.yml
CHANGED
@@ -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.
|
@@ -56,13 +59,13 @@ module RuboCop
|
|
56
59
|
# def method_c; ...; end
|
57
60
|
class MethodOrder < Base
|
58
61
|
include Helpers
|
59
|
-
include
|
62
|
+
include CommentsHelp
|
60
63
|
include VisibilityHelp
|
61
64
|
extend AutoCorrector
|
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
|
|
@@ -209,39 +229,31 @@ module RuboCop
|
|
209
229
|
|
210
230
|
def autocorrect(corrector, method, previous_method)
|
211
231
|
previous_method_range = source_range_with_comment(previous_method)
|
212
|
-
|
232
|
+
if buffer.source[previous_method_range.end_pos + 1] == "\n"
|
233
|
+
previous_method_range = previous_method_range.adjust(end_pos: 1)
|
234
|
+
end
|
235
|
+
|
236
|
+
method_range = source_range_with_signature(method)
|
237
|
+
if buffer.source[method_range.begin_pos - 1] == "\n"
|
238
|
+
method_range = method_range.adjust(end_pos: 1)
|
239
|
+
end
|
213
240
|
|
214
241
|
corrector.insert_after(previous_method_range, method_range.source)
|
215
242
|
corrector.remove(method_range)
|
216
243
|
end
|
217
244
|
|
218
|
-
def
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
def start_position_with_comment(node)
|
223
|
-
first_comment = nil
|
224
|
-
|
225
|
-
(node.first_line - 1).downto(1) do |annotation_line|
|
226
|
-
comment = processed_source.comment_at_line(annotation_line)
|
227
|
-
break if !comment
|
228
|
-
first_comment = comment if whole_line_comment_at_line?(annotation_line)
|
229
|
-
end
|
230
|
-
|
231
|
-
start_line_position(first_comment || node)
|
232
|
-
end
|
245
|
+
def source_range_with_signature(method)
|
246
|
+
previous_node = method.left_sibling
|
247
|
+
begin_node = sorbet_signature?(previous_node) ? previous_node : method
|
233
248
|
|
234
|
-
|
235
|
-
|
236
|
-
end
|
249
|
+
begin_pos = begin_pos_with_comment(begin_node)
|
250
|
+
end_pos = end_position_for(method)
|
237
251
|
|
238
|
-
|
239
|
-
processed_source.buffer.line_range(node.loc.line).begin_pos - 1
|
252
|
+
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
240
253
|
end
|
241
254
|
|
242
|
-
def
|
243
|
-
|
244
|
-
processed_source.buffer.line_range(end_line).end_pos
|
255
|
+
def sorbet_signature?(node)
|
256
|
+
node && node.method_name == :sig && node.type == :block
|
245
257
|
end
|
246
258
|
end
|
247
259
|
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
|
+
date: 2024-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|