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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26e0b8c697ca4788f1bf6836c36cffa1d3eb6ddbf2e6422416a2a20e3fb85c41
4
- data.tar.gz: f07d041294a92e97cff5e83c8bfa05ec9f83cca4c00ac0c1ee3a626d0a2da75a
3
+ metadata.gz: c71d63b013ac3760733a55c5cc9fd5c48cbcd045a9c9565525300d553bf16ef6
4
+ data.tar.gz: 6c1a59fe8e78ce2fcaf39dff2a20692095634764a85d2c01dc93b16ff63d5881
5
5
  SHA512:
6
- metadata.gz: d50e6ec2327f88dcca6a886c87e164a592c13bea20e84c95b081f31299f701c97690b62332bfca9ffe8278260d7129cc28ca658db747220783dfe9706657e25c
7
- data.tar.gz: 939c0422bbb87f0a328ae04ccab63c9e816046e8dbbe422faf4879f32d1bd00ceac92e0d840d660ae96e3ead1a13bf5960a3cd7ae2d60c64fb07852d2c61807b
6
+ metadata.gz: 3f2a09de9e9c0da1498c32d4e3e3966ab25ade537264012ef775bcfbeed2a1ce553d0f0836cdb97ffcbab52c04fccebd1a09dd77b06b149154c7a861768b73b5
7
+ data.tar.gz: eb62f5fe37820d9063a9eaf2706319053605533c093deea74eab900a94bf6bd4358693e3e5fa9f606e31e68d35dad07950827a61b58216575c2923b51a7eb832
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Canon
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
@@ -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 ends in a *trailing phase*: the run of
9
- # statements that call one of `TrailingMethods`. Exactly one blank line
10
- # opens that run. Before it come an arrange phase and, when the last
11
- # leading statement is a call rather than an assignment, an act phase
12
- # opened by one blank line. Inside a phase no blank line is allowed.
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
- # An assignment cannot end the act phase. When the last statement before
15
- # the trailing phase assigns, there is no act: every leading statement
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 — no blank line opens the trailing phase
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
- # total = order.total
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
- # total = order.total
35
+ # settle(order)
46
36
  #
47
- # report total
37
+ # report order.total
48
38
  # end
49
39
  #
50
- # # good — the call is the act
51
- # step 'totals the order' do
52
- # order = build_order
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 order.total
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
- MSG_ARRANGE_SPLIT = 'Remove the blank line inside the arrange phase.'
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, trailing_start)
87
- check_leading_phases(statements, trailing_start)
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, trailing_start)
91
- statements[trailing_start..].each_cons(2) do |previous, following|
92
- if multiline_pair?(previous, following)
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 check_leading_phases(statements, trailing_start)
101
- return if trailing_start.zero?
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
- leading.each_cons(2).with_index do |(previous, following), index|
107
- check_leading_gap(leading, previous, following, index)
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 check_leading_gap(leading, previous, following, index)
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, MSG_ARRANGE_SPLIT)
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
- trailing_start = nil
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-canon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - skiftle