rubocop-fourshark 0.11.2 → 0.12.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 +10 -0
- data/lib/rubocop/cop/style/ordered_constants.rb +14 -4
- 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: 2e6ff1114154e4093f52449659fb89bd30418cfcf6057a2175d38a97898e1ef7
|
|
4
|
+
data.tar.gz: a487773631597cee69880424b2c137f7c201e580e67b73eb44d79e1cd1be0dab
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2b23d8a3fc8cc19fe27f04b371ee824ae9688c26a1b0938bc47e0ec47421215913772562f0cc005ed4db3bf6121e37a999e4aa8b5a2116971b31d676ef516e37
|
|
7
|
+
data.tar.gz: d051ae90d627a60471da2ffaf31e7cc031c05027de6b9272db3c6e00fac9027d2efa44c09b4e75a06866ca28015183d4cf096d0248b185eee8e21c1dd079e756
|
data/CHANGELOG.md
CHANGED
|
@@ -7,9 +7,10 @@ module RuboCop
|
|
|
7
7
|
module Style
|
|
8
8
|
# Requires a run of consecutive constant assignments to be sorted
|
|
9
9
|
# alphabetically by name. A lone constant is never flagged; a run is
|
|
10
|
-
# broken by any non-constant statement.
|
|
11
|
-
#
|
|
12
|
-
#
|
|
10
|
+
# broken by any non-constant statement. A constant whose value references
|
|
11
|
+
# one declared above it in the run is not flagged, since Ruby cannot load
|
|
12
|
+
# it any earlier. Flags without autocorrecting. Experimental: if it flags
|
|
13
|
+
# more than it helps, drop it.
|
|
13
14
|
class OrderedConstants < ::RuboCop::Cop::Base
|
|
14
15
|
MSG = 'Sort constant assignments alphabetically (`%<name>s` should come before `%<previous>s`).'
|
|
15
16
|
|
|
@@ -22,16 +23,25 @@ module RuboCop
|
|
|
22
23
|
private
|
|
23
24
|
|
|
24
25
|
def flag_unsorted(run)
|
|
25
|
-
run.each_cons(2) do |previous, current|
|
|
26
|
+
run.each_cons(2).with_index do |(previous, current), index|
|
|
26
27
|
previous_name = constant_name(previous)
|
|
27
28
|
current_name = constant_name(current)
|
|
28
29
|
|
|
29
30
|
next if current_name >= previous_name
|
|
31
|
+
next if references_earlier_constant?(current, run.first(index + 1))
|
|
30
32
|
|
|
31
33
|
add_offense(current.loc.name, message: format(MSG, name: current_name, previous: previous_name))
|
|
32
34
|
end
|
|
33
35
|
end
|
|
34
36
|
|
|
37
|
+
def references_earlier_constant?(node, earlier_constants)
|
|
38
|
+
earlier_names = earlier_constants.map { |constant| constant_name(constant) }
|
|
39
|
+
|
|
40
|
+
node.expression.each_node(:const).any? do |reference|
|
|
41
|
+
reference.namespace.nil? && earlier_names.include?(reference.short_name.to_s)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
35
45
|
def constant_assignment?(node)
|
|
36
46
|
node.is_a?(::RuboCop::AST::Node) && node.casgn_type?
|
|
37
47
|
end
|