rubocop-canon 0.3.0 → 0.4.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/lib/rubocop/canon/version.rb +1 -1
- data/lib/rubocop/cop/canon/block_phases.rb +35 -68
- 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: c71d63b013ac3760733a55c5cc9fd5c48cbcd045a9c9565525300d553bf16ef6
|
|
4
|
+
data.tar.gz: 6c1a59fe8e78ce2fcaf39dff2a20692095634764a85d2c01dc93b16ff63d5881
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3f2a09de9e9c0da1498c32d4e3e3966ab25ade537264012ef775bcfbeed2a1ce553d0f0836cdb97ffcbab52c04fccebd1a09dd77b06b149154c7a861768b73b5
|
|
7
|
+
data.tar.gz: eb62f5fe37820d9063a9eaf2706319053605533c093deea74eab900a94bf6bd4358693e3e5fa9f606e31e68d35dad07950827a61b58216575c2923b51a7eb832
|
|
@@ -3,20 +3,17 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Canon
|
|
6
|
-
# Enforces the phase structure of a block body.
|
|
6
|
+
# Enforces the two-phase structure of a block body.
|
|
7
7
|
#
|
|
8
|
-
# A body checked by this cop
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
8
|
+
# A body checked by this cop has two phases. The *trailing phase* begins
|
|
9
|
+
# at the first call to one of `TrailingMethods` and runs to the end of
|
|
10
|
+
# the body, whatever else it holds. Everything before it is the *setup*.
|
|
11
|
+
# Exactly one blank line separates the two, and no blank line appears
|
|
12
|
+
# inside either — except around a statement spanning several lines,
|
|
13
|
+
# which is always set apart.
|
|
13
14
|
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
# belongs to one arrange phase.
|
|
17
|
-
#
|
|
18
|
-
# A statement spanning several lines is always set apart by a blank line
|
|
19
|
-
# on each side, whatever phase it is in.
|
|
15
|
+
# There is no act phase and nothing is left to the author: where a blank
|
|
16
|
+
# line goes follows from the statements alone.
|
|
20
17
|
#
|
|
21
18
|
# `Blocks` names the methods whose blocks are checked and
|
|
22
19
|
# `TrailingMethods` the calls that make up the trailing phase, matched
|
|
@@ -24,36 +21,30 @@ module RuboCop
|
|
|
24
21
|
# the cop does nothing until it is configured.
|
|
25
22
|
#
|
|
26
23
|
# @example Blocks: ['step'], TrailingMethods: ['report']
|
|
27
|
-
# # bad
|
|
28
|
-
# step 'totals the order' do
|
|
29
|
-
# order = build_order
|
|
30
|
-
# report order.total
|
|
31
|
-
# end
|
|
32
|
-
#
|
|
33
|
-
# # bad — an assignment is arrange, not act
|
|
24
|
+
# # bad
|
|
34
25
|
# step 'totals the order' do
|
|
35
26
|
# order = build_order
|
|
36
27
|
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
# report total
|
|
28
|
+
# settle(order)
|
|
29
|
+
# report order.total
|
|
40
30
|
# end
|
|
41
31
|
#
|
|
42
32
|
# # good
|
|
43
33
|
# step 'totals the order' do
|
|
44
34
|
# order = build_order
|
|
45
|
-
#
|
|
35
|
+
# settle(order)
|
|
46
36
|
#
|
|
47
|
-
# report total
|
|
37
|
+
# report order.total
|
|
48
38
|
# end
|
|
49
39
|
#
|
|
50
|
-
# # good — the call is
|
|
51
|
-
# step '
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
# settle(order)
|
|
40
|
+
# # good — everything from the first trailing call on is one phase
|
|
41
|
+
# step 'lists the orders' do
|
|
42
|
+
# fetch_orders
|
|
55
43
|
#
|
|
56
|
-
# report
|
|
44
|
+
# report status
|
|
45
|
+
# body = parse_body
|
|
46
|
+
# reload
|
|
47
|
+
# report body
|
|
57
48
|
# end
|
|
58
49
|
class BlockPhases < Base
|
|
59
50
|
extend AutoCorrector
|
|
@@ -61,10 +52,8 @@ module RuboCop
|
|
|
61
52
|
|
|
62
53
|
MSG_EXTRA = 'Remove the blank line inside the trailing phase.'
|
|
63
54
|
MSG_MISSING = 'Add a blank line before the trailing phase.'
|
|
64
|
-
|
|
65
|
-
MSG_ACT_UNSEPARATED = 'Add a blank line before the act.'
|
|
55
|
+
MSG_SETUP_SPLIT = 'Remove the blank line inside the setup.'
|
|
66
56
|
MSG_MULTILINE = 'Add a blank line around the multiline statement.'
|
|
67
|
-
ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn masgn op_asgn or_asgn and_asgn].freeze
|
|
68
57
|
|
|
69
58
|
def on_block(node)
|
|
70
59
|
return unless configured_block?(node)
|
|
@@ -83,40 +72,30 @@ module RuboCop
|
|
|
83
72
|
trailing_start = detect_trailing_start(statements)
|
|
84
73
|
return if trailing_start.nil?
|
|
85
74
|
|
|
86
|
-
check_trailing_phase(statements
|
|
87
|
-
|
|
75
|
+
check_trailing_phase(statements[trailing_start..])
|
|
76
|
+
check_setup(statements[...trailing_start], statements[trailing_start])
|
|
88
77
|
end
|
|
89
78
|
|
|
90
|
-
def check_trailing_phase(statements
|
|
91
|
-
statements
|
|
92
|
-
|
|
93
|
-
require_blank_line(previous, following, MSG_MULTILINE)
|
|
94
|
-
else
|
|
95
|
-
forbid_blank_line(previous, following, MSG_EXTRA)
|
|
96
|
-
end
|
|
79
|
+
def check_trailing_phase(statements)
|
|
80
|
+
statements.each_cons(2) do |previous, following|
|
|
81
|
+
check_gap(previous, following, MSG_EXTRA)
|
|
97
82
|
end
|
|
98
83
|
end
|
|
99
84
|
|
|
100
|
-
def
|
|
101
|
-
return if
|
|
102
|
-
|
|
103
|
-
require_blank_line(statements[trailing_start - 1], statements[trailing_start], MSG_MISSING)
|
|
104
|
-
leading = statements[..(trailing_start - 1)]
|
|
85
|
+
def check_setup(statements, first_trailing)
|
|
86
|
+
return if statements.empty?
|
|
105
87
|
|
|
106
|
-
|
|
107
|
-
|
|
88
|
+
require_blank_line(statements.last, first_trailing, MSG_MISSING)
|
|
89
|
+
statements.each_cons(2) do |previous, following|
|
|
90
|
+
check_gap(previous, following, MSG_SETUP_SPLIT)
|
|
108
91
|
end
|
|
109
92
|
end
|
|
110
93
|
|
|
111
|
-
def
|
|
94
|
+
def check_gap(previous, following, extra_message)
|
|
112
95
|
if multiline_pair?(previous, following)
|
|
113
96
|
require_blank_line(previous, following, MSG_MULTILINE)
|
|
114
|
-
elsif assignment?(leading.last)
|
|
115
|
-
forbid_blank_line(previous, following, MSG_ARRANGE_SPLIT)
|
|
116
|
-
elsif index == leading.size - 2
|
|
117
|
-
require_blank_line(previous, following, MSG_ACT_UNSEPARATED)
|
|
118
97
|
else
|
|
119
|
-
forbid_blank_line(previous, following,
|
|
98
|
+
forbid_blank_line(previous, following, extra_message)
|
|
120
99
|
end
|
|
121
100
|
end
|
|
122
101
|
|
|
@@ -140,20 +119,8 @@ module RuboCop
|
|
|
140
119
|
multiline?(following)
|
|
141
120
|
end
|
|
142
121
|
|
|
143
|
-
def assignment?(node)
|
|
144
|
-
ASSIGNMENT_TYPES.include?(node.type)
|
|
145
|
-
end
|
|
146
|
-
|
|
147
122
|
def detect_trailing_start(statements)
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
statements.each_with_index.reverse_each do |statement, index|
|
|
151
|
-
break unless trailing?(statement)
|
|
152
|
-
|
|
153
|
-
trailing_start = index
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
trailing_start
|
|
123
|
+
statements.index { |statement| trailing?(statement) }
|
|
157
124
|
end
|
|
158
125
|
|
|
159
126
|
def configured_block?(node)
|