rubocop-canon 0.2.0 → 0.2.1
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/canon/version.rb +1 -1
- data/lib/rubocop/cop/canon/block_phases.rb +18 -4
- data/lib/rubocop/cop/canon/declaration_groups.rb +38 -33
- 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: 24d224c2ecf5e361f702c300a79778baf8ad63247fb726cb8c39b89534d7dd3f
|
|
4
|
+
data.tar.gz: 8e4e0ca31f844a61bdba984c2efd2ccadb2d0ddea11693f38dc452c9b356e40c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b1eea01a2c5652d7513ccfe08b048cd61817f4f20c3dcb757c5e7b0e96ac99c92d0b52f1840d40b69a9c3c5b2eced42444c22ae29ed3461eedc83c1dbd4b5081
|
|
7
|
+
data.tar.gz: 98f0a6237ae0b71995af7bf03e8d8b46bcf83ebd92f3fa0ddf81f607a503ab71061b365f2e19a05279702791998be00d7cd1d621224dcdd965ae79d2d3fc2d84
|
|
@@ -15,6 +15,10 @@ module RuboCop
|
|
|
15
15
|
# against the leftmost call in a chain. Both are empty by default, so
|
|
16
16
|
# the cop does nothing until it is configured.
|
|
17
17
|
#
|
|
18
|
+
# A body carrying more phases than `MaxPhases` allows keeps the last
|
|
19
|
+
# separator before the trailing phase and loses the earlier ones —
|
|
20
|
+
# everything ahead of the last leading statement is one phase.
|
|
21
|
+
#
|
|
18
22
|
# @example Blocks: ['step'], TrailingMethods: ['report']
|
|
19
23
|
# # bad — no blank line opens the trailing phase
|
|
20
24
|
# step 'totals the order' do
|
|
@@ -91,12 +95,22 @@ module RuboCop
|
|
|
91
95
|
end
|
|
92
96
|
|
|
93
97
|
def check_phase_count(statements)
|
|
94
|
-
separators = statements
|
|
95
|
-
|
|
98
|
+
separators = leading_separators(statements)
|
|
99
|
+
return if separators.size < max_phases - 1
|
|
100
|
+
|
|
101
|
+
surplus = separators[0..-(max_phases - 1)]
|
|
102
|
+
|
|
103
|
+
surplus.each do |previous|
|
|
104
|
+
register_extra_blank_line(previous, format(MSG_TOO_MANY_PHASES, max_phases:))
|
|
96
105
|
end
|
|
97
|
-
|
|
106
|
+
end
|
|
98
107
|
|
|
99
|
-
|
|
108
|
+
def leading_separators(statements)
|
|
109
|
+
statements.each_cons(2).filter_map do |previous, following|
|
|
110
|
+
next if comments_between?(previous, following)
|
|
111
|
+
|
|
112
|
+
previous if blank_lines_between(previous, following) == 1
|
|
113
|
+
end
|
|
100
114
|
end
|
|
101
115
|
|
|
102
116
|
def detect_trailing_start(statements)
|
|
@@ -6,31 +6,26 @@ module RuboCop
|
|
|
6
6
|
# Enforces blank lines between declaration groups in a class, module or
|
|
7
7
|
# DSL block body.
|
|
8
8
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
9
|
+
# `GroupedMethods` is the whole specification: two declarations calling
|
|
10
|
+
# methods listed in the same group take no blank line between them, and
|
|
11
|
+
# two calling methods listed in different groups take exactly one. The
|
|
12
|
+
# cop says nothing about a method name it has not been given, so a DSL
|
|
13
|
+
# it does not know stays as its author wrote it.
|
|
13
14
|
#
|
|
14
|
-
#
|
|
15
|
+
# A block is a group of one whatever it calls, so a DSL block always
|
|
16
|
+
# stands apart from the declarations around it. Everything else the cop
|
|
17
|
+
# has not been told about — an unlisted method name, a constant — it
|
|
18
|
+
# leaves alone.
|
|
15
19
|
#
|
|
16
|
-
# @example
|
|
20
|
+
# @example GroupedMethods: [[belongs_to, has_many], [validate, validates]]
|
|
17
21
|
# # bad
|
|
18
22
|
# class Shift < ApplicationRecord
|
|
19
|
-
# belongs_to :service
|
|
20
|
-
#
|
|
21
|
-
# belongs_to :site
|
|
22
|
-
# validates :starts_at, presence: true
|
|
23
|
-
# end
|
|
24
|
-
#
|
|
25
|
-
# # good
|
|
26
|
-
# class Shift < ApplicationRecord
|
|
27
|
-
# belongs_to :service
|
|
28
23
|
# belongs_to :site
|
|
29
24
|
#
|
|
25
|
+
# has_many :shift_assignments, dependent: :destroy
|
|
30
26
|
# validates :starts_at, presence: true
|
|
31
27
|
# end
|
|
32
28
|
#
|
|
33
|
-
# @example GroupedMethods: [[belongs_to, has_many]]
|
|
34
29
|
# # good
|
|
35
30
|
# class Shift < ApplicationRecord
|
|
36
31
|
# belongs_to :site
|
|
@@ -38,6 +33,12 @@ module RuboCop
|
|
|
38
33
|
#
|
|
39
34
|
# validates :starts_at, presence: true
|
|
40
35
|
# end
|
|
36
|
+
#
|
|
37
|
+
# # good — neither name is listed, so the cop leaves the body alone
|
|
38
|
+
# class Filtering < Capability::Base
|
|
39
|
+
# request_transformer RequestTransformer
|
|
40
|
+
# api_builder APIBuilder
|
|
41
|
+
# end
|
|
41
42
|
class DeclarationGroups < Base
|
|
42
43
|
extend AutoCorrector
|
|
43
44
|
include BlankLineHelp
|
|
@@ -46,7 +47,8 @@ module RuboCop
|
|
|
46
47
|
MSG_MISSING = 'Add a blank line between declaration groups.'
|
|
47
48
|
ACCESS_MODIFIERS = %i[private protected public module_function private_class_method].freeze
|
|
48
49
|
DEFINITION_TYPES = %i[def defs].freeze
|
|
49
|
-
|
|
50
|
+
BLOCK_TYPES = %i[block numblock].freeze
|
|
51
|
+
STANDALONE_TYPES = %i[block numblock def defs].freeze
|
|
50
52
|
|
|
51
53
|
def on_class(node)
|
|
52
54
|
check_declarations(node)
|
|
@@ -75,6 +77,7 @@ module RuboCop
|
|
|
75
77
|
next if definitions?(previous, following)
|
|
76
78
|
next if access_modifier?(previous)
|
|
77
79
|
next if access_modifier?(following)
|
|
80
|
+
next unless grouped?(previous, following)
|
|
78
81
|
|
|
79
82
|
check_gap(previous, following, expected_blank_lines(previous, following))
|
|
80
83
|
end
|
|
@@ -92,26 +95,30 @@ module RuboCop
|
|
|
92
95
|
register_missing_blank_line(previous, following, MSG_MISSING)
|
|
93
96
|
end
|
|
94
97
|
|
|
98
|
+
def grouped?(previous, following)
|
|
99
|
+
return true if standalone?(previous)
|
|
100
|
+
return true if standalone?(following)
|
|
101
|
+
return false if group_key(previous).nil?
|
|
102
|
+
|
|
103
|
+
!group_key(following).nil?
|
|
104
|
+
end
|
|
105
|
+
|
|
95
106
|
def expected_blank_lines(previous, following)
|
|
107
|
+
return 1 if standalone?(previous)
|
|
108
|
+
return 1 if standalone?(following)
|
|
96
109
|
return 0 if group_key(previous) == group_key(following)
|
|
97
110
|
|
|
98
111
|
1
|
|
99
112
|
end
|
|
100
113
|
|
|
101
|
-
def
|
|
102
|
-
|
|
103
|
-
return :constant if node.casgn_type?
|
|
104
|
-
return node.object_id unless node.send_type?
|
|
105
|
-
|
|
106
|
-
grouped_key(node.method_name)
|
|
114
|
+
def standalone?(node)
|
|
115
|
+
STANDALONE_TYPES.include?(node.type)
|
|
107
116
|
end
|
|
108
117
|
|
|
109
|
-
def
|
|
110
|
-
|
|
111
|
-
group_index = grouped_methods.index { |group| group.include?(name) }
|
|
112
|
-
return name if group_index.nil?
|
|
118
|
+
def group_key(node)
|
|
119
|
+
return unless node.send_type?
|
|
113
120
|
|
|
114
|
-
|
|
121
|
+
grouped_methods.index { |group| group.include?(node.method_name.to_s) }
|
|
115
122
|
end
|
|
116
123
|
|
|
117
124
|
def declaration_context?(node)
|
|
@@ -123,7 +130,9 @@ module RuboCop
|
|
|
123
130
|
end
|
|
124
131
|
|
|
125
132
|
def declaration?(node)
|
|
126
|
-
return true if
|
|
133
|
+
return true if node.casgn_type?
|
|
134
|
+
return true if DEFINITION_TYPES.include?(node.type)
|
|
135
|
+
return node.send_node.receiver.nil? if BLOCK_TYPES.include?(node.type)
|
|
127
136
|
|
|
128
137
|
node.send_type? && node.receiver.nil?
|
|
129
138
|
end
|
|
@@ -139,10 +148,6 @@ module RuboCop
|
|
|
139
148
|
ACCESS_MODIFIERS.include?(node.method_name)
|
|
140
149
|
end
|
|
141
150
|
|
|
142
|
-
def multiline?(node)
|
|
143
|
-
node.first_line != node.last_line
|
|
144
|
-
end
|
|
145
|
-
|
|
146
151
|
def grouped_methods
|
|
147
152
|
@grouped_methods ||= Array(cop_config['GroupedMethods']).map { |group| Array(group).map(&:to_s) }
|
|
148
153
|
end
|