rubocop-canon 0.4.0 → 0.5.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: c71d63b013ac3760733a55c5cc9fd5c48cbcd045a9c9565525300d553bf16ef6
4
- data.tar.gz: 6c1a59fe8e78ce2fcaf39dff2a20692095634764a85d2c01dc93b16ff63d5881
3
+ metadata.gz: f8b4a8966824947c873a90109b21e96a5d2c3fa1d01c71c3423ffe45afa7864e
4
+ data.tar.gz: eea9755c6e9ceab22ab0a316c5ec3eb52e3f290baa9cfe1a546adaaa25dc1bb3
5
5
  SHA512:
6
- metadata.gz: 3f2a09de9e9c0da1498c32d4e3e3966ab25ade537264012ef775bcfbeed2a1ce553d0f0836cdb97ffcbab52c04fccebd1a09dd77b06b149154c7a861768b73b5
7
- data.tar.gz: eb62f5fe37820d9063a9eaf2706319053605533c093deea74eab900a94bf6bd4358693e3e5fa9f606e31e68d35dad07950827a61b58216575c2923b51a7eb832
6
+ metadata.gz: fd398ceccc79ca4d5d8f575dcf9141e84bd0648e7ca673edb166955c56898b65f61efcd4988435cb21831a83485c2972b7e12c97defeb9f5ab7610eead5ad805
7
+ data.tar.gz: 65d340eb6eec5fba2b6d95c592abc238d869dd047135059089583596558191633f227a7a0e9aaa125383c241fd433214a435a252f03ca511e808f067a432adca
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Canon
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
@@ -12,6 +12,7 @@ module RuboCop
12
12
  include RangeHelp
13
13
 
14
14
  BLOCK_TYPES = %i[block numblock].freeze
15
+ MSG_MULTILINE = 'Add a blank line around the multiline statement.'
15
16
 
16
17
  private
17
18
 
@@ -31,6 +32,26 @@ module RuboCop
31
32
  node.first_line != node.last_line
32
33
  end
33
34
 
35
+ def multiline_pair?(previous, following)
36
+ return true if multiline?(previous)
37
+
38
+ multiline?(following)
39
+ end
40
+
41
+ def require_blank_line(previous, following, message)
42
+ return if comments_between?(previous, following)
43
+ return unless blank_lines_between(previous, following).zero?
44
+
45
+ register_missing_blank_line(previous, following, message)
46
+ end
47
+
48
+ def forbid_blank_line(previous, following, message)
49
+ return if comments_between?(previous, following)
50
+ return unless blank_lines_between(previous, following) == 1
51
+
52
+ register_extra_blank_line(previous, message)
53
+ end
54
+
34
55
  def comments_between?(previous, following)
35
56
  processed_source.comments.any? do |comment|
36
57
  comment.loc.line >= previous.last_line && comment.loc.line < following.first_line
@@ -53,7 +53,6 @@ module RuboCop
53
53
  MSG_EXTRA = 'Remove the blank line inside the trailing phase.'
54
54
  MSG_MISSING = 'Add a blank line before the trailing phase.'
55
55
  MSG_SETUP_SPLIT = 'Remove the blank line inside the setup.'
56
- MSG_MULTILINE = 'Add a blank line around the multiline statement.'
57
56
 
58
57
  def on_block(node)
59
58
  return unless configured_block?(node)
@@ -99,26 +98,6 @@ module RuboCop
99
98
  end
100
99
  end
101
100
 
102
- def require_blank_line(previous, following, message)
103
- return if comments_between?(previous, following)
104
- return unless blank_lines_between(previous, following).zero?
105
-
106
- register_missing_blank_line(previous, following, message)
107
- end
108
-
109
- def forbid_blank_line(previous, following, message)
110
- return if comments_between?(previous, following)
111
- return unless blank_lines_between(previous, following) == 1
112
-
113
- register_extra_blank_line(previous, message)
114
- end
115
-
116
- def multiline_pair?(previous, following)
117
- return true if multiline?(previous)
118
-
119
- multiline?(following)
120
- end
121
-
122
101
  def detect_trailing_start(statements)
123
102
  statements.index { |statement| trailing?(statement) }
124
103
  end
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Canon
6
6
  # Enforces blank lines around a method body's standalone calls and
7
- # nowhere else.
7
+ # multiline statements, and nowhere else.
8
8
  #
9
9
  # `SeparatedMethods` names the calls that stand on their own —
10
10
  # `authorize!`, `expose`, `mail`. Each takes exactly one blank line
@@ -15,8 +15,15 @@ module RuboCop
15
15
  # The list is empty by default, so the cop only removes until it is
16
16
  # configured.
17
17
  #
18
- # The position after a guard clause belongs to
19
- # `Layout/EmptyLineAfterGuardClause`, so this cop leaves it alone.
18
+ # Guard clauses group: two in a row take no blank line between them.
19
+ # The position after the last guard belongs to
20
+ # `Layout/EmptyLineAfterGuardClause`, so this cop leaves it alone — and
21
+ # recognises a guard by that cop's own definition: an `if` whose branch
22
+ # is a single-line `return`, `break`, `next`, `raise` or `fail`. A bare
23
+ # `raise` on its own line is a statement, not a guard.
24
+ #
25
+ # A statement spanning several lines is always set apart by a blank
26
+ # line on each side, guards included.
20
27
  #
21
28
  # An entry matches a method name anywhere in the call chain. Write it
22
29
  # dotted — `errors.add` — to match a receiver and method together, so
@@ -47,12 +54,12 @@ module RuboCop
47
54
  # Current.account = nil
48
55
  # end
49
56
  #
50
- # # good — the guard clause owns its own blank line
57
+ # # good — guards group, and the last one owns the blank line after it
51
58
  # def total
52
- # items = order.items
53
- # return 0 if items.empty?
59
+ # return 0 if order.nil?
60
+ # return 0 if order.items.empty?
54
61
  #
55
- # items.sum(&:amount)
62
+ # order.items.sum(&:amount)
56
63
  # end
57
64
  class MethodBodyBlankLines < Base
58
65
  extend AutoCorrector
@@ -60,7 +67,7 @@ module RuboCop
60
67
 
61
68
  MSG_EXTRA = 'Remove the blank line inside the method body.'
62
69
  MSG_MISSING = 'Add a blank line around the standalone call.'
63
- JUMP_METHODS = %i[raise fail throw].freeze
70
+ JUMP_METHODS = %i[raise fail].freeze
64
71
 
65
72
  def on_def(node)
66
73
  check_method_body(node)
@@ -77,29 +84,32 @@ module RuboCop
77
84
  return if statements.size < 2
78
85
 
79
86
  statements.each_cons(2) do |previous, following|
80
- next if guard?(previous)
87
+ next if guard_boundary?(previous, following)
81
88
 
82
- check_gap(previous, following, expected_blank_lines(previous, following))
89
+ check_gap(previous, following)
83
90
  end
84
91
  end
85
92
 
86
- def check_gap(previous, following, expected)
87
- return if comments_between?(previous, following)
88
-
89
- blank_lines = blank_lines_between(previous, following)
90
- return if blank_lines == expected
91
- return if blank_lines > 1
93
+ def guard_boundary?(previous, following)
94
+ return false unless guard?(previous)
92
95
 
93
- return register_extra_blank_line(previous, MSG_EXTRA) if expected.zero?
96
+ !guard?(following)
97
+ end
94
98
 
95
- register_missing_blank_line(previous, following, MSG_MISSING)
99
+ def check_gap(previous, following)
100
+ if multiline_pair?(previous, following)
101
+ require_blank_line(previous, following, MSG_MULTILINE)
102
+ elsif separated_pair?(previous, following)
103
+ require_blank_line(previous, following, MSG_MISSING)
104
+ else
105
+ forbid_blank_line(previous, following, MSG_EXTRA)
106
+ end
96
107
  end
97
108
 
98
- def expected_blank_lines(previous, following)
99
- return 1 if separated?(previous)
100
- return 1 if separated?(following)
109
+ def separated_pair?(previous, following)
110
+ return true if separated?(previous)
101
111
 
102
- 0
112
+ separated?(following)
103
113
  end
104
114
 
105
115
  def separated?(node)
@@ -116,21 +126,23 @@ module RuboCop
116
126
  end
117
127
 
118
128
  def guard?(node)
119
- return true if jump?(node)
120
129
  return false unless node.if_type?
121
- return false unless node.modifier_form?
122
- return true if jump?(node.if_branch)
123
130
 
124
- jump?(node.else_branch)
131
+ branch = node.if_branch
132
+ return false if branch.nil?
133
+ return true if branch.guard_clause?
134
+
135
+ jump?(branch)
125
136
  end
126
137
 
127
138
  def jump?(node)
128
- return false if node.nil?
129
139
  return true if node.return_type?
130
140
  return true if node.break_type?
131
141
  return true if node.next_type?
142
+ return false unless node.send_type?
143
+ return false unless node.receiver.nil?
132
144
 
133
- node.send_type? && JUMP_METHODS.include?(node.method_name)
145
+ JUMP_METHODS.include?(node.method_name)
134
146
  end
135
147
 
136
148
  def separated_methods
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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - skiftle